简体   繁体   English

GraphQL 字段带空格

[英]GraphQL field with space

I'm trying to use a new GraphQL server on a very old legacy code, where the column names have spaces, eg: "Poke ball"我正在尝试在非常旧的遗留代码上使用新的 GraphQL 服务器,其中列名有空格,例如:“Poke ball”

I've been trying to run this query:我一直在尝试运行此查询:

query{{userItems{Poke ball}}}

and got this:得到了这个:

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

I've tried to use quotes with no luck, any idea if this is supported / workaround?我尝试使用引号但没有运气,知道是否支持/解决方法吗?

Thanks.谢谢。

The GraphQL specification requires that names of things (fields, types, arguments, etc.) only contain letters, numbers and underscores. GraphQL 规范要求事物的名称(字段、类型、arguments 等)仅包含字母、数字和下划线。 A field name cannot contain a space because spaces and other whitespace are used to separate individual tokens.字段名称不能包含空格,因为空格和其他空格用于分隔各个标记。 In other words, one or more spaces or line returns are used to indicate that, for example, one field's name has terminated and another has begun.换句话说,一个或多个空格或换行符用于表示,例如,一个字段的名称已终止,而另一个字段的名称已开始。

If your underlying data layer is returning data with keys that contain spaces, you need to define a field with an allowed name (like pokeball ) and then write a resolver for that field.如果您的底层数据层正在返回包含空格键的数据,您需要定义一个具有允许名称的字段(如pokeball ),然后为该字段编写一个解析器。 For example:例如:

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"];
      },
    },
    ...
  }),
});

or in the schema, do this或在架构中,执行此操作

directive @fetch(from : String!) on FIELD_DEFINITION

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM