Coder Social home page Coder Social logo

Comments (29)

helfer avatar helfer commented on May 3, 2024 8

@LoicMahieu: Unless you're running a modified version of graphql-js, this shouldn't be a problem if you npm dedupe. The limitation has to do with flow type checking.

from graphiql.

hallettj avatar hallettj commented on May 3, 2024 4

Just want to mention that when importing a schema from another package, and using yarn link in development, I run into the same problems that @LoicMahieu describes with lerna.

Edit: I found a solution, which I wanted to share in case anyone else runs into this issue: do more linking so that each of your packages is linked to the same copy of graphql. (This also works for other dependencies that are sensitive to multiple copies, such as react.)

In this scenario, schema-package exports a graphql schema, and server-package imports that schema, perhaps to serve it over HTTP:

  1. $ yarn global add graphql
  2. $ cd ~/.config/yarn/global/node_modules/graphql && yarn link
  3. $ cd path/to/schema-package && yarn link graphql && yarn link
  4. $ cd path/to/server-package && yarn link graphql && yarn link schema-package

After these steps, server-package is linked to your development copy of schema-package, and both are linked to the same copy of graphql. This should cause instanceof checks in graphql code to work properly.

@leebyron You said that the error has to do with flow type-checking; but I think that is not correct. The error comes from three runtime checks that look like this:

  invariant(
    schema instanceof GraphQLSchema,
    'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
    'not multiple versions of GraphQL installed in your node_modules directory.'
  );

Some options for avoiding errors like this when using lerna, yarn link, or npm link could be to change the check from an instanceof check to a check for expected properties, or to disable invariant checks if an environment variable or configuration option is set.

from graphiql.

sulliwane avatar sulliwane commented on May 3, 2024 2

Ok, it's related to babel 6 (babel-core). If I use [email protected] then no problem. Maybe there is a babel 6 preset that could work, I just tried with presets: ['es2015', 'stage-1', 'react'] and with presets: ['es2015'], both without success (graphql/graphql-js#228)

from graphiql.

marvinhagemeister avatar marvinhagemeister commented on May 3, 2024 2

Find the culprit: Seems like the documentation isn't fully updated yet to reflect the recent changes in v0.2. The solution is to follow the upgrade guide which contains more information then the server setup doc.

from graphiql.

richburdon avatar richburdon commented on May 3, 2024 2

