Babel plugin to make .gql/.graphql files importable
npm install babel-plugin-import-graphql



import syntax for .graphql and .gql files.
babel-plugin-inline-import-graphql-ast package name is deprecated. Please use babel-plugin-import-graphql (NPM) instead.
plugins array:
babel-plugin-inline-import-graphql-ast (or inline-import-graphql-ast) -> import-graphql.
package.json file
devDependencies:
babel-plugin-inline-import-graphql-ast -> babel-plugin-import-graphql.
babel-plugin-import-graphql is 2.4.4 so please make sure your version string matches that. For instance, "babel-plugin-import-graphql": "^2.0.0" is fine because of the caret.
2.4.4 or widen your version range to include it.
babel-core@^6.26.3 or @babel/core@^7.0.0-beta.40 (Lower betas may work but weren't tested)
graphql-tag@^2.1.0 (only if using the runtime option described below)
bash
$ yarn add -D babel-plugin-import-graphql
`
In .babelrc file:
`JSON
{
"plugins": ["import-graphql"]
}
`
Each time you modify a GraphQL file, the cache must be cleared for the changes to take effect.
If using node then the node_modules/.cache/babel-loader folder must be cleared. I recommend prepending the relevant script in your package.json and rerunning the script when you change a GraphQL file:
`JSON
{
"scripts": {
"start": "rm -rf ./node_modules/.cache/babel-loader && node index.js"
}
}
`
If using React Native then the metro cache must be reset every time you change a GraphQL file:
`
react-native start --reset-cache
`
> Note: Windows users would need to use rimraf or another solution in place of rm -rf.
Basic Usage
`JavaScript
...
import myQuery from './query.graphql'
...
export default graphql(myQuery)(myComponent)
`
Supported features
$3
Feature | Description
-|-
Default import | The entire source code for the file will act as the default export.
\#import syntax | Types, etc. in one GraphQL file can be imported into another GraphQL file using this syntax: #import "./types.graphql". These imports will be resolved recursively to any reasonable depth of files. Currently, all content in the named file will be imported and there is no way to import specific types. If you want that behavior, you can store a single type in each file.
$3
All variants of the import syntax are supported for non-schema files, except import './filename'.
Feature | Description
-|-
Multiple operations/fragments per file | Multiple operations (queries/mutations/subscriptions) and/or fragments can be placed in a single file. However, in this case you cannot use unnamed operations/fragments. For example, query { test } would need to be query someName { test }.
Default import | The first or only operation/fragment in a file will act as the default export. However, for backwards compatibility reasons, if there are both operations and fragments in a file, the first operation will act as the default export.
Named imports | All operations/fragments, including the default, act as named exports.
\#import syntax | Fragments in one GraphQL file can be imported into another GraphQL file using this syntax: #import "./fragment.graphql". These imports will be resolved recursively to any reasonable depth of files. Currently, all fragments in the named file will be imported and there is no way to import specific fragments. If you want that behavior, you can store a single fragment in each file.
Full example
#### Before (without this plugin):
ProductsPage.js
`JSX
import React, { Component } from 'react'
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'
class ProductsPage extends Component {
render() {
if (this.props.data.loading) return Loading...
return {This is my data: ${this.props.data.queryName}}
}
}
const productsQuery = gql
export default graphql(productsQuery)(ProductsPage)
`
#### After (with this plugin):
productFragment.graphql
`GraphQL
fragment productFragment on Product {
name
description
weight
}
`
productsQuery.graphql
`GraphQL
#import "./productFragment.graphql"
query products {
products {
productId
...productFragment
}
}
`
ProductsPage.js
`JSX
import React, { Component } from 'react'
import { graphql } from 'react-apollo'
import myImportedQuery from './productsQuery.graphql'
class ProductsPage extends Component {
render() {
if (this.props.data.loading) return Loading...
return {This is my data: ${this.props.data.queryName}}
}
}
export default graphql(myImportedQuery)(ProductsPage)
`
Options
Option | Type | Default | Description
-|-|-|-
nodePath | String | value of NODE_PATH environment variable | Intended for use with react-app-rewire-inline-import-graphql-ast -- Used to allow resolution of absolute paths to your .gql/.graphql files. If you already have your NODE_PATH variable set in your environment, you don't need to set this option. Not currently respected by #import syntax.
runtime | Boolean | false | Enabling this option requires graphql-tag to be installed as a peerDependency. -- Instead of inlining the parsed AST object, which is very large, this option inlines your GraphQL source code along with an import of the gql function from graphql-tag and parses your GraphQL source code with gql at runtime.
extensions | Array | [] | Enables loading GraphQL SDL files that have a custom extension, e.g. '.prisma'
emitDeclarations | Boolean | false | Enables emmitting .d.ts` files next to GraphQL query/fragment source file.