Modern GraphQL examples with strings, compilers, and SDKs
Tags: graphql, queries, learn
Published: 2021-10-15 13:36:48

---
title: Modern GraphQL examples with strings, compilers, and SDKs
published: true
date: 2021-10-07 15:34:32 UTC
tags: GraphQL,Queries,Learn
canonical_url: https://hasura.io/blog/graphql-examples/
---

 ![Modern GraphQL examples with strings, compilers, and SDKs](https://hasura.io/blog/content/images/2021/10/graphql_examples.png)

Every few years or so it seems relevant to review the current state of GraphQL, see which trends have caught on with the industry, what the current best practices are, and make some speculation about what’s coming next. We recently reviewed [the current state of GraphQL directives](https://hasura.io/blog/graphql-directives-you-should-know-about/), which you can read about here. Today, we’ll have a refreshed (2021) look at the query syntax itself.

Are you new to GraphQL? This article will cover details on the current state of querying content with the GraphQL query syntax. [For an introduction to GraphQL, have a look at this article, here.](https://hasura.io/learn/graphql/intro-graphql/what-is-graphql/)

# Types of Operations

One of the confusions people face with GraphQL is “what is a query”. This confusion comes from the fact that a query is a query. The syntax that is sent to the server to fetch data is a query, but a query can have one of three operation types, query, mutation, or subscription.

If you’re looking for a more concise intro to GraphQL guide, have a look at our [core concepts guide](https://hasura.io/learn/graphql/intro-graphql/core-concepts/) from our learning resources.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh5.googleusercontent.com/2aYByFWAqsmaPURtmpvTsJjtn5ZuIkebwGGJCFn-XefkrQyVJYqcbpbKbLv18a9l5lNrSi2axt9oUdWDGqutFJ1hbfvKqqBal3dVJA8bYeg8uHlQnZyjFlc57m9eapnwvv-kp3l8=s0)

The name immediately following the operation type is the operation name. These are often used for by server-side tooling to know exactly which query was requesting that data since the query itself can have nearly any shape. Hasura Cloud uses these names to support features like [observability metrics](https://hasura.io/graphql/monitoring/), [caching](https://hasura.io/graphql/caching/), and [allow lists](https://hasura.io/graphql/security/).

To complicate matters, you’ll often see the query operation assumed by default because most GraphQL servers (as allowed by the specification) will assume a “query query type”. It’s an accepted query shorthand.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh5.googleusercontent.com/GPgYTcdfECFPhlz0_evIr5r9S63CMCb49jN1apJAzZOuTHMtVKAH4eOuLxAzX-TpH1nzKrQDkhrKQETOsfcGOwui9NOTm4ujpfmDvpUsEQsW5BB0GCwxjZUK9PJg4kxwqQEFY9dy=s0)

With that bit of confusion out of the way, let’s look at the remaining parts of thequery syntax.

# Parts of the query

A query, regardless of type, is composed of a handful of components to create the final document sent to the server.

## Fields

Fields, sometimes referred to as leaves of the query, are the actual pieces of data you are wanting to fetch from the server. As the GraphQL specification requires, the data responding from the server must return valid JSON, and so the fields are the keys to the JSON object.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh4.googleusercontent.com/PzrW_GGH0ACjNntrDtxSIdV1xeG3oQ0A8N7H1IA-fWiKCwx8k95HXxfBu5nq9YdrjY1Q8md4zX13dd_V1io-vWgkPqbohGVO2iUPGHESMDK-KCViJXevCl4tP5eZZHgWKFsWch7n=s0)

Some have argued that this type of readable syntax has led to the mass adoption of GraphQL. It’s considerably readable for anyone looking to understand what data is being requested for. It’s also very expressive for its simplicity. Have a look at what a relational join query looks like.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh4.googleusercontent.com/sbCcP_YHyZZPs1ABH8-ylyE_PL2PeZXEuyudc5GyCE3qfmLVoLXO_4Z8dL2rQYMkVsRmnUf_BGUBLOac9Zo7tZr9LRUMKzZjc3hS16A__LoXAEx8rmvjasmGTX6Z9tkxATQIsqM6=s0)

It’s very clear what we are requesting, and what we expect will return from the server.

## Aliases

A limitation to GraphQL fields, since it returns valid JSON, is that keys need to be unique, and so you can only request fields one time. But there are cases you may want to request the same field for a different reason. Or you may want to rename the key returning from the server. For that, we need to use Aliases.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh4.googleusercontent.com/Ruh2I7iuMPB6FI1H2xpGmwvHSeEP8yuWz1tsNtesCxqTcYqQj-IZ-nbrc5vWb9wjVCuj39HOyT4IN68nQhB07VLZIDhzcfIv8S_ZV-lZ43ejmmrKwErFF0WQGJgZIT3SV0GaQTwN=s0)

## Arguments

With a readable syntax, we can begin to see the benefits of GraphQL, but without the ability to pass in search parameters, we are missing a very important part of any query language! That’s where we get arguments. Arguments are defined as an Input Type, and allow us to make requests for subsets of our data. Every vendor implements arguments differently in terms of the accepted syntax. Some follow a mongo-inspired theme, others follow a more traditional SQL-like naming convention. Some frameworks and tooling like Relay requires a guaranteed set of arguments to support automatic pagination and other framework-specific features. For the purposes of our examples, I’ll be using the same syntax that Hasura uses, which means you can use these examples in your own queries.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh5.googleusercontent.com/9UnQmhB8rdjnpMcWXWj2Pu5tjXvu8gNye-x74_JKUzS_V1plYZYAsMTw0ABv8yntlYoBrc5qpKTGGlRDYymoCZXne47VAJMPSitliQDquYCTfWzFTXWdPM3lG-ceRrqA8vPrgNLc=s0)