I think this is the solution (if you're using webpack):

http://stackoverflow.com/questions/31169760/how-to-avoid-react-loading-twice-with-webpack-when-developing

from graphiql.

davidMuir avatar davidMuir commented on May 3, 2024 2

In case this helps anyone else, I've came across this when mixing ES6 and CommonJS modules. Choosing one or the other sorted it.

from graphiql.

aymericbouzy avatar aymericbouzy commented on May 3, 2024 2

In my case, the issue was not having twice graphql but twice @types/graphql. Downgrading my apollo-client version from ^1.9.1 to 1.9.0 solved it :

$ yarn add [email protected]

I found that by inspecting yarn.lock file, and identifying which dependency was requiring a different version of @types/graphql.

from graphiql.

colllin avatar colllin commented on May 3, 2024 1

In case it helps anyone else, I got this error when graphql was installed as a devDependency instead of a normal dependency with the rest of my graphql-related packages.

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

I'm not able to reproduce this. Please let me know if you stumble upon a clearer reason for the issue so I can help fix.

from graphiql.

muskacirca avatar muskacirca commented on May 3, 2024

I think I'm running into the same issue. I tried to use [email protected] but with no success

here is my project : https://github.com/muskacirca/graphqlserver

If anyone have an idea on what is going on, it will be great.

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

Sometimes this occurs if you have multiple copies of graphql-js in your node_modules folder, that can happen if different parts of your app require different versions of the library.

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

A good way to diagnose this is to run npm shrinkwrap and then look to see if multiple copies of graphql-js appear in the outputted file.

from graphiql.

muskacirca avatar muskacirca commented on May 3, 2024

Actually I was getting the first error : Schema must be an instance of GraphQLSchema

It was an error in how I export & import my schema.

export var Schema = new GraphQLSchema({
query: GraphQLRoot
});

import {Schema} from './schema' --> adding the brackets solve my issue

from graphiql.

sorenbs avatar sorenbs commented on May 3, 2024

@leebyron I am getting this exact error message with multiple copies of the same version of graphql in node_modules. Running npm shrinkwrap all entries ae like this: graphql: '^0.4.18'

I have two modules, both depend on graphql and one depends on the other. Is this unsupported, or could you give any pointers to what I could try?

ps. this is not related to graphiql i think, but posting here as this is the only mention i have seen of this.

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

Unfortunately there's no work around for this at the moment. The solution is to ensure that there is exactly one copy of graphql-js in your node_modules (recursively).

The latest versions of npm (v3) should install maximally flat, so if you have multiple dependencies that require graphql, it should just be a matter of ensuring that they depend on the same version of graphql-js

from graphiql.

sorenbs avatar sorenbs commented on May 3, 2024

Thanks Lee!

from graphiql.

LoicMahieu avatar LoicMahieu commented on May 3, 2024

Is there any reason about the necessary to share the same graphql-js module ? I wanted to split my code in multiple modules, one module for graphql defintions and one other module for serving it with express-graphql. Unfortunatly, due to this limitation, this is not possible.

from graphiql.

LoicMahieu avatar LoicMahieu commented on May 3, 2024

@helfer In my case the problem is not the version. We use have several packages in the same repository and we use lerna in development for linking packages together.
We wanted to separate schema and http related in different packages but they doesn't share the same GraphQL module in this case.

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

I haven't used lerna before - but due to how JavaScript's instanceof operator works, it's unfortunately necessary to ensure only one copy of graphql is included at runtime. I know that if you use npm, and your multiple packages all depend on a common version of graphql, then only a single copy will be installed. If that fails, npm dedupe is a good tool. You should investigate if lerna has similar capabilities.

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

To be clear: it's certainly possible to split up your graphql definitions into multiple packages, and it's a pattern I see very often in larger codebases. The critical detail is that the package.json for each must refer to the same version of graphql so only one copy is installed in the node_modules directory.

from graphiql.

marvinhagemeister avatar marvinhagemeister commented on May 3, 2024

I have the same issue. graphql is only installed once and my schema is a simple array of GraphQL schema language type definitions as stated in the docs.

EDIT: Have only tested with graphiql so far.

from graphiql.

muskacirca avatar muskacirca commented on May 3, 2024

Have you checked the way you import yoiur schema ?

export var Schema = new GraphQLSchema({query: GraphQLRoot});

import {Schema} from './schema'

from graphiql.

marvinhagemeister avatar marvinhagemeister commented on May 3, 2024

@muskacirca: Yes, but I'm using a type definition array. According to the docs this should work as well. Here my setup:

schema.js:

const typeDefinitions = `
    type Person {
        id: Int!
    }

    # this schema allows the following two queries:
    type RootQuery {
        person(id: Int): Person
    }

    type RootMutation {
        createPerson(
            name: String!
        ): Person
    }

    schema {
        query: RootQuery
        mutation: RootMutation
    }
`;

export default [typeDefinitions];

server.js

import koa from 'koa';
import koaRouter from 'koa-router'
import { apolloKoa, graphiqlKoa } from 'apollo-server';
import schema from './graphql/schemas';
import mocks from '../data/mock';

const app = new koa();
const router = new koaRouter();

router.get('/graphiql', graphiqlKoa({
    endpointURL: '/graphql'
}));

router.post('/graphql', apolloKoa({
    schema,
    mocks,
    printErrors: true
}));

app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, () =>
    console.log('GraphQL running on 3000')); // eslint-disable-line no-console

from graphiql.

leebyron avatar leebyron commented on May 3, 2024

Glad you figured it out

from graphiql.

richburdon avatar richburdon commented on May 3, 2024

Still having this problem. All recursive dependencies have the same version:

> npm --version
3.10.7

> babel-node --version
6.16.0

> find node_modules -name graphql
node_modules/graphql
node_modules/graphql-subscriptions/node_modules/graphql

> grep version node_modules/graphql/package.json 
0.7.2

> grep version node_modules/graphql-subscriptions/node_modules/graphql/package.json
0.7.2

from graphiql.

afreeland avatar afreeland commented on May 3, 2024

@davidMuir I was afraid of this =(

I am using npm link on a local module which appears to give babel a little bit of trouble trying to transpile, so I am mixing es6 with CommonJS...I will be happy when all this stuff settles down a bit and I can just build things =)

from graphiql.

richburdon avatar richburdon commented on May 3, 2024

If you're using lerna make sure you run lerna bootstrap --hoist so that common deps are included in the root package and shared amongst other packages.

from graphiql.

DavidHe1127 avatar DavidHe1127 commented on May 3, 2024

Had the same issue and turned out I have both apollo-client and apollo-server-restify installed under the same project. Remove apollo-client resolves my issue

from graphiql.

TSMMark avatar TSMMark commented on May 3, 2024

@richburdon's solution worked for me. In webpack config:

resolve: {
  alias: {
    graphql: path.resolve('./node_modules/graphql')
  }
}

from graphiql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.