graphql - Apollo GraphQL 教程:“GraphQLError:无法在类型 \"LaunchConnection\" 上查询字段 \"id\"。”
问题描述
我正在尝试在此步骤中遵循 Apollo GraphQL 教程,https: //www.apollographql.com/docs/tutorial/resolvers/#run-queries-in-the-playground 。在https://github.com/apollographql/fullstack-tutorial之后,我跑了
cd final/server && npm i && npm start
也
cd final/client && npm i && npm start
(对于final/server
,我首先删除了package-lock.json
before runningnpm install
因为我遇到了sqlite3
依赖问题)。
但是,在 GraphQL 操场上localhost:4000
,如果我尝试运行查询
query GetLaunches {
launches {
id
mission {
name
}
}
}
我收到错误响应
{
"error": {
"errors": [
{
"message": "Cannot query field \"id\" on type \"LaunchConnection\".",
"locations": [
{
"line": 3,
"column": 5
}
],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED",
"exception": {
"stacktrace": [
"GraphQLError: Cannot query field \"id\" on type \"LaunchConnection\".",
" at Object.Field (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:64:31)",
" at Object.enter (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/language/visitor.js:334:29)",
" at Object.enter (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/language/visitor.js:385:25)",
" at visit (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/language/visitor.js:252:26)",
" at Object.validate (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/validation/validate.js:63:22)",
" at validate (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/apollo-server-core/dist/requestPipeline.js:211:32)",
" at Object.<anonymous> (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/apollo-server-core/dist/requestPipeline.js:124:42)",
" at Generator.next (<anonymous>)",
" at fulfilled (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/apollo-server-core/dist/requestPipeline.js:4:58)",
" at processTicksAndRejections (internal/process/task_queues.js:93:5)"
]
}
}
},
{
"message": "Cannot query field \"mission\" on type \"LaunchConnection\".",
"locations": [
{
"line": 4,
"column": 5
}
],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED",
"exception": {
"stacktrace": [
"GraphQLError: Cannot query field \"mission\" on type \"LaunchConnection\".",
" at Object.Field (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:64:31)",
" at Object.enter (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/language/visitor.js:334:29)",
" at Object.enter (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/language/visitor.js:385:25)",
" at visit (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/language/visitor.js:252:26)",
" at Object.validate (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/graphql/validation/validate.js:63:22)",
" at validate (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/apollo-server-core/dist/requestPipeline.js:211:32)",
" at Object.<anonymous> (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/apollo-server-core/dist/requestPipeline.js:124:42)",
" at Generator.next (<anonymous>)",
" at fulfilled (/Users/kurt/Documents/Scratch/fullstack-tutorial/final/server/node_modules/apollo-server-core/dist/requestPipeline.js:4:58)",
" at processTicksAndRejections (internal/process/task_queues.js:93:5)"
]
}
}
}
]
}
}
(见下面的截图)。
知道是什么原因造成的吗?我确实在右侧弹出窗口中看到了一个 GraphQL 模式,它似乎包含以下字段:
directive @cacheControl(
maxAge: Int
scope: CacheControlScope
) on FIELD_DEFINITION | OBJECT | INTERFACE
enum CacheControlScope {
PUBLIC
PRIVATE
}
type Launch {
id: ID!
site: String
mission: Mission
rocket: Rocket
isBooked: Boolean!
}
type LaunchConnection {
cursor: String!
hasMore: Boolean!
launches: [Launch]!
}
type Mission {
name: String
missionPatch(size: PatchSize): String
}
type Mutation {
bookTrips(launchIds: [ID]!): TripUpdateResponse!
cancelTrip(launchId: ID!): TripUpdateResponse!
login(email: String): String
}
enum PatchSize {
SMALL
LARGE
}
type Query {
launches(
pageSize: Int
after: String
): LaunchConnection!
launch(id: ID!): Launch
me: User
}
type Rocket {
id: ID!
name: String
type: String
}
type TripUpdateResponse {
success: Boolean!
message: String
launches: [Launch]
}
scalar Upload
type User {
id: ID!
email: String!
trips: [Launch]!
}
解决方案
查看您提供的架构,查询launches
返回类型LaunchConnection
type LaunchConnection {
cursor: String!
hasMore: Boolean!
launches: [Launch]!
}
type Query {
launches: LaunchConnection!
}
您的查询,如下,预计返回类型LaunchConnection
query GetLaunches {
launches {
id
mission {
name
}
}
}
但是LaunchConnection
有字段cursor
, hasMore
& launches
。您要求的字段id
和mission
类型不正确。您应该首先深入了解该launches
字段,LaunchConnection
然后您可以询问该Launch
类型的字段。您的查询应如下所示:
query GetLaunches {
launches {
launches {
id
mission {
name
}
}
}
}
推荐阅读
- javascript - 在聚合调用中限制字段属性的更好方法
- ms-access - TempVars - 获取 ID 以在下一个表单中使用
- javascript - Webpack 监视文件更改并触发其他文件的加载器
- php - 这个 PHP 伪 DI 模式是错误的/危险的吗?
- vue.js - v-select 与过滤剂量工作
- architecture - 从 N 层转换为 DDD
- kurento - Coturn 服务器不完整的日志
- javascript - 使用 JqGrid 分组时数据无法正确显示
- symfony - 在 Translator 扩展服务中获取 sf3 应用程序的全局应用程序容器
- php - 为什么 get_headers() 返回 400 Bad request,而 CLI curl 返回 200 OK?