Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Kaltsoon opened this issue May 4, 2018 · 2 comments
Closed

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

Kaltsoon opened this issue May 4, 2018 · 2 comments

Comments

@Kaltsoon
Copy link

Kaltsoon commented May 4, 2018

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.

@jaydenseric
Copy link

jaydenseric commented May 16, 2018

This is because apollo-server is published only as CJS/.js without ESM/.mjs. What happens, is that graphql is imported using ESM in your code, but it is imported in apollo-server code using CJS. The objects are in different .js/.mjs modules and graphql refuses to cooperate via that assertion, thinking you are loading two different GraphQL packages.

The appropriate solution is for Apollo to publish native-ESM capable packages. Until then, you can create a graphql.js file in your project, containing:

module.exports = require('graphql')

And then in your server file:

- import { GraphQLSchema, GraphQLObjectType, GraphQLString, graphql } from 'graphql';
+ import graphql from './graphql'
+ const { GraphQLSchema, GraphQLObjectType, GraphQLString, graphql } = graphql

It's a sad workaround, I know 😣

Here is a PR for the exact same issue in express-graphql: graphql/express-graphql#433

If you're using graphql-relay, you might want to subscribe to this too: graphql/graphql-relay-js#209

@Kaltsoon
Copy link
Author

Thanks @jaydenseric, this will do for now. 🙂

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants