未执行Apollo GraphQL解析器,返回null
我对Apollo GraphQL有问题。考虑以下代码:
import { ApolloServer } from 'apollo-server';
import path from 'path';
import { loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
...
const schema = loadSchemaSync(path.join(__dirname, './schema/*.graphql'), {
loaders: [
new GraphQLFileLoader()
]
})
const resolvers: Resolvers = {
Query: {
taskResults: (root, args, context) => {
console.log('taskResults')
return []
}
}
}
;(async () => {
const server = new ApolloServer({
schema,
resolvers,
csrfPrevention: true
});
const { url } = await server.listen()
console.log(`
I'm having an issue with Apollo GraphQL. Consider the following code:
import { ApolloServer } from 'apollo-server';
import path from 'path';
import { loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
...
const schema = loadSchemaSync(path.join(__dirname, './schema/*.graphql'), {
loaders: [
new GraphQLFileLoader()
]
})
const resolvers: Resolvers = {
Query: {
taskResults: (root, args, context) => {
console.log('taskResults')
return []
}
}
}
;(async () => {
const server = new ApolloServer({
schema,
resolvers,
csrfPrevention: true
});
const { url } = await server.listen()
console.log(`???? Server ready at ${url}`);
})().catch(console.error)
When I execute this query:
query {
taskResults {
id
}
}
It returns (actual)
{
"data": {
"taskResults": null
}
}
instead of (expected)
{
"data": {
"taskResults": []
}
}
Also, it does not print "taskResults" to the console, which means that the resolver is not actually executed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,我将
loadschemasync()
的结果分配给schema
而不是typedefs
:The problem was that I was assigning the result of
loadSchemaSync()
toschema
and nottypeDefs
: