From 817868ce78b52ca2379c678f983db899b1dd8e57 Mon Sep 17 00:00:00 2001 From: jlevycpa Date: Sun, 26 Jun 2016 02:22:47 -0400 Subject: [PATCH] Recommend importing gql literal into each file (#118) * Recommended that gql literals be imported in all files. * Fixed error and switched to new graphql-tag package --- docs/source/apollo-client/core.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/source/apollo-client/core.md b/docs/source/apollo-client/core.md index dd4a8f3d09f..834b1e464a9 100644 --- a/docs/source/apollo-client/core.md +++ b/docs/source/apollo-client/core.md @@ -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.