|
1 | 1 | --- |
2 | | -title: Querying with GraphQL |
| 2 | +title: Querying data with GraphQL |
3 | 3 | --- |
4 | 4 |
|
5 | | -> This page is a work in progress. |
| 5 | +There are many options for loading data into React components. One of the most |
| 6 | +popular and powerful of these is a technology called |
| 7 | +[GraphQL](http://graphql.org/). |
| 8 | + |
| 9 | +GraphQL was invented at Facebook to help product engineers _pull_ needed data into |
| 10 | +React components. |
| 11 | + |
| 12 | +GraphQL is a **q**uery **l**anguage (the _QL_ part of its name). If you're |
| 13 | +familiar with SQL, it works in a very similar way. Using a special syntax, you describe |
| 14 | +the data you want in your component and then that data is given |
| 15 | +to you. |
| 16 | + |
| 17 | +Gatsby uses GraphQL to enable [page and layout |
| 18 | +components](/docs/building-with-components/) to declare what data they and their |
| 19 | +sub-components need. Gatsby then handles making sure that data is available in |
| 20 | +the browser when needed by your components. |
| 21 | + |
| 22 | +## What does a GraphQL query look like? |
| 23 | + |
| 24 | +GraphQL lets you ask for the exact data you need. Queries look like JSON: |
| 25 | + |
| 26 | +```graphql |
| 27 | +{ |
| 28 | + site { |
| 29 | + siteMetadata { |
| 30 | + title |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Which returns: |
| 37 | + |
| 38 | +```json |
| 39 | +{ |
| 40 | + "site": { |
| 41 | + "siteMetadata": { |
| 42 | + "title": "A Gatsby site!" |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +A basic page component with a GraphQL query might look like this: |
| 49 | + |
| 50 | +```jsx |
| 51 | +import React from "react"; |
| 52 | + |
| 53 | +export default ({ data }) => ( |
| 54 | + <div> |
| 55 | + <h1>About {data.site.siteMetadata.title}</h1> |
| 56 | + <p>We're a very cool website you should return to often.</p> |
| 57 | + </div> |
| 58 | +); |
| 59 | +
|
| 60 | +export const query = graphql` |
| 61 | + query AboutQuery { |
| 62 | + site { |
| 63 | + siteMetadata { |
| 64 | + title |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +`; |
| 69 | +``` |
| 70 | +
|
| 71 | +The result of the query is automatically inserted into your React component |
| 72 | +on the `data` prop. GraphQL and Gatsby lets you ask for data and then |
| 73 | +immediately start using it. |
| 74 | +
|
| 75 | +## How to learn GraphQL |
| 76 | +
|
| 77 | +Gatsby might be the first time you've seen GraphQL! We hope you love it as much |
| 78 | +as we do and find it useful for all your projects. |
| 79 | + |
| 80 | +When starting out with GraphQL, we recommend the following two tutorials: |
6 | 81 |
|
7 | | -# What is GraphQL? |
| 82 | +* https://www.howtographql.com/ |
| 83 | +* http://graphql.org/learn/ |
| 84 | + |
| 85 | +[The official Gatsby tutorial](/tutorial/part-four/) also includes an introduction to using GraphQL specifically with Gatsby. |
| 86 | + |
| 87 | +## How does GraphQL and Gatsby work together? |
| 88 | + |
| 89 | +One of the great things about GraphQL is how flexible it is. People use GraphQL |
| 90 | +with [many different programming languages](http://graphql.org/code/) and for web and native apps. |
| 91 | + |
| 92 | +Most people using GraphQL run it on a server to respond live to requests for |
| 93 | +data from clients. You define a schema (a schema is a formal way of describing |
| 94 | +the shape of your data) for your GraphQL server and then your GraphQL resolvers |
| 95 | +retrieve data from databases and/or other APIs. |
| 96 | + |
| 97 | +Gatsby is unique that it uses GraphQL at _build-time_ and _not_ for live |
| 98 | +sites. This means you don't need to run additional services (e.g. a database |
| 99 | +and node.js service) to use GraphQL for production websites. |
| 100 | +
|
| 101 | +Gatsby is a great framework for building apps so it's possible and encouraged |
| 102 | +to pair Gatsby's native build-time GraphQL with GraphQL queries running against |
| 103 | +a live GraphQL server from the browser. |
| 104 | +
|
| 105 | +## Where does Gatsby's GraphQL schema come from? |
| 106 | + |
| 107 | +Most usages of GraphQL involve manually creating a GraphQL schema. |
| 108 | + |
| 109 | +With Gatsby, we instead use plugins which fetch data from different sources |
| 110 | +which we use to automatically _infer_ a GraphQL schema. |
8 | 111 |
|
9 | | -graphql.org describes it as "...a query language for APIs and a runtime for |
10 | | -fulfilling those queries with your existing data". |
| 112 | +If you give Gatsby data that looks like: |
11 | 113 |
|
12 | | -Gatsby uses GraphQL to create a consistent interface between your static site |
13 | | -and your data sources, where data sources can be anything from local markdown |
14 | | -files to a remote API. |
| 114 | +```json |
| 115 | +{ |
| 116 | + "title": "A long long time ago" |
| 117 | +} |
| 118 | +``` |
15 | 119 |
|
16 | | -Gatsby describes your data by creating GraphQL _schemas_ from your data sources. |
| 120 | +Gatsby will create a schema that looks something like: |
17 | 121 |
|
18 | | -GraphQL _queries_ can then be associated with your Gatsby pages, ensuring each |
19 | | -page receives exactly the data it needs. |
| 122 | +``` |
| 123 | +title: String |
| 124 | +``` |
20 | 125 |
|
21 | | -# Why GraphQL? |
| 126 | +This makes it easy to pull data from anywhere and immediately start writing |
| 127 | +GraphQL queries against your data. |
22 | 128 |
|
23 | | -* As Gatsby runs on both server (at build time) & client, need way to specify |
24 | | - which data is needed. |
25 | | -* This is a _build-time only_ use of GraphQL. You don't need to run a GraphQL |
26 | | - server in production. |
27 | | -* Convenient way to describe data requirements of component. |
28 | | -* Why query colocation rocks |
29 | | -* Uses the Relay Modern compiler behind the scenes |
| 129 | +This _can_ cause confusion though as some data sources allow you to define |
| 130 | +a schema but parts of that schema might still not be recreated in Gatsby if |
| 131 | +there's not yet any data added for that part of the schema. |
30 | 132 |
|
31 | | -# Basic Terminology |
| 133 | +## Powerful data transformations |
32 | 134 |
|
33 | | -* Types based on file type + way data can be transformed |
34 | | -* Connections |
35 | | -* Shallow intro to how data layer works e.g. source and transformer plugins. |
36 | | -* Compare to webpack loaders — like loaders except create schema that can then |
37 | | - be queried. |
38 | | -* Named queries? |
39 | | -* Using query parameters to manipulate results? |
| 135 | +GraphQL enables another unique feature of Gatsby — it lets you control data transformations with arguments to your queries. Some examples. |
40 | 136 |
|
41 | | -# Example queries |
| 137 | +### Formatting dates |
42 | 138 |
|
43 | | -Showing off sorting, filtering, picking fields, programmatic transformations |
| 139 | +People often store dates like "2018-01-05" but want to display the date in some other form like "January 5th, 2018". One way of doing this is to load a date formatting JavaScript library into the browser. With Gatsby's GraphQL layer you can instead do the formatting at query time like: |
44 | 140 |
|
45 | | -[Some example queries from Gatsby's tests](https://github.com/gatsbyjs/gatsby/blob/52e36b9994a86fc473cd2f966ab6b6f87ee8eedb/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js#L116-L432) - |
46 | | -look for \`template literal blocks\` with `allNode{}` in them. |
| 141 | +```graphql |
| 142 | +{ |
| 143 | + date(formatString: "MMMM Do, YYYY") |
| 144 | +} |
| 145 | +``` |
47 | 146 |
|
48 | | -... |
| 147 | +### Markdown |
49 | 148 |
|
50 | | -# Further reading |
| 149 | +Gatsby has _transformer_ plugins which can transform data from one form to another. A common example is markdown. If you install [`gatsby-transformer-remark`](/packages/gatsby-transformer-remark/) then in your queries, you can specify you want the transformed HTML version instead of markdown: |
51 | 150 |
|
52 | | -## Getting started with GraphQL |
| 151 | +```graphql |
| 152 | +markdownRemark { |
| 153 | + html |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### Images |
| 158 | + |
| 159 | +Gatsby has rich support for processing images. Responsive images are a big part of the modern web and typically involve creating 5+ sized thumbnails per photo. With Gatsby's [`gatsby-transformer-sharp`](/packages/gatsby-transformer-sharp/), you can _query_ your images for responsive versions. The query automatically creates all the needed responsive thumbnails and returns `src` and `srcSet` fields to add to your image element. |
| 160 | +
|
| 161 | +Combined with a special Gatsby image component, [gatsby-image](/packages/gatsby-image/), you have a very powerful set of primatives for building sites with images. |
| 162 | +
|
| 163 | +See also the following blog posts: |
| 164 | +
|
| 165 | +* [Making Website Building Fun](/blog/2017-10-16-making-website-building-fun/) |
| 166 | +* [Image Optimization Made Easy with Gatsby.js](https://medium.com/@kyle.robert.gill/ridiculously-easy-image-optimization-with-gatsby-js-59d48e15db6e) |
| 167 | +
|
| 168 | +## Further reading |
| 169 | +
|
| 170 | +### Getting started with GraphQL |
53 | 171 |
|
54 | 172 | * http://graphql.org/learn/ |
55 | | -* https://services.github.com/on-demand/graphql/ |
56 | | -* https://apis.guru/graphql-voyager/ |
57 | 173 | * https://www.howtographql.com/ |
58 | 174 | * https://reactjs.org/blog/2015/05/01/graphql-introduction.html |
59 | | -* ... |
| 175 | +* https://services.github.com/on-demand/graphql/ |
60 | 176 |
|
61 | | -## Advanced readings on GraphQL |
| 177 | +### Advanced readings on GraphQL |
62 | 178 |
|
63 | 179 | * [GraphQL specification](https://facebook.github.io/graphql/October2016/) |
64 | 180 | * [Interfaces and Unions](https://medium.com/the-graphqlhub/graphql-tour-interfaces-and-unions-7dd5be35de0d) |
65 | | -* [Gatsby uses the Relay Compiler](https://facebook.github.io/relay/docs/en/compiler-architecture.html) |
66 | | -* ... |
67 | | - |
68 | | -## TODO — create live GraphQL explorer for GatsbyJS.org |
69 | | - |
70 | | -* iFrame of graphiql instance for this site running on Heroku so people can run |
71 | | - live queries. |
| 181 | +* [Relay Compiler (which Gatsby uses to process queries)](https://facebook.github.io/relay/docs/en/compiler-architecture.html) |
0 commit comments