How do I work with GraphQL?
  • 28 Jun 2022
  • 1 Minute to read
  • Contributors
  • Dark
    Light

How do I work with GraphQL?

  • Dark
    Light

First steps for using GraphQL.

Query, mutation, delete

A GraphQL requests starts with query (in REST: GET), mutation (POST, PUT) or delete (DELETE). In the examples, we focus on queries — we only read data and do not change it.

You can use the web interface GraphiQL for this exercise.

Name and type of the request

"query" is followed by a self-chosen name which has to start with a letter. The name is not important.

After the self-chosen name of the query ("q1"), add curly braces. Press CTRL + SPACE to get a list of possible queries, select "productsearch" or type "productsearch" directly.

query q1 { productsearch }

Filter criteria

Add round brackets. Within, we define filter criteria. Round brackets are used in GraphQL to restrict returned results. In this example, we filter products by the search term "gkfi". The query is not yet finished.

query q1 { productsearch(query:"gkfi") }

Result

With GraphQL you can request exactly the information you need. You can request hierarchical data structures and filter within a result.

Add curly brackets after the round brackets.

  • content: The actual result
  • pageInfo: Total number of results and hasNextPage (for pagination)
  • facets: Aggregation of results (for frontend filters for example)

We use "content" and work our way down to "descriptionShort".

query q1 {
  productsearch(query: "gkfi") {
    content {
      productDetails {
        descriptions {
          descriptionShort
        }
      }
    }
  }
}

The query is now correct and can be formatted using the "Prettify" Button.