Compiles GraphQL tagged template strings using graphql-tag.
Installations
npm install babel-plugin-graphql-tag
Score
51.5
Supply Chain
98
Quality
75.6
Maintenance
100
Vulnerability
100
License
Developer
gajus
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
14.17.0
NPM Version
7.8.0
Statistics
172 Stars
101 Commits
28 Forks
3 Watching
1 Branches
19 Contributors
Updated on 13 Mar 2024
Bundle Size
819.62 kB
Minified
197.48 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
25,307,135
Last day
-8.4%
14,374
Compared to previous day
Last week
-10%
70,876
Compared to previous week
Last month
-0.7%
335,300
Compared to previous month
Last year
-16.8%
4,180,010
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
2
babel-plugin-graphql-tag
Compiles GraphQL tagged template strings using graphql-tag.
Motivation
Compiling GraphQL queries at the build time:
- reduces the script initialization time; and
- removes the
graphql-tag
dependency
Removing the graphql-tag
dependency from the bundle saves approx. 50 KB.
Implementation
- Searches for imports of
graphql-tag
and removes them. - Searches for tagged template literals with
gql
identifier and compiles them usinggraphql-tag
.
Example compilation
Input:
1import gql from 'graphql-tag'; 2// if using apollo v3 3import { gql } from '@apollo/client'; 4 5const foo = gql`query {bar}`; 6
Output:
1const foo = { 2 "definitions": [ 3 { 4 "directives": [ 5 ], 6 "kind": "OperationDefinition", 7 "operation": "query", 8 "selectionSet": { 9 "kind": "SelectionSet", 10 "selections": [ 11 { 12 "alias": null, 13 "arguments": [ 14 ], 15 "directives": [ 16 ], 17 "kind": "Field", 18 "name": { 19 "kind": "Name", 20 "value": "bar" 21 }, 22 "selectionSet": null 23 } 24 ] 25 }, 26 "variableDefinitions": [ 27 ] 28 } 29 ], 30 "kind": "Document", 31 "loc": { 32 "end": 11, 33 "start": 0 34 } 35}; 36
NOTE: require() is also supported.
Using fragments
Using GraphQL fragments requires to:
- Define a fragment using
graphql-tag
. - Append the referenced fragment as a variable to the end of the GraphQL query.
Example:
1import gql from 'graphql-tag'; 2 3const bar = gql` 4 fragment barFragment on Foo { 5 field1 6 field2 7 } 8`; 9 10const foo = gql` 11 query foo { 12 foo { 13 ...barFragment 14 } 15 } 16 17 ${bar} 18`; 19
Options
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"]
)
1// babel.config.js 2plugins: [ 3 [ 4 "babel-plugin-graphql-tag", 5 { 6 strip: true, 7 transform: (source, ast) => { 8 const h = hash(source); // use your favorite hashing method 9 graphqlAstHashes[h] = ast; // write this to a file when compilation is complete 10 return { 11 queryId: h 12 }; 13 } 14 } 15 ] 16]
Known Issues
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 10/15 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
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
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Score
3.8
/10
Last Scanned on 2024-11-18
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-graphql-tag
babel-plugin-import-graphql
Babel plugin to make .gql/.graphql files importable
graphql-let
A webpack loader/babel-plugin/babel-plugin-macros/CLI/generated file manager of GraphQL code generator.
graphql-tag
A JavaScript template literal tag that parses GraphQL queries
@graphql-tools/graphql-tag-pluck
Pluck graphql-tag template literals