【问题标题】:Apollo GraphQL tutorial: "GraphQLError: Cannot query field \"id\" on type \"LaunchConnection\"."Apollo GraphQL 教程:“GraphQLError:无法查询类型 \"LaunchConnection\" 上的字段 \"id\"。”
【发布时间】:2020-03-17 19:51:08
【问题描述】:

我在这一步尝试按照 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,我在运行npm install 之前首先删除了package-lock.json,因为我遇到了sqlite3 依赖项的问题。

但是,在localhost:4000 的 GraphQL 操场上,如果我尝试运行查询

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]!
}

【问题讨论】:

    标签: graphql apollo


    【解决方案1】:

    查看您提供的架构,查询launches 返回类型LaunchConnection

    type LaunchConnection {
      cursor: String!
      hasMore: Boolean!
      launches: [Launch]!
    }
    
    type Query {
      launches: LaunchConnection!
    }
    

    您在下面的查询应返回类型LaunchConnection

    query GetLaunches {
      launches {
        id
        mission {
          name
        }
      }
    }
    

    但是LaunchConnection 有字段cursorhasMorelaunches。您要求输入不正确类型的字段 idmission。您应该首先深入了解LaunchConnection 上的launches 字段,然后您可以询问Launch 类型上的字段。您的查询应如下所示:

    query GetLaunches {
        launches {
            launches {
                id
                mission {
                    name
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 2018-08-30
      • 2021-11-13
      • 2020-07-27
      • 2019-01-17
      • 1970-01-01
      • 2016-12-05
      • 2019-10-09
      • 2018-06-13
      相关资源
      最近更新 更多