GraphQL枚举类型是否自动解析其值? [英] Do GraphQL enum types resolve their values automatically?

查看:71
本文介绍了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中,枚举类型中的每个枚举值都可能具有关联的值.设置此值是可选的;它默认为值名称的字符串表示形式.也就是说,对于像这样的枚举:

 枚举ExampleEnum {OO酒吧} 

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

  const解析器= {询问: {例如:()=>11,//ExampleEnum类型的字段},ExampleEnum:{FOO:11酒吧:23},} 

完成此操作后,我们可以在解析器中返回定义的值,并将其序列化为适当的Enum值.注意:用作参数的枚举也是如此-如果将 FOO 作为参数传递,则解析器实际上将接收到 11 的值.

不提供任何值等效于:

  ExampleEnum:{FOO:"FOO",BAR:"BAR",} 

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

香草GraphQL.js怎么样?

如果您以编程方式定义架构,则还可以提供

枚举值的值.这与上面的示例等效:

  const ExampleEnumType = new GraphQLEnumType({名称:"ExampleEnum",值:{FOO:{值: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天全站免登陆