Skip to content

Commit 878f821

Browse files
KyleAMathewsjastack
authored andcommitted
Add docs page with overview of Gatsby's usage of GraphQL (gatsbyjs#3557)
* Add docs page with overview of Gatsby's usage of GraphQL * Add missing word
1 parent cb7c848 commit 878f821

File tree

5 files changed

+163
-55
lines changed

5 files changed

+163
-55
lines changed

docs/docs/add-404-page.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Add 404 Page"
2+
title: "Adding a 404 Page"
33
---
44

55
To create a 404 page create a page whose path matches the regex

docs/docs/building-with-components.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
title: Building with Components
33
---
44

5-
## Requirements
6-
75
To use Gatsby, you will need a basic understanding of React components.
86

97
The [official tutorial](https://reactjs.org/tutorial/tutorial.html)

docs/docs/querying-with-graphql.md

Lines changed: 155 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,181 @@
11
---
2-
title: Querying with GraphQL
2+
title: Querying data with GraphQL
33
---
44

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:
681

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.
8111

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:
11113

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+
```
15119

16-
Gatsby describes your data by creating GraphQL _schemas_ from your data sources.
120+
Gatsby will create a schema that looks something like:
17121

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+
```
20125

21-
# Why GraphQL?
126+
This makes it easy to pull data from anywhere and immediately start writing
127+
GraphQL queries against your data.
22128

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.
30132
31-
# Basic Terminology
133+
## Powerful data transformations
32134
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.
40136
41-
# Example queries
137+
### Formatting dates
42138
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:
44140

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+
```
47146

48-
...
147+
### Markdown
49148

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:
51150

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
53171
54172
* http://graphql.org/learn/
55-
* https://services.github.com/on-demand/graphql/
56-
* https://apis.guru/graphql-voyager/
57173
* https://www.howtographql.com/
58174
* https://reactjs.org/blog/2015/05/01/graphql-introduction.html
59-
* ...
175+
* https://services.github.com/on-demand/graphql/
60176
61-
## Advanced readings on GraphQL
177+
### Advanced readings on GraphQL
62178
63179
* [GraphQL specification](https://facebook.github.io/graphql/October2016/)
64180
* [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)

docs/tutorial/part-four/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ directly into our components**—in the shape and form we want.
5656

5757
## How Gatsby's data layer uses GraphQL to pull data into components
5858

59-
If you're familiar with the React world, there are many options for loading data
60-
into components. One of the most popular and robust of these is a technology
61-
called [GraphQL](http://graphql.org/).
59+
There are many options for loading data into React components. One of the most
60+
popular and powerful of these is a technology called
61+
[GraphQL](http://graphql.org/).
6262

63-
GraphQL was invented at Facebook to help product engineers pull needed data into
63+
GraphQL was invented at Facebook to help product engineers _pull_ needed data into
6464
components.
6565

6666
GraphQL is a **q**uery **l**anguage (the _QL_ part of its name). If you're
6767
familiar with SQL, it works in a very similar way. Using a special syntax, you describe
6868
the data you want in your component and then that data is given
6969
to you.
7070

71-
In Gatsby, GraphQL enables components to declare and receive the data they need.
71+
Gatsby uses GraphQL to enable components to declare the data they need.
7272

7373
## Our first GraphQL query
7474

www/src/pages/docs/doc-links.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
link: /docs/plugins/
1919
- title: PRPL Pattern
2020
link: /docs/prpl-pattern/
21-
- title: Querying with GraphQL*
21+
- title: Querying data with GraphQL
2222
link: /docs/querying-with-graphql/
2323
- title: Guides
2424
items:
25-
- title: Add 404 Page
25+
- title: Adding a 404 Page
2626
link: /docs/add-404-page/
2727
- title: Adding Images, Fonts, and Files
2828
link: /docs/adding-images-fonts-files/

0 commit comments

Comments
 (0)