GraphQL queries consist of one or more patterns which are matched against the big graph containing all the data on the server. The patterns describe a subgraph of the big graph and is used to search for the relevant data in the big graph.

A pattern is expressed in terms of the relationships between objects and the fields the objects contain. This containment can be defined recursively. Let’s have a look at an example of a pattern.

library {
  books {
    title
    id
  }
}

In the example, we see the pattern, which is used to search for a library object that contains books objects, which in turn contain both a title field and an id field. Angle brackets are used to express the containment. In front of the bracket is the name of the object (e.g. library or books), inside the brackets are the names of the fields of that object (e.g. books or title and id). Multi-level hierarchies can be built because a field can also be an object (e.g. books) which contains its own fields (e.g. title and id). When referring to several fields, they are each listed in a new line, without any separating commas or semicolons.

Speaking about semicolons: Note, that the GraphQL language is not JSON, even though it may look similar due to the use of angle brackets.

What is a GraphQL Pattern?
Tagged on:         

Matthias Biehl

As API strategist, Matthias helps clients discover their opportunities for innovation with APIs & ecosystems and turn them into actionable digital strategies. Based on his experience in leading large-scale API initiatives in both business and technology roles, he shares best practices and provides both strategic and practical guidance. He has stayed a techie at heart and at some point, got a Ph.D. Matthias publishes a blog at api-university.com, is the author of several books on APIs, and regularly speaks at technology conferences.