-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
36353be
Target docusaurus docs instead of the old ones in the template/README.md
petetnt 47e6231
Update loading-graphql-files.md
petetnt 035993b
Update loading-graphql-files.md
petetnt 7162ffd
Update loading-graphql-files.md
petetnt 8a87f06
Fix package name
donovantc 04ead00
Fix another package name
donovantc 1062456
Update loading-graphql-files.md
amyrlam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thangraphql-tag.macro
hereThere was a problem hiding this comment.
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 thegql
and theloader
whilegraphql-tag
only has the tag.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😂