File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change 94
94
"title" : " Integrating with an API Backend" ,
95
95
"sidebar_label" : " Integrating with an API"
96
96
},
97
+ "loading-graphql-files" : {
98
+ "title" : " Loading .graphql Files" ,
99
+ "sidebar_label" : " Loading .graphql Files"
100
+ },
97
101
"making-a-progressive-web-app" : {
98
102
"title" : " Making a Progressive Web App"
99
103
},
Original file line number Diff line number Diff line change 20
20
" adding-a-sass-stylesheet" ,
21
21
" post-processing-css" ,
22
22
" adding-images-fonts-and-files" ,
23
+ " loading-graphql-files" ,
23
24
" using-the-public-folder" ,
24
25
" code-splitting"
25
26
],
You can’t perform that action at this time.
0 commit comments