Skip to content

Commit

Permalink
Recommend importing gql literal into each file (#118)
Browse files Browse the repository at this point in the history
* Recommended that gql literals be imported in all files.

* Fixed error and switched to new graphql-tag package
  • Loading branch information
jlevycpa authored and Sashko Stubailo committed Jun 26, 2016
1 parent 21f3740 commit 817868c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions docs/source/apollo-client/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ const query = gql`
`
```

There are two ways to import and use the `gql` tag. You can either register it globally:
The `gql` tag is a function under the hood, therefore it must be imported wherever it is used:

```js
// Register it globally
import { registerGqlTag } from 'apollo-client/gql';
registerGqlTag();
import gql from 'graphql-tag';

// Now, in any part of your app you can use the gql tag
const query = gql`...`;
```

Alternatively, you can import it in every file if you want to be more explicit:
Alternatively, if you prefer *not* to import the `gql` tag each time you use it, you can register it as a global:

```js
import gql from 'apollo-client/gql';
// In a browser
import gql from 'graphql-tag';
window['gql'] = gql;

// In node.js
import gql from 'graphql-tag';
global['gql'] = gql;

// Now, in any part of your app you can use the gql tag
const query = gql`...`;
```

**Note:** ES6 imports are hoisted, which may mean that client code using the `gql` tag gets evaluated before the registration of the global. To avoid race conditions, it's best to just import the tag into each file that uses it.

This template literal tag serves two functions:

1. It parses the query string.
Expand Down

0 comments on commit 817868c

Please sign in to comment.