Tom Quarendon
Tom Quarendon

Reputation: 5718

Can a graphql query or mutation return a scalar value

Syntactically you can define a query or a mutation in the schema such that it returns a type. However, an operation definition (i.e a query or mutation invoked by a client) has to have a SelectionSet, so I have to do:

mutation X { field }

So the result of my mutation or query has to be an object with fields, it can't be a scalar. Is this right? It feels like I ought to be able to just return a scalar. The result is always wrapped in an envelope when sending across HTTP, so the result would be valid JSON either way (a simple scalar isn't strictly valid JSON).

Is my reading correct?

Upvotes: 25

Views: 30090

Answers (1)

lipp
lipp

Reputation: 5936

You can actually return a scalar, like Boolean or String

type Mutation {
  hello(who: String!): String
}

The issuing this query

mutation foo {
  hello("peter")
}

result would look like this

data.hello // string

Tested this with graphql-yoga + graphql-playground:

schema from playground result from playground

Upvotes: 50

Related Questions