graphiql icon indicating copy to clipboard operation
graphiql copied to clipboard

Schema must be an instance of GraphQLSchema

Open sulliwane opened this issue 11 years ago • 29 comments

Hello, I get this error when hitting /graphql with browser:

{
  "errors": [
    {
      "message": "Schema must be an instance of GraphQLSchema. Also ensure that there are not multiple versions of GraphQL installed in your node_modules directory."
    }
  ]
}

any suggestion? My schema is an instance of GraphQLSchema...and graphql appears only once in package.json

thanks

sulliwane avatar Nov 16 '15 07:11 sulliwane

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 (https://github.com/graphql/graphql-js/issues/228)

sulliwane avatar Nov 16 '15 11:11 sulliwane

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.

leebyron avatar Nov 30 '15 20:11 leebyron

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.

muskacirca avatar Feb 01 '16 13:02 muskacirca

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.

leebyron avatar Feb 03 '16 09:02 leebyron

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.

leebyron avatar Feb 03 '16 09:02 leebyron

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

muskacirca avatar Feb 21 '16 11:02 muskacirca

@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.

sorenbs avatar Mar 07 '16 21:03 sorenbs

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

leebyron avatar Mar 07 '16 22:03 leebyron

Thanks Lee!

sorenbs avatar Mar 07 '16 22:03 sorenbs

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.

LoicMahieu avatar Jun 15 '16 11:06 LoicMahieu

@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.

helfer avatar Jun 15 '16 17:06 helfer

@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.

LoicMahieu avatar Jun 16 '16 07:06 LoicMahieu

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.

leebyron avatar Jun 17 '16 01:06 leebyron

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.

leebyron avatar Jun 17 '16 01:06 leebyron

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.

marvinhagemeister avatar Aug 17 '16 09:08 marvinhagemeister

Have you checked the way you import yoiur schema ?

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

import {Schema} from './schema'

muskacirca avatar Aug 17 '16 10:08 muskacirca

@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

marvinhagemeister avatar Aug 17 '16 10:08 marvinhagemeister

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.

marvinhagemeister avatar Aug 17 '16 12:08 marvinhagemeister

Glad you figured it out

leebyron avatar Aug 17 '16 16:08 leebyron

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

richburdon avatar Nov 18 '16 00:11 richburdon