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.

Cannot use GraphQLObjectType "Query" from another module or realm

See original GitHub issue

Bug report

I’m trying to implement this middleware in my express based graphql server. However on doing so I receive an error which is only there when applying this middleware.

Received error

Error: Cannot use GraphQLObjectType "Query" 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.

image

Code

import path from "path";
import { ApolloServer, makeExecutableSchema } from "apollo-server-express";
import { importSchema } from "graphql-import";
import { applyMiddleware } from "graphql-middleware";

import { shield, allow } from "graphql-shield";

import Mutation from "./resolvers/Mutation";
import Query from "./resolvers/Query";

const schema = makeExecutableSchema({
	typeDefs: importSchema(path.resolve("../src/schema.graphql")),
	resolvers: {
		Mutation,
		Query
	}
});

// No errors when you remove shield from the applyMiddleware call 
const adjustedSchema = applyMiddleware(schema, shield({}, { fallbackRule: allow }));

export default new ApolloServer({
	schema: adjustedSchema, // No errors when you pass schema: schema
	resolverValidationOptions: {
		requireResolversForResolveType: false
	},
	context: ctx => ctx
});

Versions used (all latest at this point in time)

"apollo-server-express": "^2.6.9",
"express": "^4.17.1",
"express-graphql": "^0.8.0",
"graphql": "^14.4.2",
"graphql-import": "^0.7.1",
"graphql-middleware": "^4.0.0",
"graphql-shield": "^6.0.4",
"prisma-binding": "^2.3.15"

How did I try to debug already

  • Remove node_modules folder and run npm install
  • Search the package-lock.json file for other versions specified for graphql, not the case

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
maticzavcommented, Aug 16, 2019

@nealoke thank you so much for sharing all the progress, the solution, everything. Thanks!

I am genuinely glad you resolved it! 🙂

1reaction
nealokecommented, Aug 15, 2019

I found the issue, and like I thought has nothing to do with this package. It basically was an issue with our setup which uses hoisting to pull all the dependencies to the root.

Inside the webpack.config.js I was using webpack-node-externals but only for the modules in the package (eg. packages/api/node_modules). So because most of the packages are pulled to the root it did not exclude these when building the server.js with webpack.

Before

{
    target: "node",
    externals: [
        nodeExternals({
            whitelist: "webpack/hot/poll?1000"
        })
    ]
    // ... Other config for webpack
}

After

{
    target: "node",
    externals: [
        nodeExternals({
            whitelist: "webpack/hot/poll?1000",
            modulesDir: path.resolve(__dirname, "../../node_modules")
        })
    ]
    // ... Other config for webpack
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Cannot use GraphQLObjectType "__Directive" from ...
Error: Cannot use GraphQLObjectType "__Directive" from another module or realm . Ensure that there is only one instance of "graphql" in the ...
Read more >
Cannot use GraphQLObjectType "__Directive" from another ...
I get this error: ✖ Cannot use GraphQLObjectType "__Directive" from another module or realm. Ensure that there is only one instance of "graphql ......
Read more >
Error Cannot use GraphQLScalarType [*Type*] from another ...
I am trying to use apollo:codegen to generate schema and interfaces. I go through everything I found on the net, but with no...
Read more >
Duplicate graphql error - Strapi Community Forum
I get this error: Error: Cannot use GraphQLInputObjectType “FileInfoInput” from another module or realm. Ensure that there is only one instance ...
Read more >
type-graphql/Lobby - Gitter
node_modules/graphql/type/definition.js:551:18) ag:s at GraphQLObjectType. ... -like-cannot-use-graphqlschema-object-object-from-another-module-or-realm-how ...
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