graphql-hive / gateway

GraphQL gateway that can act as a Federation Gateway or a Proxy Gateway for any GraphQL service.
https://the-guild.dev/graphql/hive/docs/gateway
MIT License
7 stars 1 forks source link

GraphQL Mesh with OpenAPI cannot find fetch even if it is defined using getExecutorForUnifiedGraph #1

Open MichaelTheFear opened 2 weeks ago

MichaelTheFear commented 2 weeks ago

I was trying to follow two parts of a tutorial for GraphQL Mesh:

  1. OpenAPI / Swagger Source Handlers
  2. Local Execution

Those two parts are and for some reason when I run them separately it works fine, but when I decide to join them together (meaning running the Mesh with the OpenAPI Source, and the getExecutorForUnifiedGraph) it gives me the following result:

{
  errors: [
    TypeError: You should have fetch defined in either the config or the context!
        at field.resolve (file:///usr/src/app/node_modules/@graphql-mesh/transport-rest/esm/directives/httpOperation.js:229:20)
        at executeField (file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/execute.js:317:24)
        at executeFields (file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/execute.js:265:28)
        at executeOperation (file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/execute.js:225:18)
        at file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/execute.js:65:45
        at new ValueOrPromise (/usr/src/app/node_modules/value-or-promise/build/main/ValueOrPromise.js:14:21)
        at executeImpl (file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/execute.js:65:20)
        at execute (file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/execute.js:48:12)
        at file:///usr/src/app/node_modules/@graphql-tools/executor/esm/execution/normalizedExecutor.js:12:37
        at new ValueOrPromise (/usr/src/app/node_modules/value-or-promise/build/main/ValueOrPromise.js:14:21) {
      path: [Array],
      locations: undefined,
      extensions: [Object: null prototype] {}
    }
  ],
  data: [Object: null prototype] { feed_availability: null }
}

I did find in this page that I can pass my own fetch as a parameter of the Mesh, but trying it out with node-fetch or whatwg-node/fetch won't solve it.

I did however find a temporary solution,which is adding this two lines inthe httpOperation.js file of the graphql-mesh

This one in the line 9:

import { createGraphQLError, memoize1 } from '@graphql-tools/utils';
import { Blob, File, FormData, URLSearchParams, fetch as whatwgFetch } from '@whatwg-node/fetch';
import { isFileUpload } from './isFileUpload.js';

And this one in the line 223:

operationLogger.debug(`=> Fetching `, fullPath, `=>`, requestInit);
const fetch = context?.fetch || globalFetch || whatwgFetch;
if (!fetch) {

Basically this solution is adding a fallback for the whatwgFetch in case there is no other fetch being define.

I was wondering if I should open this issue in the GraphQL Mesh repo, but since it works fine with the normal npx hive-gateway supergraph supergraph.js, and only has this error when using the getExecutorForUnifiedGraph I decided to open this here.

Here is a repo in which you can reproduce this error: hive-fetch-error

dotansimha commented 1 week ago

Moved to the Hive's Gateway repo. @ardatan thoughts?