[英]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.