Apollo GraphQL教程:“ GraphQLError:无法查询类型为“ LaunchConnection \”的字段“ id”。

时间:2019-11-21 19:26:37

标签: graphql apollo

在这一步,我尝试遵循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之前我先删除了npm install,因为我遇到了与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)"
            ]
          }
        }
      }
    ]
  }
}

(请参见下面的屏幕截图)。

enter image description here

是什么原因造成的?我确实在右侧弹出窗口中看到了一个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]!
}

1 个答案:

答案 0 :(得分: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。您应该首先进入launches上的LaunchConnection字段,然后才能询问Launch类型的字段。您的查询应如下所示:

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