【问题标题】:GraphQL Java - MutationGraphQL Java - 变异
【发布时间】:2021-01-04 08:50:43
【问题描述】:

我正在尝试设置一个突变,我在我的 SDL 架构中使用输入类型

schema {
query: QueryType
mutation: MutationType
}

....

type Currency {
id: ID!,
name: String
code: String
...
}

type MutationType {
createCurrency(input: CurrencyInput): Currency
}

input currencyInput {
name: String!
code: String!
}

我通过的查询是

{
"query": "mutation { createCurrency(input: $input) { id name code } }",
"variables": { "input": {"name": "NewCurrency", "code": "NCY"} }
}

我正在传递查询和变量

Map<String, Object> variables = new HashMap<String, Object>();

....

// Data is a com.fasterxml.jackson.databind.JsonNode
variables.put("input", data.get("variables").get("input"));

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
                .query(query)
                .variables(variables)
                .build();

但是,我不断收到验证错误

{message=Validation error of type UndefinedVariable: Undefined variable input @ 'createCurrency', locations=[{line=1, column=34}], extensions={classification=ValidationError}}

我是否以错误的格式传递变量? 任何帮助将非常感激! :)

【问题讨论】:

  • 错误的查询/变异语法...graphql.org/learn/queries/#variables
  • @xadm 你有他们的例子的 SDL 模式的例子吗?或者你能具体说明什么问题吗? :)
  • $input: inputType ... input: $input - 两个“步骤”
  • @xadm 所以基本上我需要像这样构建我的查询? “查询”:突变 { createCurrency($input currencyInput) { createCurrency(input $input) { id name code } } }
  • 几乎,关闭,不必要的括号...阅读文档,在 graphiql/playground 中尝试,它会提示正确的语法并突出显示错误

标签: java graphql


【解决方案1】:

我的请求查询不正确。 这是一个带有变量的正确变异查询示例

{
    "query": "mutation($input: CurrencyInput) { currency(input: $input) { id name code } }",
    "variables": {
        "input": {
            "name": "newName",
            "code": "NCY"
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 2019-03-22
    • 2019-07-15
    • 2016-12-02
    • 2018-05-31
    • 2020-01-21
    • 2021-01-19
    • 2017-08-24
    • 2018-11-04
    相关资源
    最近更新 更多