Fields

Fields is the attribute of an object, we can declare fields in query or mutation

For example given the following query:

{
  hero {
    name // Field
  }
}

We have our field here: name. As a result, GraphQL will returns the name of this hero object:

{
  "data": {
    "hero": {
      "name": "R2-D2"
    }
  }
}

We can also make subselection of fields if we want to go a bit deeper:

{
  hero {
    name
    # Queries can have comments!
    friends {
      name
    }
  }
}

Will results into

{
  "data": {
    "hero": {
      "name": "R2-D2",
      "friends": [
        {
          "name": "Luke Skywalker"
        },
        {
          "name": "Han Solo"
        },
        {
          "name": "Leia Organa"
        }
      ]
    }
  }
}

Note

  • For query, the fields of this object will be executed in parallel. Whereas in mutation, the fields will be executed in sequential
  • GraphQL essentially is a tool to filter out the fields of the given result of resolver