diff --git a/.changeset/mighty-clouds-taste.md b/.changeset/mighty-clouds-taste.md new file mode 100644 index 00000000000..1fe5d9101be --- /dev/null +++ b/.changeset/mighty-clouds-taste.md @@ -0,0 +1,7 @@ +--- +'@graphql-tools/batch-delegate': patch +'@graphql-tools/delegate': patch +'@graphql-tools/utils': patch +--- + +enhance: avoid using globalThis diff --git a/packages/batch-delegate/src/getLoader.ts b/packages/batch-delegate/src/getLoader.ts index c1f4130c55e..d0ba711562c 100644 --- a/packages/batch-delegate/src/getLoader.ts +++ b/packages/batch-delegate/src/getLoader.ts @@ -61,10 +61,12 @@ const getLoadersMap = memoize3(function getLoadersMap( return new Map>(); }); +const GLOBAL_CONTEXT = {}; + export function getLoader(options: BatchDelegateOptions): DataLoader { const { schema, fieldName, context, info, dataLoaderOptions } = options; const targetFieldName = fieldName ?? info.fieldName; - const loaders = getLoadersMap(context ?? globalThis ?? window ?? global, info.fieldNodes, schema); + const loaders = getLoadersMap(context ?? GLOBAL_CONTEXT, info.fieldNodes, schema); let loader = loaders.get(targetFieldName); diff --git a/packages/delegate/src/delegateToSchema.ts b/packages/delegate/src/delegateToSchema.ts index 0e079cc63ed..dbd86c3b805 100644 --- a/packages/delegate/src/delegateToSchema.ts +++ b/packages/delegate/src/delegateToSchema.ts @@ -186,6 +186,8 @@ function validateRequest(delegationContext: DelegationContext, document: Do } } +const GLOBAL_CONTEXT = {}; + function getExecutor(delegationContext: DelegationContext): Executor { const { subschemaConfig, targetSchema, context } = delegationContext; @@ -194,7 +196,7 @@ function getExecutor(delegationContext: DelegationContext): if (subschemaConfig?.batch) { const batchingOptions = subschemaConfig?.batchingOptions; executor = getBatchingExecutor( - context ?? globalThis ?? window ?? global, + context ?? GLOBAL_CONTEXT, executor, batchingOptions?.dataLoaderOptions, batchingOptions?.extensionsReducer diff --git a/packages/utils/src/AggregateError.ts b/packages/utils/src/AggregateError.ts index 4d13b500c93..34bca120d30 100644 --- a/packages/utils/src/AggregateError.ts +++ b/packages/utils/src/AggregateError.ts @@ -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); @@ -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 };