Skip to content

Commit 2b5b737

Browse files
author
Sashko Stubailo
authored
Merge pull request apollographql#3373 from detrohutt/patch-2
doc(recipes/babel): add inline-import-graphql-ast
2 parents 4244f4f + c84123d commit 2b5b737

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

docs/source/recipes/babel.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ To avoid this runtime overhead, you can precompile your queries created with `gr
99
1. Using [babel-plugin-graphql-tag](#using-babel-plugin-graphql-tag)
1010
2. Using [graphql-tag.macro](#using-graphql-tagmacro)
1111

12+
If you prefer to keep your GraphQL code in separate files (`.graphql` or `.gql`) you can use [babel-plugin-inline-import-graphql-ast](https://github.com/detrohutt/babel-plugin-inline-import-graphql-ast). This plugin still uses `graphql-tag` under the hood, but transparently. You simply `import` your operations/fragments as if each were an export from your GraphQL file. This carries the same precompilation benefits as the above approaches.
13+
1214
## Using babel-plugin-graphql-tag
1315

1416
This approach will allow you to use the `graphql-tag` library as usual, and when processing the files with this babel plugin, the calls to that library will be replaced by the precompiled result.
@@ -69,9 +71,52 @@ const query = gql`
6971
`;
7072
```
7173

74+
## Using babel-plugin-graphql-tag
75+
76+
Install the plugin in your dev dependencies:
77+
78+
```
79+
# with npm
80+
npm install --save-dev babel-plugin-inline-import-graphql-ast
81+
82+
# or with yarn
83+
yarn add --dev babel-plugin-inline-import-graphql-ast
84+
```
85+
86+
Then add the plugin in your `.babelrc` configuration file:
87+
88+
```
89+
{
90+
"plugins": [
91+
"inline-import-graphql-ast"
92+
]
93+
}
94+
```
95+
96+
Now any `import` statements importing from a GraphQL file type will return a ready-to-use GraphQL DocumentNode object.
97+
98+
```javascript
99+
import React, { Component } from 'react';
100+
import { graphql } from 'react-apollo';
101+
import myImportedQuery from './productsQuery.graphql';
102+
// or for files with multiple operations:
103+
// import { query1, query2 } from './queries.graphql';
104+
105+
class QueryingComponent extends Component {
106+
render() {
107+
if (this.props.data.loading) return <h3>Loading...</h3>;
108+
return <div>{`This is my data: ${this.props.data.queryName}`}</div>;
109+
}
110+
}
111+
112+
export default graphql(myImportedQuery)(QueryingComponent);
113+
```
114+
72115
## Fragments
73116

74-
Both approaches work with fragments. You can have fragments defined in a different call to `gql` (either in the same file or in a different one). You can then include them into the main query using interpolation, like this:
117+
All of these approaches support the use of fragments.
118+
119+
For the first two approaches, you can have fragments defined in a different call to `gql` (either in the same file or in a different one). You can then include them into the main query using interpolation, like this:
75120

76121
```js
77122
import gql from 'graphql-tag';
@@ -97,3 +142,5 @@ const query = gql`
97142
${fragments.hello}
98143
`;
99144
```
145+
146+
With `babel-plugin-inline-import-graphql-ast`, you can just include your fragment in your GraphQL file along-side whatever uses it, or even import it from a separate file using the GraphQL `#import` syntax. See the [README](https://github.com/detrohutt/babel-plugin-inline-import-graphql-ast) for more information.

0 commit comments

Comments
 (0)