如何忽略未知的枚举值? [英] How to ignore unknown enum values?

查看:24
本文介绍了如何忽略未知的枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道忽略/丢弃 GraphQL/Apollo 服务器中未知枚举值的最佳方法是什么.

I'm wondering what would be the best way to ignore/discard the unknown enum values in GraphQL/Apollo server.

假设我的 GraphQL 模式定义了枚举数组enum Service { Supermarket, TicketSales }",它现在工作正常,但后来我使用的其他服务正在添加一些新值(例如 Playground),而我的客户只是不不支持它,我只想忽略它并无错误地返回支持的值.

Let's say my GraphQL schema defines array of enums "enum Service { Supermarket, TicketSales }" and it works fine now, but later on other service I'm using is adding some new values (e.g. Playground) and my client just doesn't support it and I would just like to ignore it and return the supported values without error.

在 GraphQL 中执行此操作的最佳方法是什么.我的第一个想法是制作指令,该指令将从架构中读取支持的值并忽略其他所有内容,但在谷歌搜索后我没有找到任何好的示例如何做到这一点.你能指点我的方向吗?

What would be the best way to do this in GraphQL. My first idea was to make directive that would read the supported values from schema and ignore everything else, but after googling around I didn't find any good examples how to do it. Can you point me a direction where to go about this?

推荐答案

在玩了几圈之后,我发现了一些可以用来解决我最初遇到的问题的东西,所以我会把它贴在这里,以防其他人有同样的疑问.

After playing around I found something that I can use to get around my original problem, so I will post it here in case somebody else is wondering the same thing.

所以我最初的问题简而言之,我从另一个服务接收了几种不同的可用服务"类型的字符串数组,我想将它们映射到枚举以获得更好的打字稿支持等.但问题是,如果我从另一个服务获取一些未知值,我的 graphql 将失败.

So my original problem was in short that I'm receiving several different "available services" kind of string arrays from another services and I was thinking to map them to enum for better typescript support etc. But the problem was that if I get some unknown value from another service, my graphql will fail.

所以我最初的想法是用指令来修复它,毕竟我已经开始工作了:

So my original idea was to fix it with directive which I after all got working:

# In schema
directive @mapUnknownTo(value: String) on ENUM

enum SomeAttribute @mapUnknownTo(value: "__UNKNOWN__") {
  SomeAttribute1
  AnotherAttribute
  SomethingElse
  __UNKNOWN__
}

指令实现是:

import { SchemaDirectiveVisitor } from 'graphql-tools';
import { GraphQLEnumType } from 'graphql';

export class MapUnknownToDirective extends SchemaDirectiveVisitor {
  visitEnum(type: GraphQLEnumType) {
    const { value = '__UNKNOWN__' } = this.args;
    const valueMap = type.getValues().reduce((map, v) => map.set(v.value, v.name), new Map<string, string>());
    type.serialize = (v: string): string => valueMap.get(v) || value;
  }
}

所以这会将所有未在架构中定义的值映射到某个自定义值中,这并不是我最初想要的,但至少它没有给出错误,所以没问题.

So this will map all the values not defined in schema into some custom value, which is not exactly what I originally wanted, but at least it's not giving an error, so it's okay-ish.

我仍然不能 100% 确定指令是否适用于这种情况,但至少这是一种可能的解决方案.

I'm still not 100% sure if directives are way to go on cases like this, but at least it's one possible solution.

这篇关于如何忽略未知的枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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