【问题标题】:How to add default values to input arguments in graphql如何将默认值添加到graphql中的输入参数
【发布时间】:2023-03-20 10:39:02
【问题描述】:

我有这种输入类型,我想为其中一个字段添加一个默认值。我想在 ExampleInput 中的 value 字段中添加 0。

type ExampleType {
  value: Int
  another: String
}

type Mutation {
  example(input: ExampleInput): ExampleType
}

input ExampleInput {
  value: Int
  another: String
}

有什么想法吗?

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    它看起来像输入对象类型上的grammar allows default values,所以你可以声明

    input ExampleInput {
      value: Int = 0
      another: String
      isAvailable: Boolean = false
    }
    

    The spec 明确表示存在默认值并解释了它们的使用方式(“输入强制”下的第一个项目符号)。

    (是否有任何特定的工具支持这一点可能是可变的:graphql.org 有一个非正式版本的 IDL 在它实际出现在规范中之前很长一段时间,我觉得有些库还没有赶上尚未发布规范。)

    【讨论】:

    • 如何在手动类型声明中设置默认值?像 new GraphQLObjectType({ args:{ isAdmin:{type:GraphQLBoolean} } }) 如何将 isAdmin 默认设置为 False?
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2017-01-07
    • 2013-09-29
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多