Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document .graphql and .gql file loading with graphql.macro #5481

Merged
merged 7 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docusaurus/docs/loading-graphql-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
id: loading-graphql-files
title: Loading .graphql Files
sidebar_label: Loading .graphql Files
---

To load `.gql` and `.graphql` files, first install the [`graphl-tag.macro`](https://www.npmjs.com/package/graphql-tag.macro) package by running

```sh
npm install --save graphl-tag.macro
```

Alternatively you may use `yarn`:

```sh
yarn add graphql-tag.macro

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant graphql.macro rather than graphql-tag.macro here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a ton @AndresRodH ! You are totally right, graphql.macro offers both the gql and the loader while graphql-tag only has the tag.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed this one! 😆yarn add graphql.macro

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaa 🤦‍♂️ maybe it's fixed now. I blame the GitHub editor and doing changes on the go 😂

```

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.
4 changes: 4 additions & 0 deletions docusaurus/website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions docusaurus/website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down