space 的 GraphQL 字段

GraphQL field with space

我正在尝试在非常旧的遗留代码上使用新的 GraphQL 服务器,其中列名有空格,例如:“Poke ball”

我一直在尝试 运行 这个查询:

query{{userItems{Poke ball}}}

得到这个:

extensions: {code: "GRAPHQL_VALIDATION_FAILED",…}
locations: [{line: 1, column: 12}]
message: "Cannot query field "Poke" on type "UserItems"."

我试过使用引号但没有成功,知道这是否受支持/解决方法吗?

谢谢。

GraphQL specification requires that names of things (fields, types, arguments, etc.) only contain letters, numbers and underscores. A field name cannot contain a space because spaces and other whitespace 用于分隔各个标记。换句话说,一个或多个空格或行 returns 用于表示,例如,一个字段的名称已终止,另一个已开始。

如果您的底层数据层正在返回包含空格的键的数据,您需要定义一个具有允许名称的字段(如 pokeball),然后为该字段编写一个解析器。例如:

const UserItems = new GraphQLObjectType({
  name: "UserItems",
  fields: () => ({
    pokeball: {
      type: Pokeball,
      resolve: (parent) => {
        // The parent value will be whatever the parent field resolved to.
        // We look for a property named "Poke ball" on that value and return it.
        return parent["Poke ball"];
      },
    },
    ...
  }),
});

或在模式中,执行此操作

directive @fetch(from : String!) on FIELD_DEFINITION

type Product {
    Pokeball : String @fetch(from:"Poke ball")
}