graphql查询或变异是否可以返回标量值

Tom*_*don 7 graphql

从语法上讲,您可以在模式中定义查询或变异,以使其返回类型。但是,操作定义(即客户端调用的查询或变异)必须具有SelectionSet,所以我必须这样做:

mutation X { field }
Run Code Online (Sandbox Code Playgroud)

因此,我的变异或查询结果必须是具有字段的对象,不能是标量。这是正确的吗?感觉我应该能够只返回一个标量。通过HTTP发送时,结果始终包裹在一个信封中,因此,结果无论哪种方式都是有效的JSON(简单的标量并非严格有效的JSON)。

我的阅读正确吗?

lip*_*ipp 14

You can actually return a scalar, like Boolean or String

type Mutation {
  hello(who: String!): String
}
Run Code Online (Sandbox Code Playgroud)

The issuing this query

mutation foo {
  hello("peter")
}
Run Code Online (Sandbox Code Playgroud)

result would look like this

data.hello // string
Run Code Online (Sandbox Code Playgroud)

Tested this with graphql-yoga + graphql-playground:

游乐场中的架构 result from playground

  • 用什么?当我尝试时,GraphiQL 不喜欢这种语法,而且规范显然也不允许。我怀疑服务器实现确实允许这样做,但在规范中,操作的定义是后面跟着 SelectionSet 和 *not* SelectionSet(opt) (2认同)