Skip to content

Instantly share code, notes, and snippets.

@m-allanson
Last active November 6, 2019 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-allanson/83c93d62c1044fac848d09986ad02262 to your computer and use it in GitHub Desktop.
Save m-allanson/83c93d62c1044fac848d09986ad02262 to your computer and use it in GitHub Desktop.

GraphQL works by using a "schema" to describe what data is available for querying.

A schema consists of a list of fields, and the "type" for each field. Each field must have a type. A type can be something like String or Int. (More complex custom types can be created too).

In Gatsby, "schema inference" is an automatic process where Gatsby examines your data, then works out the correct schema based on the data.

Here's some example data.

const myTitles = [
{
  "title": "A long long time ago",
  "rank": 3
},
{
  "title": "Mystery title",
  "rank": 1
},
{
  "title": "The longest title out of all the titles in this list",
  "rank": 3
}
]

Note that every "title" is a string, and every "rank" is a number. Gatsby's schema inference would create a schema like this:

title: String
rank: Int

which would let you write a query that looked like this:

allTitles{
  title
  rank
}

Once you start adding arguments to your queries, hopefully you can see how useful GraphQL can be:

allTitles(filter: {rank: {eq: 3}}) {
  title
  rank
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment