I have this very persistent problem which is very strange to me. It actually happened in a larger project but I isolated it in a very minimal project setup and the error keeps happening, hopefully someone can help.
This is my setup: node v20.0.0 yarn 4.6.0
I am using graphql in Version 16.8.1 and am trying to run a very simple test file with vitest. The test file:
import { createYoga } from 'graphql-yoga'
import { describe, test, expect, beforeAll } from 'vitest'
import { buildSchema } from 'graphql'
describe('GraphQL endpoint', async () => {
const gqlSchema = buildSchema(`
type Query {
hello: String
}
`)
const yogaRepository = createYoga({
schema: gqlSchema,
landingPage: false,
graphqlEndpoint: '/graphql'
})
beforeAll(async () => {
})
test('should fetch events', async () => {
expect(true).toEqual(true)
})
})
Gives this output:
Error: Cannot use GraphQLSchema "{ __validationErrors: undefined, description: undefined, extensions: {}, astNode: undefined, extensionASTNodes: [], _queryType: Query, _mutationType: undefined, _subscriptionType: undefined, _directives: [@include, @skip, @deprecated, @specifiedBy, @oneOf], _typeMap: { Query: Query, String: String, Boolean: Boolean, __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation }, _subTypeMap: {}, _implementationsMap: {} }" 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.
❯ instanceOf node_modules/graphql/jsutils/instanceOf.js:48:19
❯ isSchema node_modules/graphql/type/schema.js:32:37
❯ useSchema node_modules/graphql-yoga/esm/plugins/use-schema.js:7:9
❯ new YogaServer node_modules/graphql-yoga/esm/server.js:113:34
❯ createYoga node_modules/graphql-yoga/esm/server.js:409:20
❯ gql.test.ts:12:28
10| `)
11|
12| const yogaRepository = createYoga({
| ^
13| schema: gqlSchema,
14| landingPage: false,
Please note that the test file is intentionally very simple (and the test useless) to isolate the problem. This is the content of my package.json:
{
"name": "api-clean-test",
"packageManager": "yarn@4.6.0",
"dependencies": {
"graphql": "^16.8.1",
"graphql-yoga": "^5.15.1",
"vitest": "^3.2.4"
},
"scripts": {
"test": "yarn vitest"
},
"resolutions": {
"graphql": "^16.0.0"
}
}
The result of yarn why graphql
which is recommended online a lot looks very normal to me:
yarn why graphql
└─ api-clean-test@workspace:.
└─ graphql@npm:16.11.0 (via npm:^16.0.0)
I installed everything from scratch and don't have any yarn links or something like this going on. I use the nodeLinker: node-modules
option.
Could anyone point me in the direction of the problem? I really don't know whats going on.
EDIT:
I narrowed it down a little bit. This only occurs in Vites Test files. When converting this file to a normal JS file and running it normally, this doesn't happen.
The problem is with Vitest + graphql. It is a known bug and a workaround was posted here:
https://github.com/vitest-dev/vitest/issues/4605
The workaround is to add this line to your vite config:
resolve: { alias: { graphql: "graphql/index.js" } },