Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
babel-plugin-inline-import-graphql-ast
Advanced tools
Babel plugin to make .gql/.graphql files importable
Babel plugin allowing you to import
.graphql
and .gql
files. The code is pre-parsed by gql
from graphql-tag
and the GraphQL AST object is inserted into your code at transpile time.
Cannot read property 'type' of undefined
in file containing .gql/.graphql importI'm putting this right at the top because if you use this package to any real degree you'll almost certainly experience this error at some point and it can be hard to track down if you don't know what it is. This is caused by a bug in Babel 6.
A PR has been submitted to Babel here and hopefully they'll merge it and make a patch release soon.
In the meantime, there's an easy, although annoying, workaround. Just change the content of the file throwing the error in any way. The easiest and most reliable change I've found is to simply add an empty comment //
on it's own line when this error occurs. As the file gets updated over time, you can later remove the empty comment. I usually only have 1 or 2 of these in my project at any given time.
Replaces graphql-tag/loader
in projects where Webpack is unavailable(i.e. NextJS)
Users of create-react-app that want to avoid ejecting their app can use this package indirectly by using react-app-rewire-inline-import-graphql-ast
Before (without babel-plugin-inline-import-graphql-ast):
// ProductsPage.js
...
import { gql } from 'react-apollo'
class ProductsPage extends React.Component {
...
}
const productsQuery = gql`
query products {
products {
productId
name
description
weight
}
}
`
export default graphql(productsQuery)(ProductsPage)
Now (with babel-plugin-inline-import-graphql-ast):
// productFragment.graphql
fragment productFragment on Product {
name
description
weight
}
// productsQuery.graphql
#import "./productFragment.graphql"
query products {
products {
productId
...productFragment
}
}
// ProductsPage.js
...
import myImportedQuery from './productsQuery.graphql'
class ProductsPage extends React.Component {
...
}
export default graphql(myImportedQuery)(ProductsPage)
Note: both cases are equivalent and will result in similar code after Babel transpiles them. Check How it works section for details.
yarn add -D babel-plugin-inline-import-graphql-ast
Add a .babelrc
file and write:
{
"plugins": [
"babel-plugin-inline-import-graphql-ast"
]
}
or pass the plugin with the plugins-flag on CLI
babel-node myfile.js --plugins babel-plugin-inline-import-graphql-ast
Babel-Inline-Import supports the following file extensions:
nodePath
-- Takes a string just like the NODE_PATH
environment variable and is used to allow resolution of absolute paths to your .gql
/.graphql
files. Note this currently only works in javascript files. If you already have your NODE_PATH
variable set in your environment, you don't need to set this option. It is intended primarily for use with react-app-rewire-inline-import-graphql-ast.When you import
a .graphql
or .gql
file, it is parsed into a GraphQL AST object by the gql
function from graphql-tag
. This AST object is inserted directly into the importing file, in a variable with the name defined in the import statement. The import should be treated as a default import(name whatever you want; no braces necessary)
Babel does not track dependency between imported and importing files after the transformation is made. Therefore, you need to change the importing file in order to see your changes in the imported file take effect. To overcome this, you can:
BABEL_DISABLE_CACHE=1
)Also make sure that your task runner is watching for changes in the imported file as well. You can see it working here.
Note - If BABEL_DISABLE_CACHE
doesn't work, you'll need to manually clear the node_modules/.cache
folder to re-transpile your .gql
/.graphql
files. You can either prepend your normal start
script command with rimraf ./node_modules/.cache &&
or create a separate script i.e. clean
. Note you'd need the rimraf dependency installed in this example
This package started out as a modified version of babel-plugin-inline-import
v2.1.2 (January 26, 2018)
FAQs
Babel plugin to make .gql/.graphql files importable
The npm package babel-plugin-inline-import-graphql-ast receives a total of 349 weekly downloads. As such, babel-plugin-inline-import-graphql-ast popularity was classified as not popular.
We found that babel-plugin-inline-import-graphql-ast demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.