Skip to content

Issue with apollo-server-koa and es modules #1035

Closed
@Kaltsoon

Description

@Kaltsoon

I've found an issue while running a query in a simple koa server using es modules with Node v10.0.0. My index.mjs server file is the following:

import { GraphQLSchema, GraphQLObjectType, GraphQLString, graphql } from 'graphql';
import Koa from 'koa';
import KoaRouter from 'koa-router';
import apolloKoa from 'apollo-server-koa';
import koaBody from 'koa-bodyparser'

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      test: {
        type: GraphQLString,
        resolve: () => 'Hello world!'
      }
    })
  })
});

const apolloMiddleware = apolloKoa.graphqlKoa({
  schema,
});

const app = new Koa();

const router = new KoaRouter();

app.use(koaBody());

router.post('/graphql', apolloMiddleware);
router.get('/graphql', apolloMiddleware);

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

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(4567);

When I start the server with node --experimental-modules ./index.mjs everything goes smoothly, but as soon as I open http://localhost:4567/graphiql or send query directly to http://localhost:4567/graphql, this error apperas:

Error: Cannot use GraphQLSchema "[object Object]" 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 (/Users/kalle/Desktop/test/node_modules/graphql/jsutils/instanceOf.js:15:13)
      at isSchema (/Users/kalle/Desktop/test/node_modules/graphql/type/schema.js:52:35)
      at validateSchema (/Users/kalle/Desktop/test/node_modules/graphql/type/validate.js:55:25)
      at assertValidSchema (/Users/kalle/Desktop/test/node_modules/graphql/type/validate.js:80:16)
      at Object.validate (/Users/kalle/Desktop/test/node_modules/graphql/validation/validate.js:58:35)
      at doRunQuery (/Users/kalle/Desktop/test/node_modules/apollo-server-core/dist/runQuery.js:110:38)
      at /Users/kalle/Desktop/test/node_modules/apollo-server-core/dist/runQuery.js:21:56
      at process._tickCallback (internal/process/next_tick.js:178:7)

I have read issues such as this, but they don't seem to match my case.

Everything works fine with CommonJS modules using require, but I'm not satisfied with that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions