1

What's the correct way to format a response, and how would I go about it?

In example I see online, the response data is never nested inside a key with the mutation name

Here's the request

mutation CreateUser($input: CreateUserInput!) {
  createUser(input: $input) {
    id
    name
  }
}

Which of these is the correct response format for GraphQL?

This is what mine looks like now.

${"data" => %{
  "createUser" => %{
    "id" => 1,
    "name" => "Bob",
  }
}}

This is what I see in some example online (and I think it looks a lot cleaner)

${"data" => %{
  "id" => 1,
  "name" => "Bob",
}}

Which of these is more idiomatic GraphQL? And if it's the second one, is there a way to get Absinthe to format the response this way?

Peter R
  • 3,185
  • 23
  • 43

1 Answers1

0

The first is the correct way. I'm not finding anywhere in the spec that explicitly says 2 is bad, but every example they give is the first format.

If I had to speculate it's that way because you can query multiple fields, and in that case it would be ugly (and not always possible) to merge the responses.

They do say, however, you can alias the field as whatever you want: https://graphql.github.io/graphql-spec/July2015/#sec-Field-Alias

Brett Beatty
  • 5,690
  • 1
  • 23
  • 37