【问题标题】:GraphQL schema - is it possible to set constraints on a custom type fieldGraphQL 模式 - 是否可以在自定义类型字段上设置约束
【发布时间】:2020-09-19 23:06:09
【问题描述】:

这是我使用的 GraphQL Schema 的简化 sn-p。有 - 一个枚举,类型 Contributor 和类型 Review。

enum CONTRIBUTOR_TYPE {
  AUTHOR
  EDITOR
}

type Contributor {
  name: String
  role: CONTRIBUTOR_TYPE
}

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

我想描述的是,“作者”字段不仅仅是贡献者类型的数组,而是具有特定工作角色的贡献者。在伪代码中它看起来像

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

有没有办法在 GraphQL Schema 中描述这个约束?

【问题讨论】:

    标签: graphql graphql-schema


    【解决方案1】:

    您应该能够使用架构指令来描述/强制执行约束。对于 apollo 服务器,请查看 https://www.apollographql.com/docs/apollo-server/schema/creating-directives/,对于 graphql-java,请查看 https://www.graphql-java.com/documentation/v15/sdl-directives/,对于其他服务器实现,请查看其相应的文档。

    【讨论】:

    • 我们使用阿波罗服务器。我没有考虑为此寻找指令。谢谢。
    猜你喜欢
    • 2011-10-07
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2015-11-05
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多