question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"one instance of graphql" error when using esm package.

See original GitHub issue

When using esm package (https://github.com/standard-things/esm) even most simple package.json configurations (only dependancies are esm and graphql-yoga) results in the Error: Cannot use GraphQLSchema "[object Object]" from another module or realm. error when using imports.

node -r esm testWithImport.js will fail while node testWithRequire.js and node -r esm testWithRequire.js will work.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jdaltoncommented, Aug 28, 2018

This happens if you were to add a new npm script called

"mjsImport": "node --experimental-modules serverWithImport.mjs"

and a test file serverWithImport.mjs:

import graphglYoga from 'graphql-yoga';
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";

const { GraphQLServer } = graphglYoga

var Something = new GraphQLObjectType({
    fields: {
        else: { type: GraphQLString },
    },
    name: 'Something',
})

var schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        fields: {
            something: {
                type: Something,
            },
        },
        name: 'Query',
    }),
});

var server = new GraphQLServer({ schema });
const instance = server.start(); // defaults to port 4000

too.

The issue is that graphql-yoga is CJS (and loads the CJS entry of graphql) but you’re loading the ESM version of graphql.

Because graphgl is running two entry points they create 2 separate instances (a limitation of their current dual approach). The way around it is for graphql to instantiate their instances in CJS and export them for the ESM entry to consume.

0reactions
hoscommented, Jul 31, 2019

My solution was to replace

import graphql from 'graphql'

with

import graphql from 'graphql/index.js'

I was using flags --experimental-modules --es-module-specifier-resolution=node to omit extensions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix the annoying "Ensure there is only one instance of ...
Error: Cannot use GraphQLSchema "[object Object]" from another module or realm. Ensure that there is only one instance of "graphql" in the node_modules ......
Read more >
GraphQL error handling to the max with Typescript, codegen ...
In REST, one can send a response with a appropriate status code to inform the caller about the kind of exception encountered, e.g.:...
Read more >
graphql-upload - npm
Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js ...
Read more >
Migrating to Apollo Client 3.0 - Apollo GraphQL Docs
The @apollo/client package includes both React hooks and GraphQL request handling, ... If you are updating an existing application to use Apollo Client...
Read more >
gqlr - NPM Package Overview - Socket - Socket.dev
Actually Isomorphic (works with Node / browsers). Ships a real ESM module, instead of the fake one TS generates. Why? graphql-request was causing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found