graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

Receive 'Cannot use GraphQLSchema' error when using makeExecutableSchema in junction with SchemaLink

Open MikeWinfied opened this issue 6 years ago • 20 comments

Hi, everyone!

When trying to run GraphQL w/o a server, providing SchemaLink created using makeExecutableSchema, I receive an error when running a query:

Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
    at new ApolloError (https://t0i3t.csb.app/node_modules/@apollo/client/errors/ApolloError.js:42:24)
    at ObservableQuery.getCurrentResult (https://t0i3t.csb.app/node_modules/@apollo/client/core/ObservableQuery.js:123:18)
    at QueryData._this.getQueryResult (https://t0i3t.csb.app/node_modules/@apollo/client/react/data/QueryData.js:42:53)
    at QueryData.getExecuteResult (https://t0i3t.csb.app/node_modules/@apollo/client/react/data/QueryData.js:160:23)
    at QueryData.execute (https://t0i3t.csb.app/node_modules/@apollo/client/react/data/QueryData.js:113:47)
    at eval (https://t0i3t.csb.app/node_modules/@apollo/client/react/hooks/utils/useBaseQuery.js:56:55)
    at useDeepMemo (https://t0i3t.csb.app/node_modules/@apollo/client/react/hooks/utils/useDeepMemo.js:19:14)
    at useBaseQuery (https://t0i3t.csb.app/node_modules/@apollo/client/react/hooks/utils/useBaseQuery.js:55:50)
    at useQuery (https://t0i3t.csb.app/node_modules/@apollo/client/react/hooks/useQuery.js:14:46)
    at Message (https://t0i3t.csb.app/src/App.js:59:40)
    at renderWithHooks (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:16260:18)
    at updateFunctionComponent (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:18347:20)
    at beginWork$1 (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:20176:16)
    at beginWork$$1 (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:25756:14)
    at performUnitOfWork (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:24695:12)
    at workLoopSync (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:24671:22)
    at performSyncWorkOnRoot (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:24270:11)
    at eval (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:12199:24)
    at unstable_runWithPriority (https://t0i3t.csb.app/node_modules/scheduler/cjs/scheduler.development.js:697:12)
    at runWithPriority$2 (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:12149:10)
    at flushSyncCallbackQueueImpl (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:12194:7)
    at flushSyncCallbackQueue (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:12182:3)
    at scheduleUpdateOnFiber (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:23709:9)
    at dispatchAction (https://t0i3t.csb.app/node_modules/react-dom/cjs/react-dom.development.js:17076:5)"

My dependencies are:

    "@apollo/client": "3.0.1",
    "@graphql-tools/schema": "6.0.13",
    "graphql": "15.3.0",
    "react": "16.12.0",
    "react-dom": "16.12.0",

From what I can gather, I have only one graphql available so I'm not even sure that the message hits in the right direction.

The demo is available here

I don't have a GraphQL server available at my disposal. After considering the @rest directive I've come to the conclusion that having full-bodied resolvers would be cleaner and would easily integrate into the existing codebase.

MikeWinfied avatar Jul 16 '20 11:07 MikeWinfied

Hi @MikeWinfied . I ran that sandbox code locally and couldn't reproduce the issue. Is the code where you're seeing the issue also being run on Code Sandbox or are you getting that same error when running the code locally?

danielrearden avatar Jul 16 '20 12:07 danielrearden

Hi, @danielrearden! Thanks for your quick reply! I'm seeing the same error on my local environment. The code on sandbox is a boiled-down version to trace down the issue.

MikeWinfied avatar Jul 16 '20 12:07 MikeWinfied

@danielrearden @MikeWinfied We don't have graphql as dependency in our packages. Maybe it is @apollo/client that has graphql as its dependency.

ardatan avatar Jul 16 '20 12:07 ardatan

@ardatan does it mean that the issue should be redirected to @apollo/client?

MikeWinfied avatar Jul 16 '20 12:07 MikeWinfied

@ardatan I thought it might be an issue with @apollo/client too, but npm ls shows only the one version installed. And the code runs fine for me as long as its not on Code Sandbox

danielrearden avatar Jul 16 '20 12:07 danielrearden

@MikeWinfied can you try removing node_modules and then running npm install or yarn install? You can also verify that there's not more than one version being installed by running npm ls graphql (it should only show the one version).

danielrearden avatar Jul 16 '20 12:07 danielrearden

@danielrearden, the same 😓 The removal didn't help and yarn list --pattern graphql yields only one version of GrahpQL:

├─ @graphql-tools/[email protected]
├─ @graphql-tools/[email protected]
├─ [email protected]
└─ [email protected]

MikeWinfied avatar Jul 16 '20 13:07 MikeWinfied

You might get the same error even with the same version of graphql if you have multiple instances of GraphQL, i.e. if was not always being used as a peer dependency, or if sometimes uses as a fork of graphql.

Did apollo fork graphql at some point internally?

Not sure if above is relevant or even actually true in full, just thinking out loud (sort of).

yaacovCR avatar Jul 16 '20 14:07 yaacovCR

https://github.com/graphql/graphql-js/issues/2523#issuecomment-659457211

yaacovCR avatar Jul 16 '20 15:07 yaacovCR

Hi, @danielrearden! Should I address my issue elsewhere? It's interesting that everything works fine if one downgrades graphql-js to 14.7.0.

MikeWinfied avatar Jul 23 '20 09:07 MikeWinfied

Based on the comment in the issue linked above, it sounds like this error might be specific to @apollo/client@3 like @ardatan originally suggested :/

danielrearden avatar Jul 23 '20 11:07 danielrearden

Workers?

https://github.com/graphql/graphql-js/issues/2801#issuecomment-696768674

yaacovCR avatar Oct 06 '20 12:10 yaacovCR

Running into this when trying to use the approach mentioned by OP in a Cypress Component Test. Tried to work around it by dynamically importing all needed dependencies, but cypress doesn't support that currently in that scenario.

    "@apollo/client": "3.3.0-beta.13",
    "@graphql-tools/schema": "^6.2.4",
    "cypress": "^5.4.0",
    "cypress-react-unit-test": "^4.16.1",

stoically avatar Oct 17 '20 09:10 stoically

After some digging, what I found so far is that the default cypress webpack bundle ends up using different entry points for the graphql module:

@graphql-tools/schema seems to use the esm version (.mjs)

/* harmony import */ var graphql__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! graphql */ "./node_modules/graphql/index.mjs");

while @apollo/client/link/schema seems to use the commonjs version (.js)

/* harmony import */ var graphql_execution_execute__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! graphql/execution/execute */ "./node_modules/graphql/execution/execute.js");

stoically avatar Oct 17 '20 10:10 stoically

Submitted an PR over at apollo-client (https://github.com/apollographql/apollo-client/issues/7184) which fixes the issue for me. If you'd like to see if it works for you as well, feel free to test with

    "@apollo/client": "github:stoically/apollo-client-dist",

as dependency

stoically avatar Oct 17 '20 12:10 stoically

Is this issue still relevant?

ardatan avatar Oct 27 '20 02:10 ardatan

The fix that went into 3.3.0-beta14 and 3.2.5 solved it for me

stoically avatar Oct 27 '20 07:10 stoically

Ok closing this issue then because the issue seems unrelated to graphql-tools.

ardatan avatar Oct 27 '20 07:10 ardatan

Hi @stoically , have you been able to fix this issue?, I'm getting the same issue with Cypress as well, it seems that there is an issue when using

import { makeExecutableSchema } from '@graphql-tools/schema' makeExecutableSchema({ typeDefs: schema })

Any ideas ? Thank you

rodrigo398 avatar Sep 11 '21 15:09 rodrigo398

Could you please create a reproduction in a different issue? Thanks!

ardatan avatar Sep 11 '21 20:09 ardatan