Loading .graphql Files

To load .gql and .graphql files, first install the graphql and graphql.macro packages by running:

  1. npm install --save graphql graphql.macro

Alternatively you may use yarn:

  1. yarn add graphql graphql.macro

Then, whenever you want to load .gql or .graphql files, import the loader from the macro package:

  1. import { loader } from 'graphql.macro';
  2. const query = loader('./foo.graphql');

And your results get automatically inlined! This means that if the file above, foo.graphql, contains the following:

  1. query {
  2. hello {
  3. world
  4. }
  5. }

The previous example turns into:

  1. const query = {
  2. 'kind': 'Document',
  3. 'definitions': [{
  4. ...
  5. }],
  6. 'loc': {
  7. ...
  8. 'source': {
  9. 'body': '\\\\n query {\\\\n hello {\\\\n world\\\\n }\\\\n }\\\\n',
  10. 'name': 'GraphQL request',
  11. ...
  12. }
  13. }
  14. };

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.

  1. import { gql } from 'graphql.macro';
  2. const query = gql`
  3. query User {
  4. user(id: 5) {
  5. lastName
  6. ...UserEntry1
  7. }
  8. }
  9. `;