diff --git a/docusaurus/docs/loading-graphql-files.md b/docusaurus/docs/loading-graphql-files.md new file mode 100644 index 00000000000..423cbff3d3e --- /dev/null +++ b/docusaurus/docs/loading-graphql-files.md @@ -0,0 +1,71 @@ +--- +id: loading-graphql-files +title: Loading .graphql Files +sidebar_label: Loading .graphql Files +--- + +To load `.gql` and `.graphql` files, first install the [`graphql.macro`](https://www.npmjs.com/package/graphql.macro) package by running: + +```sh +npm install --save graphql.macro +``` + +Alternatively you may use `yarn`: + +```sh +yarn add graphql.macro +``` + +Then, whenever you want to load `.gql` or `.graphql` files, import the `loader` from the macro package: + +```js +import { loader } from 'graphql.macro'; + +const query = loader('./foo.graphql'); +``` + +And your results get automatically inlined! This means that if the file above, `foo.graphql`, contains the following: + +```graphql +gql` + query { + hello { + world + } + } +`; +``` + +The previous example turns into: + +```javascript +const query = { + 'kind': 'Document', + 'definitions': [{ + ... + }], + 'loc': { + ... + 'source': { + 'body': '\\\\n query {\\\\n hello {\\\\n world\\\\n }\\\\n }\\\\n', + 'name': 'GraphQL request', + ... + } + } +}; +``` + +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. + +```js +import { gql } from 'graphql.macro'; + +const query = gql` + query User { + user(id: 5) { + lastName + ...UserEntry1 + } + } +`; +``` diff --git a/docusaurus/website/i18n/en.json b/docusaurus/website/i18n/en.json index 5df3cc56344..bef5ea6dcae 100644 --- a/docusaurus/website/i18n/en.json +++ b/docusaurus/website/i18n/en.json @@ -94,6 +94,10 @@ "title": "Integrating with an API Backend", "sidebar_label": "Integrating with an API" }, + "loading-graphql-files": { + "title": "Loading .graphql Files", + "sidebar_label": "Loading .graphql Files" + }, "making-a-progressive-web-app": { "title": "Making a Progressive Web App" }, diff --git a/docusaurus/website/sidebars.json b/docusaurus/website/sidebars.json index 219c14e82ab..7817f45ae93 100644 --- a/docusaurus/website/sidebars.json +++ b/docusaurus/website/sidebars.json @@ -22,6 +22,7 @@ "adding-a-sass-stylesheet", "post-processing-css", "adding-images-fonts-and-files", + "loading-graphql-files", "using-the-public-folder", "code-splitting" ],