You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Cleaning up the README a bit
* Slight wording/formatting tweaks
Co-authored-by: Jonathan Felchlin <[email protected]>
Co-authored-by: hwillson <[email protected]>
This is a template literal tag you can use to concisely write a GraphQL query that is parsed into the standard GraphQL AST:
15
+
The `gql`template literal tag can be used to concisely write a GraphQL query that is parsed into a standard GraphQL AST. It is the recommended method for passing queries to [Apollo Client](https://github.com/apollographql/apollo-client). While it is primarily built for Apollo Client, it generates a generic GraphQL AST which can be used by any GraphQL client.
16
16
17
17
```js
18
18
importgqlfrom'graphql-tag';
@@ -25,34 +25,68 @@ const query = gql`
25
25
}
26
26
}
27
27
`
28
+
```
29
+
30
+
The above query now contains the following syntax tree.
31
+
32
+
```js
33
+
{
34
+
"kind":"Document",
35
+
"definitions": [
36
+
{
37
+
"kind":"OperationDefinition",
38
+
"operation":"query",
39
+
"name":null,
40
+
"variableDefinitions":null,
41
+
"directives": [],
42
+
"selectionSet": {
43
+
"kind":"SelectionSet",
44
+
"selections": [
45
+
{
46
+
"kind":"Field",
47
+
"alias":null,
48
+
"name": {
49
+
"kind":"Name",
50
+
"value":"user",
51
+
...
52
+
}
53
+
}
54
+
]
55
+
}
56
+
}
57
+
]
58
+
}
59
+
```
60
+
61
+
#### Fragments
62
+
63
+
The `gql` tag can also be used to define reusable fragments, which can easily be added to queries or other fragments.
64
+
65
+
```js
66
+
importgqlfrom'graphql-tag';
28
67
29
-
// query is now a GraphQL syntax tree object
30
-
console.log(query);
31
-
32
-
// {
33
-
// "kind": "Document",
34
-
// "definitions": [
35
-
// {
36
-
// "kind": "OperationDefinition",
37
-
// "operation": "query",
38
-
// "name": null,
39
-
// "variableDefinitions": null,
40
-
// "directives": [],
41
-
// "selectionSet": {
42
-
// "kind": "SelectionSet",
43
-
// "selections": [
44
-
// {
45
-
// "kind": "Field",
46
-
// "alias": null,
47
-
// "name": {
48
-
// "kind": "Name",
49
-
// "value": "user",
50
-
// ...
68
+
constuserFragment=gql`
69
+
fragmentUser_useronUser {
70
+
firstName
71
+
lastName
72
+
}
73
+
`
51
74
```
52
75
53
-
You can easily explore GraphQL ASTs on [astexplorer.net](https://astexplorer.net/#/drYr8X1rnP/1).
76
+
The above `userFragment` document can be embedded in another document using a template literal placeholder.
77
+
78
+
```js
79
+
constquery=gql`
80
+
{
81
+
user(id: 5) {
82
+
...User_user
83
+
}
84
+
}
85
+
${userFragment}
86
+
`
87
+
```
54
88
55
-
This package is the way to pass queries into [Apollo Client](https://github.com/apollographql/apollo-client). If you're building a GraphQL client, you can use it too!
89
+
**Note:**_While it may seem redundant to have to both embed the `userFragment` variable in the template literal **AND** spread the `...User_user` fragment in the graphQL selection set, this requirement makes static analysis by tools such as `eslint-plugin-graphql` possible._
56
90
57
91
#### Why use this?
58
92
@@ -64,78 +98,100 @@ That's where this package comes in - it lets you write your queries with [ES2015
64
98
65
99
This package only has one feature - it caches previous parse results in a simple dictionary. This means that if you call the tag on the same query multiple times, it doesn't waste time parsing it again. It also means you can use `===` to compare queries to check if they are identical.
66
100
67
-
### Babel preprocessing
68
101
69
-
GraphQL queries can be compiled at build time using [babel-plugin-graphql-tag](https://github.com/gajus/babel-plugin-graphql-tag). Pre-compiling queries decreases the script initialization time and reduces the bundle size by potentially removing the need for `graphql-tag` at runtime.
102
+
### Importing graphQL files
70
103
71
-
#### TypeScript
72
-
Try this custom transformer to pre-compile your GraphQL queries in TypeScript: [ts-transform-graphql-tag](https://github.com/firede/ts-transform-graphql-tag).
104
+
_To add support for importing `.graphql`/`.gql` files, see [Webpack loading and preprocessing](#webpack-loading-and-preprocessing) below._
73
105
74
-
#### React Native, Next.js
106
+
Given a file `MyQuery.graphql`
75
107
76
-
Additionally, in certain situations, preprocessing queries via the webpack loader is not possible. [babel-plugin-import-graphql](https://www.npmjs.com/package/babel-plugin-import-graphql) will allow one to import graphql files directly into your JavaScript by preprocessing GraphQL queries into ASTs at compile-time.
If you have configured [the webpack graphql-tag/loader](#webpack-loading-and-preprocessing), you can import modules containing graphQL queries. The imported value will be the pre-built AST.
81
115
82
-
classProductsPageextendsReact.Component {
116
+
```graphql
117
+
importMyQueryfrom 'query.graphql'
118
+
```
119
+
120
+
#### Importing queries by name
121
+
122
+
You can also import query and fragment documents by name.
123
+
124
+
```graphql
125
+
queryMyQuery1 {
126
+
...
127
+
}
128
+
129
+
queryMyQuery2 {
83
130
...
84
131
}
85
132
```
86
133
87
-
#### Create-React-App
134
+
And in your JavaScript:
88
135
89
-
`[email protected]` does support the ability to preprocess queries using [evenchange4/graphql.macro](https://github.com/evenchange4/graphql.macro).
136
+
```javascript
137
+
import { MyQuery1, MyQuery2 } from'query.graphql'
138
+
```
90
139
91
-
If you're using an older version of `create-react-app`, check out [react-app-rewire-inline-import-graphql-ast](https://www.npmjs.com/package/react-app-rewire-inline-import-graphql-ast) to preprocess queries without needing to eject.
140
+
### Preprocessing queries and fragments
92
141
93
-
### Webpack preprocessing with `graphql-tag/loader`
142
+
Preprocessing GraphQL queries and fragments into ASTs at build time can greatly improve load times.
94
143
95
-
This package also includes a [webpack loader](https://webpack.js.org/concepts/loaders). There are many benefits over this approach, which saves GraphQL ASTs processing time on client-side and enable queries to be separated from script over `.graphql` files.
144
+
#### Babel preprocessing
96
145
97
-
```js
98
-
loaders: [
99
-
{
100
-
test:/\.(graphql|gql)$/,
101
-
exclude:/node_modules/,
102
-
loader:'graphql-tag/loader'
103
-
}
104
-
]
105
-
```
146
+
GraphQL queries can be compiled at build time using [babel-plugin-graphql-tag](https://github.com/gajus/babel-plugin-graphql-tag). Pre-compiling queries decreases script initialization time and reduces bundle sizes by potentially removing the need for `graphql-tag` at runtime.
106
147
107
-
then:
148
+
#### TypeScript preprocessing
108
149
109
-
```js
110
-
importqueryfrom'./query.graphql';
150
+
Try this custom transformer to pre-compile your GraphQL queries in TypeScript: [ts-transform-graphql-tag](https://github.com/firede/ts-transform-graphql-tag).
111
151
112
-
console.log(query);
113
-
// {
114
-
// "kind": "Document",
115
-
// ...
116
-
```
152
+
#### React Native and Next.js preprocessing
117
153
118
-
Testing environments that don't support Webpack require additional configuration. For [Jest](https://facebook.github.io/jest/) use [jest-transform-graphql](https://github.com/remind101/jest-transform-graphql).
154
+
Preprocessing queries via the webpack loader is not always possible. [babel-plugin-import-graphql](https://www.npmjs.com/package/babel-plugin-import-graphql) supports importing graphql files directly into your JavaScript by preprocessing GraphQL queries into ASTs at compile-time.
119
155
120
-
#### Support for multiple operations
156
+
E.g.:
121
157
122
-
With the webpack loader, you can also import operations by name:
Using the included `graphql-tag/loader` it is possible to maintain query logic that is separate from the rest of your application logic. With the loader configured, imported graphQL files will be converted to AST during the webpack build process.
169
+
170
+
_**Example webpack configuration**_
171
+
172
+
```js
173
+
{
174
+
...
175
+
loaders: [
176
+
{
177
+
test:/\.(graphql|gql)$/,
178
+
exclude:/node_modules/,
179
+
loader:'graphql-tag/loader'
180
+
}
181
+
],
131
182
...
132
183
}
133
184
```
134
185
135
-
And in your JavaScript:
136
-
```javascript
137
-
import { MyQuery1, MyQuery2 } from'query.gql'
138
-
```
186
+
#### Create React App
187
+
188
+
Preprocessing GraphQL imports is supported in **create-react-app** >= v2 using [evenchange4/graphql.macro](https://github.com/evenchange4/graphql.macro).
189
+
190
+
For **create-react-app** < v2, you'll either need to eject or use [react-app-rewire-inline-import-graphql-ast](https://www.npmjs.com/package/react-app-rewire-inline-import-graphql-ast).
191
+
192
+
#### Testing
193
+
194
+
Testing environments that don't support Webpack require additional configuration. For [Jest](https://facebook.github.io/jest/) use [jest-transform-graphql](https://github.com/remind101/jest-transform-graphql).
139
195
140
196
### Warnings
141
197
@@ -152,13 +208,19 @@ disableFragmentWarnings()
152
208
This package exports an `experimentalFragmentVariables` flag that allows you to use experimental support for [parameterized fragments](https://github.com/facebook/graphql/issues/204).
0 commit comments