Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
babel-plugin-graphql-tag
Advanced tools
Compiles GraphQL tagged template strings using graphql-tag
Compiles GraphQL tagged template strings using graphql-tag.
Compiling GraphQL queries at the build time:
graphql-tag
dependencyRemoving the graphql-tag
dependency from the bundle saves approx. 50 KB.
graphql-tag
and removes them.gql
identifier and compiles them using graphql-tag
.Input:
import gql from 'graphql-tag';
// if using apollo v3
import { gql } from '@apollo/client';
const foo = gql`query {bar}`;
Output:
const foo = {
"definitions": [
{
"directives": [
],
"kind": "OperationDefinition",
"operation": "query",
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"alias": null,
"arguments": [
],
"directives": [
],
"kind": "Field",
"name": {
"kind": "Name",
"value": "bar"
},
"selectionSet": null
}
]
},
"variableDefinitions": [
]
}
],
"kind": "Document",
"loc": {
"end": 11,
"start": 0
}
};
NOTE: require() is also supported.
Using GraphQL fragments requires to:
graphql-tag
.Example:
import gql from 'graphql-tag';
const bar = gql`
fragment barFragment on Foo {
field1
field2
}
`;
const foo = gql`
query foo {
foo {
...barFragment
}
}
${bar}
`;
importSources
- An array of names for modules to import (default = ["graphql-tag", "@apollo/client"]
)onlyMatchImportSuffix
- Matches the end of the import instead of the entire name. Useful for relative imports, e.g. ./utils/graphql
(default = false)strip
- Strips insignificant characters such as whitespace from the original GraphQL string literal to reduce the size of compiled AST (default = false)transform
- By default, graphql query strings will be replaced with their AST representations, but you can override that behavior and do whatever you like. One possible use case would be to implement persisted queries:gqlTagIdentifiers
- An array of names for gql tag identifiers (default = ["gql"]
)// babel.config.js
plugins: [
[
"babel-plugin-graphql-tag",
{
strip: true,
transform: (source, ast) => {
const h = hash(source); // use your favorite hashing method
graphqlAstHashes[h] = ast; // write this to a file when compilation is complete
return {
queryId: h
};
}
}
]
]
Some cases are really hard to track down:
const apolloClient = require('@apollo/client');
// or
import apolloClient from '@apollo/client';
const { gql } = apolloClient;
const foo = gql`...`;
If you have this kind of syntax, this plugin won't work for you.
FAQs
Compiles GraphQL tagged template strings using graphql-tag
We found that babel-plugin-graphql-tag 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.