未执行Apollo GraphQL解析器,返回null

发布于 2025-01-30 15:33:08 字数 692 浏览 2 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-02-06 15:33:08

问题在于,我将loadschemasync()的结果分配给schema 而不是typedefs

const typeDefs = loadSchemaSync(path.join(__dirname, './schema/*.graphql'), { 
  loaders: [
      new GraphQLFileLoader()
  ]
})

...

const server = new ApolloServer({
  typeDefs,
  ...
})

The problem was that I was assigning the result of loadSchemaSync() to schema and not typeDefs:

const typeDefs = loadSchemaSync(path.join(__dirname, './schema/*.graphql'), { 
  loaders: [
      new GraphQLFileLoader()
  ]
})

...

const server = new ApolloServer({
  typeDefs,
  ...
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文