From 0b6ee9c8c7ba985269ff58d3a667278ce662c46e Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Fri, 31 Jan 2025 04:27:35 -0500 Subject: [PATCH] enhance(transport-rest): use the default fetch if no fetch exists in the transport context (#8351) --- .changeset/seven-crabs-cough.md | 5 +++++ packages/transports/rest/src/directives/httpOperation.ts | 7 ++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 .changeset/seven-crabs-cough.md diff --git a/.changeset/seven-crabs-cough.md b/.changeset/seven-crabs-cough.md new file mode 100644 index 0000000000000..61564d3ba45cd --- /dev/null +++ b/.changeset/seven-crabs-cough.md @@ -0,0 +1,5 @@ +--- +'@graphql-mesh/transport-rest': patch +--- + +Use `@whatwg-node/fetch` when the transport context doesn't have one diff --git a/packages/transports/rest/src/directives/httpOperation.ts b/packages/transports/rest/src/directives/httpOperation.ts index 7285573da0acd..bad64159eb20a 100644 --- a/packages/transports/rest/src/directives/httpOperation.ts +++ b/packages/transports/rest/src/directives/httpOperation.ts @@ -9,7 +9,7 @@ import { stringInterpolator } from '@graphql-mesh/string-interpolation'; import type { Logger, MeshFetch, MeshFetchRequestInit } from '@graphql-mesh/types'; import { DefaultLogger, getHeadersObj } from '@graphql-mesh/utils'; import { createGraphQLError, memoize1 } from '@graphql-tools/utils'; -import { Blob, File, FormData, URLSearchParams } from '@whatwg-node/fetch'; +import { Blob, fetch as defaultFetch, File, FormData, URLSearchParams } from '@whatwg-node/fetch'; import { isFileUpload } from './isFileUpload.js'; import { getJsonApiFieldsQuery } from './jsonApiFields.js'; import { resolveDataByUnionInputType } from './resolveDataByUnionInputType.js'; @@ -296,10 +296,7 @@ export function addHTTPRootFieldResolver( } operationLogger.debug(`=> Fetching `, fullPath, `=>`, requestInit); - const fetch: typeof globalFetch = context?.fetch || globalFetch; - if (!fetch) { - return new TypeError(`You should have fetch defined in either the config or the context!`); - } + const fetch: typeof globalFetch = context?.fetch || globalFetch || defaultFetch; // Trick to pass `sourceName` to the `fetch` function for tracing const response = await fetch(fullPath, requestInit, context, { ...info,