简体   繁体   English

GraphQL 模式 - 是否可以在自定义类型字段上设置约束

[英]GraphQL schema - is it possible to set constraints on a custom type field

Here is a simplified snippet out of a GraphQL Schema I work with.这是我使用的 GraphQL Schema 的简化片段。 There are - an enum, type Contributor and type Review.有 - 一个枚举,类型 Contributor 和类型 Review。

enum CONTRIBUTOR_TYPE {
  AUTHOR
  EDITOR
}

type Contributor {
  name: String
  role: CONTRIBUTOR_TYPE
}

type Article {
  text: String
  authors: [Contributor]
}

What I want to describe, is that the "authors" field is not just array of type Contributor, but Contributor with specific job role.我想描述的是,“作者”字段不仅仅是贡献者类型的数组,而是具有特定工作角色的贡献者。 In pseudocode it would look like在伪代码中它看起来像

 authors: [Contributor{type: CONTRIBUTOR_TYPE.AUTHOR}]!

Is there a way to describe this constraint in GraphQL Schema?有没有办法在 GraphQL Schema 中描述这个约束?

You should be able to use schema directives to describe / enforce the constraint.您应该能够使用模式指令来描述/强制执行约束。 For apollo server check out https://www.apollographql.com/docs/apollo-server/schema/creating-directives/ , for graphql-java check out https://www.graphql-java.com/documentation/v15/sdl-directives/ , for other server implementations check their corresponding documentation.对于 apollo 服务器,请查看https://www.apollographql.com/docs/apollo-server/schema/creating-directives/ ,对于 graphql-java,请查看https://www.graphql-java.com/documentation/v sdl-directives/ ,对于其他服务器实现,请检查其相应的文档。

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

相关问题 在 GraphQL 模式中创建类型时是否可以重命名字段? - Is it possible to rename a field when creating a type within a GraphQL schema? GraphQL-有条件地确定架构中字段的类型 - GraphQL- conditionally determine the type of a field in a schema Graphql:完全更改架构中的字段类型 - Graphql: completely change field type in schema 如何在 GraphQL 模式中使用“十进制”作为字段类型? - How to use 'Decimal' as a field type in GraphQL schema? 给定一组GraphQL变量类型,是否可以使用客户端架构为该集中的每种类型创建所有有效值的映射 - Given a set of GraphQL variable types, is it possible to use the client schema to create a map of all valid values for each type in the set Graphql 模式类型 - Graphql Schema Type GraphQL / Relay架构无法在“CreateLinkPayload”类型上查询字段“store” - GraphQL/Relay Schema Cannot query field “store” on type “CreateLinkPayload” Strapi - 如何使用自定义属性扩展 graphql 类型架构? - Strapi - How to extend graphql type schema with custom property? 如何在 GraphQL 模式指令(Node.js、graphql-tools)中获取字段类型 - How to get the field type in a GraphQL schema directive (Node.js, graphql-tools) 使用 Hasura graphql 模式时如何自定义 graphql-code-generator 生成字段的类型 - How to customize the type for a graphql-code-generator produced field when consuming a Hasura graphql schema
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM