apollographql/apollo-link

SchemaLink dependency issue

Closed this issue · 12 comments

I'm following this example:
https://www.apollographql.com/docs/link/links/schema.html#Mocking

Expected Behavior
I would expect that apollo client is created with provided schema.

Actual Behavior

but as soon as I import SchemaLink it breaks my app with following error:

Uncaught Error: Cannot use GraphQLNonNull "Boolean!" 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 instanceOf (instanceOf.js:37)
    at isNonNullType (definition.mjs:94)
    at isRequiredArgument (definition.mjs:467)
    at Array.filter (<anonymous>)
    at ProvidedRequiredArgumentsOnDirectives (ProvidedRequiredArguments.mjs:91)
    at validate.mjs:53
    at Array.map (<anonymous>)
    at validateSDL (validate.mjs:52)
    at assertValidSDL (validate.mjs:66)
    at Object.buildASTSchema (buildASTSchema.mjs:47)

A simple reproduction

here's my actual code:

// import ApolloClient from 'apollo-boost';
import {ApolloClient} from 'apollo-client';
import {InMemoryCache} from 'apollo-cache-inmemory';
import {SchemaLink} from 'apollo-link-schema';

import {makeExecutableSchema, addMockFunctionsToSchema} from 'graphql-tools';
import typeDefs from './schema';
import mocks from './mocks';

// Create GraphQL schema object
const schema = makeExecutableSchema({typeDefs});

// Add mocks
addMockFunctionsToSchema({schema, mocks});

const cache = new InMemoryCache(window.__APOLLO_STATE__);

export default () =>
  new ApolloClient({
    link: new SchemaLink({schema}),
    cache,
  });

and here's my dependencies:

    "apollo-boost": "^0.3.1",
    "apollo-cache-inmemory": "^1.5.1",
    "apollo-client": "^2.5.1",
    "apollo-link-http": "^1.5.14",
    "apollo-link-schema": "^1.2.2",
    "graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0",
    "graphql-tag": "^2.10.1",
    "graphql-tools": "^4.0.4",
    "react": "^16.8.4",
    "react-apollo": "^2.5.2",
    "react-dom": "^16.8.4",

actually I had to add this to package.json event to be able to add apollo-link-schema to a dependencies list:

  "resolutions": {
    "graphql-tools": "^4.0.4"
  }

without this, even having apollo-link-schema in dependencies list cause the error.

so got it working with this in package.json:

  "resolutions": {
    "graphql": "0.11.7",
    "**/graphql": "0.11.7"
  }

but still, would like to have a better solution.

The problem here is the outdated dependency on graphql. The version of graphql-tools you are using requires a higher graphql version

  "peerDependencies": {
    "graphql": "^0.13.0 || ^14.0.0"
  },

This makes for a missmatch, that error occures when two instances are requiring the graphql dep in another version since it's supposed to be a singleton.

It's a good practice not to be too loosly with your deps since you are allowing different versions of graphql.

well, I know that, but it doesn't work if I use graphql above v0.11.7 which doesn't make sense to me.
and it's strange, all other packages (apollo-cache-inmemory, apollo-client, etc) seems to require similar versions: either "^0.13.0 || ^14.0.0" or "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
so what is the correct solution here?

is it because graphql-link-schema depends on "graphql-tools": "2.24.0" and this versions of graphql-tools depends on older version of graphql?

I'll look into this tonight. It would really be of much help if you could provide me with a minimal reproduction, that way I can test if I'm covering your use case with my fixes.

here you go:
https://github.com/edmundas-ramanauskas/apollo-demo
to launch it execute:
yarn install
yarn start

then, to provoke an error, just upgrade main dependency on graphql to "graphql": "^14.1.1", and execute rm -rf node_modules && yarn install and yarn start

@JoviDeCroock any news on this one?

Hey sorry @edmundas-ramanauskas I haven't had a lot of time as of late. I'll try to find some this week. Sorry for the inconvenience

The issue was located in your react-apollo version, you can safely remove the resolutions if you upgrade your react-apollo version. React-apollo was requiring the old graphql version instead of accepting the deduplication with newer one.

thanks, it solved the problem.