Documentation

TutorialHow-To GuidesReferenceConceptual Guides

v3 Docsv2 Docsv1 Docs
Sign Up for Free

GraphQL Query Options

Intro

This page will walk you through a series of GraphQL queries, each designed to demonstrate a particular feature of GraphQL. These examples will work on the real schema used on graphql-reference example. You can run this example locally to experiment and poke around the innards of the site! To get to the GraphiQL editor, go to localhost:8000/___graphql (that’s three underscores). You can also open the CodeSandbox version of the example site.

For more background information, read about why Gatsby uses GraphQL and how to use GraphiQL in any Gatsby site.

Basic queries

Example: Site metadata

Start with the basics, pulling up the site title from your gatsby-config.js’s siteMetadata.

In the GraphiQL editor, try editing the query to include the description from siteMetadata. When typing in the query editor you can use Ctrl + Space to see autocomplete options and Ctrl + Enter to run the current query.

Example: Multiple data nodes

Gatsby structures its content as collections of nodes, which are connected to each other with edges. In this query you ask for the total count of plugins in this Gatsby site, along with specific information about each one.

In the GraphiQL editor, try using the editor’s autocomplete (Ctrl + Space) to get extended details from the packageJson nodes.

If you’re using Gatsby version 2.2.0 or later, you can remove edges and node from your query and replace it with nodes. The query will still work and the returned object will reflect the nodes structure.

Field arguments

This section covers the different arguments you can pass in to GraphQL fields.

Limit

There are several ways to reduce the number of results from a query. Here totalCount tells you there’s 8 results, but limit is used to show only the first three.

Skip

Skip over a number of results. In this query skip is used to omit the first 3 results.

Filter

In this query, the filter argument and the ne (not equals) operator are used to show only results that have a title. You can find a good video tutorial on this in LevelUpTuts Gatsby Tutorial #9: Filters & Sorting with GraphQL.

Complete list of possible operators

Gatsby relies on Sift to enable MongoDB-like query syntax for object filtering. This allows Gatsby to support operators like eq, ne, in, regex and querying nested fields through the ___ connector.

In the code block below the list, there is an example query with a description of what the query does for each operator.

  • eq: short for equal, must match the given data exactly
  • ne: short for not equal, must be different from the given data
  • regex: short for regular expression, must match the given pattern
  • glob: short for global, allows to use wildcard * which acts as a placeholder for any non-empty string
  • in: short for in array, must be an element of the array
  • nin: short for not in array, must NOT be an element of the array
  • gt: short for greater than, must be greater than given value
  • gte: short for greater than or equal, must be greater than or equal to given value
  • lt: short for less than, must be less than given value
  • lte: short for less than or equal, must be less than or equal to given value
  • elemMatch: short for element match, this indicates that the field you are filtering will return an array of elements, on which you can apply a filter using the previous operators

If you want to understand more how these filters work, looking at the corresponding tests in the codebase could be very useful.

Filtering on multiple fields

It is also possible to filter on multiple fields by separating the individual filters by a comma (which works as an AND):

In this query the fields categories and title are filtered to find the book that belongs to the magical creatures category AND has Fantastic in its title.

Combining operators

You can also combine the mentioned operators. This query filters on /History/ for the regex operator, which would return Hogwarts: A History and History of Magic. Then the ne operator filters out History of Magic, so the final result contains only Hogwarts: A History.

Sort

The ordering of your results can be specified with sort. Here the results are sorted in ascending order of frontmatter’s date field.

Sorting on multiple fields

You can also sort on multiple fields but the sort keyword can only be used once. The second sort field gets evaluated when the first field (here: date) is identical. The results of the following query are sorted in ascending order of date and title field.

Children's Anthology of Monsters and Break with Banshee both have the same date (1992-01-02) but in the first query (only one sort field) the latter comes after the first. The additional sorting on the title puts Break with Banshee in the right order.

Sort order

By default, sort fields will be sorted in ascending order. Optionally, you can specify a sort order per field by providing an array of ASC (for ascending) or DESC (for descending) values. For example, to sort by frontmatter.date in ascending order, and additionally by frontmatter.title in descending order, you would use sort: { fields: [frontmatter___date, frontmatter___title], order: [ASC, DESC] }. Note that if you only provide a single sort order value, this will affect the first sort field only, the rest will be sorted in default ascending order.

Formatting

Dates

Dates can be formatted using the formatString function.

Gatsby relies on Moment.js to format the dates. This allows you to use any tokens in your string. See the Moment.js documentation for more tokens.

You can also pass in a locale to adapt the output to your language. The above query gives you the English output for the weekdays, this example outputs them in German.

Example: anotherDate(formatString: "dddd, MMMM Do YYYY, h:mm:ss a") # Sunday, August 5th 2018, 10:56:14 am

Dates also accept the fromNow and difference function. The former returns a string generated with Moment.js’ fromNow function, the latter returns the difference between the date and current time (using Moment.js’ difference function).

Excerpt

Excerpts accept three options: pruneLength, truncate, and format. format can be PLAIN or HTML.

Example: Sort, filter, limit & format together

This query combines sorting, filtering, limiting and formatting together.

Query variables

In addition to adding query arguments directly to query fields, GraphQL allows you to pass in “query variables”. These can be both simple scalar values as well as objects.

The query below is the same one as the previous example, but with the input arguments passed in as “query variables”.

To add variables to page component queries, pass these in the context object when creating pages.

Group

You can also group values on the basis of a field (like the title, date, or category) and get the field value, the total number of occurrences, and the edges.

The query below gets you all categories (fieldValue) applied to a book and the number of books (totalCount) a given category is applied to. In addition, you are grabbing the title of books in a given category. For example, the response for this query contains 3 books in the magical creatures category.

Fragments

Fragments are a way to save frequently used queries for reuse.

To create a fragment, define it in a query and export it as a named export from any file Gatsby is aware of. A fragment is available for use in any other GraphQL query, regardless of its location in the project.

Fragments are globally defined in a Gatsby project, so names have to be unique.

The query below defines a fragment to get the site title, and then uses the fragment to access this information.

Aliasing

Want to run two queries on the same datasource? You can do this by aliasing your queries. See the query below for an example:

When you use your data, you will be able to reference it using the alias instead of the root query name. In this example, that would be data.someEntries or data.someMoreEntries (instead of data.allMarkdownRemark).

The same works for fields inside a query. Take this example:

Instead of receiving title you’ll get header. This is especially useful when you want to display the same field in different ways as the date shows. You both get date and relativeDate from the same source.

Conditionals

GraphQL allows you to skip a piece of a query depending on variables. This is handy when you need to render some part of a page conditionally.

In the GraphiQL editor, try changing variable withDate in the example query below:

Use directive @include(if: $variable) to conditionally include a part of a query or @skip(if: $variable) to exclude it.

You can use those directives on any level of the query and even on fragments. Take a look at an advanced example:

Edit this page on GitHub
© 2022 Gatsby, Inc.