Enables import syntax for .graphql and .gql files
Installations
npm install babel-plugin-import-graphql
Score
53.7
Supply Chain
99.5
Quality
75.7
Maintenance
100
Vulnerability
100
License
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
>= 4.x
Typescript Support
No
Node Version
14.15.3
NPM Version
6.14.9
Statistics
314 Stars
360 Commits
22 Forks
6 Watching
3 Branches
18 Contributors
Updated on 21 Nov 2024
Bundle Size
10.47 kB
Minified
3.41 kB
Minified + Gzipped
Languages
JavaScript (98.75%)
Shell (0.78%)
TypeScript (0.47%)
Total Downloads
Cumulative downloads
Total Downloads
14,084,911
Last day
12.3%
15,503
Compared to previous day
Last week
9.1%
68,973
Compared to previous week
Last month
-2.3%
275,527
Compared to previous month
Last year
14.6%
3,208,218
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
2
babel-plugin-import-graphql
Babel plugin enabling import
syntax for .graphql
and .gql
files.
For users of the old package name (babel-plugin-inline-import-graphql-ast)
Deprecation/Migration notes
As of May 27, 2018, the babel-plugin-inline-import-graphql-ast
package name is deprecated. Please use babel-plugin-import-graphql
(NPM) instead.
Migrating to babel-plugin-import-graphql
Update your babel configuration
Update plugins
array:
babel-plugin-inline-import-graphql-ast
(or inline-import-graphql-ast
) -> import-graphql
.
Update your package.json
file
Update the package name in devDependencies
:
babel-plugin-inline-import-graphql-ast
-> babel-plugin-import-graphql
.
Make sure your version string is compatible:
The first correct version of 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.
If you've pinned to a specific version, you'll need to upgrade and pin to at least 2.4.4
or widen your version range to include it.
Congratulations, you're all set!
If you enjoy my package please star the GitHub repo or share on Twitter (and follow me for updates)!
Prerequisites
-
babel-core@^7.20.7
or@babel/core@^7.0.0-beta.49
(Lower betas may work but weren't tested) -
graphql-tag@^2.9.2
(only if using theruntime
option described below)
Install
1yarn add -D babel-plugin-import-graphql
or
1npm i -D babel-plugin-import-graphql
In .babelrc
file:
1{ 2 "plugins": ["import-graphql"] 3}
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:
1{ 2 "scripts": { 3 "start": "rm -rf ./node_modules/.cache/babel-loader && node index.js" 4 } 5}
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 ofrm -rf
.
Basic Usage
1... 2import myQuery from './query.graphql' 3... 4export default graphql(myQuery)(myComponent)
Supported features
Schema files
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. |
Operation/fragment files
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
1import React, { Component } from 'react' 2import gql from 'graphql-tag' 3import { graphql } from 'react-apollo' 4 5class ProductsPage extends Component { 6 render() { 7 if (this.props.data.loading) return <h3>Loading...</h3> 8 return <div>{`This is my data: ${this.props.data.queryName}`}</div> 9 } 10} 11 12const productsQuery = gql` 13 query products { 14 products { 15 productId 16 name 17 description 18 weight 19 } 20 } 21` 22 23export default graphql(productsQuery)(ProductsPage)
After (with this plugin):
productFragment.graphql
1fragment productFragment on Product { 2 name 3 description 4 weight 5}
productsQuery.graphql
1#import "./productFragment.graphql" 2query products { 3 products { 4 productId 5 ...productFragment 6 } 7}
ProductsPage.js
1import React, { Component } from 'react' 2import { graphql } from 'react-apollo' 3import myImportedQuery from './productsQuery.graphql' 4 5class ProductsPage extends Component { 6 render() { 7 if (this.props.data.loading) return <h3>Loading...</h3> 8 return <div>{`This is my data: ${this.props.data.queryName}`}</div> 9 } 10} 11 12export 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. |
For users of create-react-app
create-react-app users can use this package without ejecting via react-app-rewire-inline-import-graphql-ast
Credits
The behavior of this plugin is inspired by and mostly mirrors the graphql-tag Webpack loader
This package started out as a modified version of babel-plugin-inline-import
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool detected
Details
- Info: SAST configuration detected: Snyk
- Warn: 0 commits out of 26 are checked with a SAST tool
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/eslint.yml:26
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/eslint.yml:28
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/snyk-security.yml:33
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/snyk-security.yml:35
- Info: topLevel 'contents' permission set to 'read': .github/workflows/dependency-review.yml:22
- Warn: no topLevel permission defined: .github/workflows/eslint.yml:1
- Info: topLevel 'contents' permission set to 'read': .github/workflows/snyk-security.yml:28
- Info: no jobLevel write permissions found
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
10 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 8
Reason
Found 1/5 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/dependency-review.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/detrohutt/babel-plugin-import-graphql/dependency-review.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/dependency-review.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/detrohutt/babel-plugin-import-graphql/dependency-review.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/eslint.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/detrohutt/babel-plugin-import-graphql/eslint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/eslint.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/detrohutt/babel-plugin-import-graphql/eslint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/snyk-security.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/detrohutt/babel-plugin-import-graphql/snyk-security.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/snyk-security.yml:77: update your workflow using https://app.stepsecurity.io/secureworkflow/detrohutt/babel-plugin-import-graphql/snyk-security.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: update-lock-files:6
- Warn: npmCommand not pinned by hash: .github/workflows/eslint.yml:35
- Warn: npmCommand not pinned by hash: .github/workflows/eslint.yml:36
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 3 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
- Warn: branch protection not enabled for branch 'beta'
Score
5.8
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to babel-plugin-import-graphql
@anders-nom/babel-plugin-import-graphql-string
Babel plugin to make .gql/.graphql files importable
babel-plugin-graphql-tag
Compiles GraphQL tagged template strings using graphql-tag
@babel/plugin-syntax-import-attributes
Allow parsing of the module attributes in the import statement
@babel/plugin-syntax-import-meta
Allow parsing of import.meta