When combined with aliases that we saw above, we can begin to see some interesting use cases.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh4.googleusercontent.com/cq1H5GI7WQzbREsJBY0IzFOtNVruM0ZwJG0VyoERDIurNaIySTIicfrnx88xvuw-CWNsgJL9iRLDewkKh_bPFRiT-zupclhabjFAj7ai_NH_Zs0s9vby2gDD7vWPxWj6IdoPIjoe=s0)

## Fragments

Looking at syntax like this, we can already see a problem arising. This is not DRY syntax! That’s where fragments let us define sub-selection of fields that we can re-use across our queries.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh5.googleusercontent.com/6PLChWEIc0FEj9eCR5kEQUc4S0HYiIV7_SIs6_TqgkbaDY_duii-5qFISTjvTEYBM_LPhGGu5JFjZ52wsBIi07hs-O3OihX6UPLKXEVHCZO_Plieh4t6Et7IzNWqUSuWugZPaYW9=s0)

Fragment syntax starts by declaring a fragment, providing a name for the fragment, and specifying which type the subset of fields will be executed on.

## Variables

We may want to declare a specific query format, but there may be some variable input from our users. For that, we can specify variable input syntax. Variables are declared with the `$` syntax followed by the variable name and a declaration of what type it is. Variables can represent any number of complex inputs that may be needed.

We can parameterize the above query by simply accepting the integer as a variable.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh3.googleusercontent.com/190no6K_5wnvtqnHHTROwv6f2Djebae55R7ib5PdP_-cC2Sdl3Ezvu3dnPA6hm_b4RDzmv8hxQXkjtsFW01DPrb4jdSiw7wcLzQqS6gKIUpBHVqphBbb5T3OT9XEreOPbVKxZDdE=s0)

Or we can provide a more complex input, replacing the entire filter statement that we generate from our application code.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh3.googleusercontent.com/c0C_1iK2uf5k2EwtxGviCjzffW6y2ke9KsQ039-i0jk0TVBCP07HKz6rq4c9sdzUPvv5vJW1IpWwiNiZCf166SxfsfNhwJOM23Am3eWEK5yKR-Uq_cLHCcMXkI9jlBiehfbzF2-A=s0)

The variables themselves are sent as a separate parameter in the request to the GraphQL endpoint. The final body payload represents the following shape:

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh3.googleusercontent.com/3WCXcJ03zCn29AZQ5c3j-czVr-J6H9bTP90Zs9istjA31N2M4QkiKyOUPpxw4RD-ly8Leh27s34Ax4wS-fcKT_UOD46eDkVzAB8CoIs15a-8oWcxcvIqP50G4es6-L8s5Uw4usMi=s0)

It’s important to indicate which of the input arguments are required per the schema definition. `$price: Integer` and `$price: Integer!` have very different implications for our resolving logic on the server! If you declare a variable in your query, you must provide, and conversely, if you provide a variable, you must use it, otherwise, the query will not validate in the query planning stage.

## Directives

