Skip to content

Commit d42f888

Browse files
petetntamyrlam
authored andcommitted
Document .graphql and .gql file loading with graphql.macro (#5481)
1 parent 9514cb8 commit d42f888

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
id: loading-graphql-files
3+
title: Loading .graphql Files
4+
sidebar_label: Loading .graphql Files
5+
---
6+
7+
To load `.gql` and `.graphql` files, first install the [`graphql.macro`](https://www.npmjs.com/package/graphql.macro) package by running:
8+
9+
```sh
10+
npm install --save graphql.macro
11+
```
12+
13+
Alternatively you may use `yarn`:
14+
15+
```sh
16+
yarn add graphql.macro
17+
```
18+
19+
Then, whenever you want to load `.gql` or `.graphql` files, import the `loader` from the macro package:
20+
21+
```js
22+
import { loader } from 'graphql.macro';
23+
24+
const query = loader('./foo.graphql');
25+
```
26+
27+
And your results get automatically inlined! This means that if the file above, `foo.graphql`, contains the following:
28+
29+
```graphql
30+
gql`
31+
query {
32+
hello {
33+
world
34+
}
35+
}
36+
`;
37+
```
38+
39+
The previous example turns into:
40+
41+
```javascript
42+
const query = {
43+
'kind': 'Document',
44+
'definitions': [{
45+
...
46+
}],
47+
'loc': {
48+
...
49+
'source': {
50+
'body': '\\\\n query {\\\\n hello {\\\\n world\\\\n }\\\\n }\\\\n',
51+
'name': 'GraphQL request',
52+
...
53+
}
54+
}
55+
};
56+
```
57+
58+
You can also use the `gql` template tag the same way you would use the non-macro version from `graphql-tag` package with the added benefit of inlined parsing results.
59+
60+
```js
61+
import { gql } from 'graphql.macro';
62+
63+
const query = gql`
64+
query User {
65+
user(id: 5) {
66+
lastName
67+
...UserEntry1
68+
}
69+
}
70+
`;
71+
```

docusaurus/website/i18n/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
"title": "Integrating with an API Backend",
9595
"sidebar_label": "Integrating with an API"
9696
},
97+
"loading-graphql-files": {
98+
"title": "Loading .graphql Files",
99+
"sidebar_label": "Loading .graphql Files"
100+
},
97101
"making-a-progressive-web-app": {
98102
"title": "Making a Progressive Web App"
99103
},

docusaurus/website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"adding-a-sass-stylesheet",
2121
"post-processing-css",
2222
"adding-images-fonts-and-files",
23+
"loading-graphql-files",
2324
"using-the-public-folder",
2425
"code-splitting"
2526
],

0 commit comments

Comments
 (0)