1

When I have a query like this:

{
  "query": "mutation { createInventory(name: $name, schema: $schema) }",
  "variables": {
    "name": "i1",
    "schema": "s"
  }
}

I get an error back: Variable '$name' is not defined.

I checked those variables were being extracted from JSON properly, and they were. To be sure, I tried explicitly passing in the name variable when I execute the query:

Executor.execute(
    GraphQLSchema.schema,
    query,
    ctx,
    variables = InputUnmarshaller.mapVars(Map("name" -> "foo"))
)

Why would Sangria not be recognizing these variables? Is there something wrong with how I'm naming them?

4

1 回答 1

3

您缺少 GraphQL 查询本身的“变量定义”部分。这是查询的一部分,指示它期望传入的变量类型。

所以这应该可以工作,只要我猜到了变量的正确类型:

{
  "query": "mutation myMutation($name: String, $schema: String) { createInventory(name: $name, schema: $schema) }",
  "variables": {
    "name": "i1",
    "schema": "s"
  }
}

在我的文章The Anatomy of a GraphQL Query中阅读更多内容。

于 2017-10-03T17:55:05.710 回答