Unable to test custom scalars
devatina11yb opened this issue · 3 comments
Describe the bug
I am currently in the process of developing a few custom scalars for my project, I can manually test the API with GraphiQL but I get a weird bug while testing with vitest.
Here is the verbatim of the error message:
TypeError: Cannot read properties of undefined (reading 'resolve')
❯ isResolveOptions node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1857:28
❯ addObject node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1707:22
❯ mergeResolvers node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1683:21
❯ createResolvers node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1561:43
❯ Object.factory node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1949:40
❯ node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1250:57
❯ applicationFactory node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1250:40
❯ Module.createApplication node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs:1316:12
❯ src/index.ts:9:31
7| const yoga = createYoga({
8| graphqlEndpoint: "/",
9| plugins: [useGraphQLModules(createApplication({ modules: [scalarsModule] }))],
| ^
10| });
11|
To Reproduce
I have published a simplified version of my project in a public repository on GitHub so you can run it on StackBlitz using this URL: https://stackblitz.com/github/devatina11yb/graphql-modules.
To confirm that the API is actually working:
- Run
pnpm run dev
. - Run the query
now
in GraphiQL:
query {
now
}
- You will get a result looking like this:
{
"data": {
"now": 1684752065821
}
}
To reproduce the bug:
- Run
pnpm run test
. - You can see the error message in the terminal.
Additional context
It may be worth noting that I actually did find the culprit but I am still struggling to figure out what is going on. The function isScalarResolver
is returning false
while testing:
// node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs (line 1859)
function isScalarResolver(obj) {
return obj instanceof GraphQLScalarType;
}
So I added some logging to the function:
// node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs (line 1859)
function isScalarResolver(obj) {
console.log('obj:', obj, '\nisScalarResolver(obj):', obj instanceof GraphQLScalarType, '\n');
return obj instanceof GraphQLScalarType;
}
You can also alter the function in StackBlitz by following these steps:
- Run
open /home/projects/qgnikzovb.github/node_modules/.pnpm/graphql-modules@2.1.2_graphql@16.6.0/node_modules/graphql-modules/index.mjs
. - Go to line 1859.
- Add
console.log('obj:', obj, '\nisScalarResolver(obj):', obj instanceof GraphQLScalarType, '\n');
to the function.
Here is what is displayed in the terminal:
- For
pnpm run dev
:
obj: { now: [Function: now] }
isScalarResolver(obj): false
obj: GraphQLScalarType {
name: 'DateTime',
description: 'DateTime scalar type',
specifiedByURL: undefined,
serialize: [Function: serialize],
parseValue: [Function: parseValue],
parseLiteral: [Function: parseLiteral],
extensions: [Object: null prototype] {},
astNode: undefined,
extensionASTNodes: []
}
isScalarResolver(obj): true
obj: {
now: [Function: now] { [Symbol(metadata)]: { moduleId: 'scalars' } }
}
isScalarResolver(obj): false
obj: GraphQLScalarType {
name: 'DateTime',
description: 'DateTime scalar type',
specifiedByURL: undefined,
serialize: [Function: serialize] { [Symbol(metadata)]: { moduleId: 'scalars' } },
parseValue: [Function: parseValue] { [Symbol(metadata)]: { moduleId: 'scalars' } },
parseLiteral: [Function: parseLiteral] {
[Symbol(metadata)]: { moduleId: 'scalars' }
},
extensions: [Object: null prototype] {},
astNode: undefined,
extensionASTNodes: []
}
isScalarResolver(obj): true
- For
pnpm run test
:
obj: { now: [Function: now] }
isScalarResolver(obj): false
obj: GraphQLScalarType {
name: 'DateTime',
description: 'DateTime scalar type',
specifiedByURL: undefined,
serialize: [Function: serialize],
parseValue: [Function: parseValue],
parseLiteral: [Function: parseLiteral],
extensions: [Object: null prototype] {},
astNode: undefined,
extensionASTNodes: []
}
isScalarResolver(obj): false
Environment
- OS: Windows 11
graphql-modules
: 2.1.2- NodeJS: 20.2.0
This is actually related to the dual package hazard loading CJS and ESM packages from graphql-js
at the same time so this is not directly related to graphql-modules
. I am closing this issue here.
Hi @devatina11yb I was wondering if you found a solution for this as I'm also running into the same error and can't find much information about the issue.
I'm not completely sure why, but adding an alias for graphql
to my vitest config (and removing the vitest cache node_modules/.vitest
) appears to resolve the error.
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
graphql: 'graphql/index.js',
},
},
})