Skip to content

Commit

Permalink
Avoid using globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Dec 9, 2021
1 parent 595c687 commit 7fa08fc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/mighty-clouds-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-tools/delegate': patch
'@graphql-tools/utils': patch
---

enhance: avoid using globalThis
4 changes: 3 additions & 1 deletion packages/batch-delegate/src/getLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ const getLoadersMap = memoize3(function getLoadersMap<K, V, C>(
return new Map<string, DataLoader<K, V, C>>();
});

const GLOBAL_CONTEXT = {};

export function getLoader<K = any, V = any, C = K>(options: BatchDelegateOptions<any>): DataLoader<K, V, C> {
const { schema, fieldName, context, info, dataLoaderOptions } = options;
const targetFieldName = fieldName ?? info.fieldName;
const loaders = getLoadersMap<K, V, C>(context ?? globalThis ?? window ?? global, info.fieldNodes, schema);
const loaders = getLoadersMap<K, V, C>(context ?? GLOBAL_CONTEXT, info.fieldNodes, schema);

let loader = loaders.get(targetFieldName);

Expand Down
4 changes: 3 additions & 1 deletion packages/delegate/src/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ function validateRequest(delegationContext: DelegationContext<any>, document: Do
}
}

const GLOBAL_CONTEXT = {};

function getExecutor<TContext>(delegationContext: DelegationContext<TContext>): Executor<TContext> {
const { subschemaConfig, targetSchema, context } = delegationContext;

Expand All @@ -194,7 +196,7 @@ function getExecutor<TContext>(delegationContext: DelegationContext<TContext>):
if (subschemaConfig?.batch) {
const batchingOptions = subschemaConfig?.batchingOptions;
executor = getBatchingExecutor(
context ?? globalThis ?? window ?? global,
context ?? GLOBAL_CONTEXT,
executor,
batchingOptions?.dataLoaderOptions,
batchingOptions?.extensionsReducer
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/src/AggregateError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ interface AggregateErrorConstructor {
readonly prototype: AggregateError;
}

let AggregateErrorImpl: AggregateErrorConstructor = globalThis.AggregateError;
let AggregateErrorImpl: AggregateErrorConstructor;

if (typeof AggregateErrorImpl === 'undefined') {
if (typeof AggregateError === 'undefined') {
class AggregateErrorClass extends Error implements AggregateError {
constructor(public errors: any[], message = '') {
super(message);
Expand All @@ -22,6 +22,8 @@ if (typeof AggregateErrorImpl === 'undefined') {
AggregateErrorImpl = function (errors: any[], message?: string) {
return new AggregateErrorClass(errors, message);
} as AggregateErrorConstructor;
} else {
AggregateErrorImpl = AggregateError;
}

export { AggregateErrorImpl as AggregateError };
Expand Down

0 comments on commit 7fa08fc

Please sign in to comment.