Directives allow us to provide variable execution for our queries based on small syntactical changes. We wrote [about GraphQL directives](https://hasura.io/blog/graphql-directives-you-should-know-about/) at length in this article. An example of a directive in Hasura Cloud is providing a `cached` directive.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh3.googleusercontent.com/S5pPhuf7FgsXkPZGFSzYFzq2KS7SHRZ90wgD9s0P9RmAl58KiXi6hi70yB2_N3T2yLNYX_ebIS2msjRIRZqsxSK_FDwjQ4CucYr8amIkw9Hwazo0l4EWCnEWO44l-iiVZemwQ2Rh=s0)

# Ways to Query

All GraphQL servers accept two inputs in the body as we saw with variables above, the `query` and `variables` parameters. Variables can be of any valid JSON type, with all declared variables needing to be provided, and all provided variables needing to be declared. The query will always be a string type.

In the following sections, we’ll look at common examples of a compiled query, but there are a number of frameworks and tools now that provide helpers around generating queries into their final format. The days of string concatenated queries are long behind us!

## String Query

As mentioned above, all queries will finally be sent to the server in a string format. In languages that support string definitions with line breaks, it can be quite helpful for declaring our query.

![Modern GraphQL examples with strings, compilers, and SDKs](https://lh4.googleusercontent.com/VWOceFIU9WcecI5huo49wTbQy32qu5_iC1EH-WUyxjV2RKZYPWUkdoauhUbet_j7qtRidFU2w3ZkpByEchLhh-Hu5gERWN9D90s2Yvii3Hunw_MsNjrSf62bwPEzC99Zf1yI_vO_=s0)

Regardless of how the string comes together, below we’ll look at some common patterns for putting queries together.

### Examples of GraphQL Queries

#### Basic Query
<!--kg-card-begin: markdown-->
```
query GetItems {
  items {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
#### Query with Inputs
<!--kg-card-begin: markdown-->
```
query GetItems {
  items(where: {
    value: {
      _lt: 50
    }
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
#### Query with Variables
<!--kg-card-begin: markdown-->
```
query GetItems($value: Integer) {
  items(where: {
    value: {
      _lt: $value
    }
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
### Examples of GraphQL Mutations

#### Basic Mutation
<!--kg-card-begin: markdown-->
```
mutation CreateItem {
  insert_item_one(object:{
    name: "New Item",
    value: 50
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
#### Mutation with Variables

```
mutation CreateItem($item: item_insert_input!) {
  insert_item_one(object: $item) {
    name
    value
  }
}

```

### Examples of GraphQL Subscriptions

#### Basic Subscription
<!--kg-card-begin: markdown-->
```
subscription GetItems {
  items {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
#### Subscription with Inputs
<!--kg-card-begin: markdown-->
```
subscription GetItems {
  items(where: {
    value: {
      _lt: 50
    }
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
#### Subscription with Variables
<!--kg-card-begin: markdown-->
```
subscription GetItems($value: Integer) {
  items(where: {
    value: {
      _lt: $value
    }
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->
### Examples of Fragments, Aliases, and Directives

#### Fragment example

```
fragment ItemDetails on Item {
  name
  value
}

query GetItems {
  items {
    ...ItemDetails
  }
}
```

#### Alias Example
<!--kg-card-begin: markdown-->
```
fragment ItemDetails on Item {
  name
  value
}

query GetItems {
  set1: items { # <---
    ...ItemDetails
  }
  set2: items { # <---
    ...ItemDetails
  }
}

```
<!--kg-card-end: markdown-->
#### Directive Examples
<!--kg-card-begin: markdown-->
```
query GetItems @cached { # <---
  items(where: {
    value: {
      _lt: 50
    }
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->

[Try out more GraphQL queries here.](https://hasura.io/learn/graphql/intro-graphql/graphql-queries/)

## Fluent GraphQL

While all queries will end up as strings, the path to get there can be whatever you want! An entire series of clients have popped up under the umbrella of “Fluent GraphQL” - the ability to write queries as abstracted concepts. We’ve written a previous round-up on the [various providers here](https://hasura.io/blog/fluent-graphql-clients-how-to-write-queries-like-a-boss/). For the purposes of this example, we’ll use GraphQL Zeus which is the client we used for our [Haura Super App](https://hasura.io/reference-app/) which is a large reference architecture.

Whichever client you use, the pattern is fairly similar. You provide your endpoint to some form of CLI, and an SDK is generated that allows you to provide object-like query structures. The benefit of this approach is that you can use typical object manipulation such as spread operators, optional subsets, and more.

### Examples of GraphQL Queries with GraphQL Zeus

#### Basic Query
<!--kg-card-begin: markdown-->
```
Gql.query({
    items: [{}, {
        name: true,
        value: true
    }]
})

```
<!--kg-card-end: markdown-->
#### Query with Inputs
<!--kg-card-begin: markdown-->
```
Gql.query({
    items: [{
        where: {
            value: {
                _lt: 50
            }
        }
    }, {
        name: true,
        value: true
    }]
})

```
<!--kg-card-end: markdown-->
#### Query with Variables
<!--kg-card-begin: markdown-->
```
# import the $ helper from the generated client

Gql.query({
    items: [{
        where: {
            value: {
                _lt: $`value`
            }
        }
    }, {
        name: true,
        value: true
    }]
}, {
    value: 50
})

```
<!--kg-card-end: markdown-->
### Examples of GraphQL Mutations with GraphQL Zeus

#### Basic Mutation
<!--kg-card-begin: markdown-->
```
Gql.mutation({
    insert_item_one: [{
        object: {
            name: "New Items",
            value: 50
        }
    }, {
        name: true,
        value: true
    }]
})

```
<!--kg-card-end: markdown-->
#### Mutation with Variables
<!--kg-card-begin: markdown-->
```
# import the $ helper from the generated client

Gql.mutation({
    insert_item_one: [{
        object: $`item`
    }, {
        name: true,
        value: true
    }]
}, {
    item: {
        name: "New Item",
        value: 50,
    }
})

```
<!--kg-card-end: markdown-->
### Examples of GraphQL Subscriptions with GraphQL Zeus

#### Basic Subscription
<!--kg-card-begin: markdown-->
```
Gql.subscribtion({
    items: [{}, {
        name: true,
        value: true
    }]
})

```
<!--kg-card-end: markdown-->
#### Subscription with Inputs
<!--kg-card-begin: markdown-->
```
Gql.subscription({
    items: [{
        where: {
            value: {
                _lt: 50
            }
        }
    }, {
        name: true,
        value: true
    }]
})

```
<!--kg-card-end: markdown-->
#### Subscription with Variables
<!--kg-card-begin: markdown-->
```
# import the $ helper from the generated client

Gql.subscription({
    items: [{
        where: {
            value: {
                _lt: $`value`
            }
        }
    }, {
        name: true,
        value: true
    }]
}, {
    value: 50
})

```
<!--kg-card-end: markdown-->
### Examples of Fragments, Aliases, and Directives with GraphQL Zeus

#### Fragment example
<!--kg-card-begin: markdown-->
```
#js Example

const itemDetails = {
    name: true,
    value: true,
}

Gql.query({
    items: [{}, {
        ...itemDetails #They're simple objects!
    }]
})

```
<!--kg-card-end: markdown-->
#### Alias Example
<!--kg-card-begin: markdown-->
```
Gql.query({
    __alias: {
        set1: {
            items: [{}, {
                name: true,
                value: true
            }]
        },
        set2: {
            items: [{}, {
                name: true,
                value: true
            }]
        }
    }
})

```
<!--kg-card-end: markdown-->
#### Directive Examples

## GraphQL Examples with GraphQL Code Generator

GraphQL code generator is a tool provided by the Guild, a consortium of open source developers that provide open tooling for the GraphQL ecosystem. What sets GraphQL Code Generator apart is the ability to generate an SDK from GraphQL documents. Where you write your queries, even testing them in GraphiQL, but still benefit from the SDK-like behavior in your application. Paired with a tool like Hasura Cloud’s allow list, you get a locked-down API with an explicit handshake between your SDK and your API.

What this means, is that if we take a query such as:

<!--kg-card-begin: markdown-->
```
query getItems {
  items(where: {
    value: {
      _lt: 50
    }
  }) {
    name
    value
  }
}

```
<!--kg-card-end: markdown-->

And you import it into your project, you can access it using an SDK style accessor such as:

<!--kg-card-begin: markdown-->
```
const { items } = await sdk.getItems();

```
<!--kg-card-end: markdown-->

Everything is exactly the same as writing traditional style GraphQL queries, mutations, and subscriptions. You just get a wrapper with invocation and authentication built in.

## Summary

The GraphQL ecosystem is growing with newer and better tools being created every day. The community is vibrant and actively pushing the specification maintainers to expand what anyone thought was possible just a few years ago, making GraphQL, and learning how to query with GraphQL, a must have skillset for any developer today.