GraphQL 枚举类型会自动解析它们的值吗? [英] Do GraphQL enum types resolve their values automatically?

查看:11
本文介绍了GraphQL 枚举类型会自动解析它们的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该期望枚举类型自动解析还是仅存在类型以限制选项?

给定以下 GraphQL 架构:

type Job {描述:字符串!状态:状态!}枚举状态{待审核PENDING_APPROVAL得到正式认可的}

以及如下所示的查询:

查询作业{描述地位}

如果我的数据库返回以下内容:

{ "description": "一些不相关的工作描述", "status": 1 }

我希望 GraphQL 返回:

{ "description": "一些不相关的工作描述", "status": "PENDING_APPROVAL" }

我是否设置错误,或者这种预期行为需要我为 status

编写解析器

const getQuestionStatus = ({ status }) =>['PENDING_REVIEW', 'PENDING_APPROVAL', 'APPROVED'][状态];

解决方案

在 GraphQL.js 中,枚举类型中的每个枚举值都可能有一个与之关联的值.设置此值是可选的;它默认为值名称的字符串表示形式.也就是说,对于像这样的枚举:

enum ExampleEnum {食品级酒吧}

默认情况下,FOO 的值为 "FOO".如果您使用 graphql-tools 构建架构(或使用 apollo-server,它在引擎盖下使用 graphql-tools`)我们可以直接在解析器中传递 Enum 类型的值:

const 解析器 = {询问: {示例:() =>11,//字段类型为 ExampleEnum},示例枚举:{福:11,酒吧:23,},}

一旦我们这样做了,我们就可以在我们的解析器中返回定义的值,它会被序列化为适当的 Enum 值.注意:用作参数的枚举也是如此——如果 FOO 作为参数传入,解析器实际上会收到 11 的值.>

不提供任何值等同于做:

ExampleEnum: {福:'福',酒吧:'酒吧',}

还值得注意的是,提供值对枚举值在响应中的显示方式没有影响.当执行结果被序列化为 JSON 时,枚举值总是显示为与枚举值的名称匹配的字符串值(即我们中的 "FOO""BAR"示例).

普通的 GraphQL.js 怎么样?

如果您以编程方式定义架构,也可以提供枚举值的值.下面是上述示例的等效项:

const ExampleEnumType = new GraphQLEnumType({name: 'ExampleEnum',值:{福:{值:11,},酒吧: {值:23,},},})

Should I expect enum types to resolve automatically or do the types only exist to limit options?

Given a GraphQL Schema of the following:

type Job {
  description: String!
  status: Status!
}

enum Status {
  PENDING_REVIEW
  PENDING_APPROVAL
  APPROVED
}

and a query that looks like:

query job {
  description
  status
}

If my database returned the following:

{ "description": "Some irrelevant job description", "status": 1 }

I would expect GraphQL to return:

{ "description": "Some irrelevant job description", "status": "PENDING_APPROVAL" }

Have I set something up incorrectly, or is this expected behaviour that will require me to write a resolver for status

const getQuestionStatus = ({ status }) => ['PENDING_REVIEW', 'PENDING_APPROVAL', 'APPROVED'][status];

解决方案

In GraphQL.js, each enum value in an Enum Type may have an associated value with it. Setting this value is optional; it defaults to the string representation of the value's name. That is, for an enum like:

enum ExampleEnum {
  FOO
  BAR
}

by default, the value for FOO will be "FOO". If you're using graphql-tools to build your schema (or using apollo-server, which uses graphql-tools under the hood`) we can pass in the values for an Enum type right in our resolvers:

const resolvers = {
  Query: {
    example: () => 11, // field with ExampleEnum type
  },
  ExampleEnum: {
    FOO: 11,
    BAR: 23,
  },
}

Once we've done that, we can return the defined value in our resolver and it will be serialized into the appropriate Enum value. Note: the same is true of enums that are used as arguments -- if FOO is passed in as an argument, the resolver will actually receive a value of 11.

Not providing any values is equivalent to doing:

ExampleEnum: {
  FOO: 'FOO',
  BAR: 'BAR',
}

It's also worthwhile to note that providing values has no impact on how the enum values are shown in the response. When the execution result is serialized into JSON, enum values are always shown as string values that match the names of the enum values (i.e. "FOO" and "BAR" in our example).

What about vanilla GraphQL.js?

Values for enum values can also be provided if you define your schema programatically. Here's an equivalent of the above example:

const ExampleEnumType = new GraphQLEnumType({
  name: 'ExampleEnum',
  values: {
    FOO: {
      value: 11,
    },
    BAR: {
      value: 23,
    },
  },
})

这篇关于GraphQL 枚举类型会自动解析它们的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