Gathering detailed insights and metrics for graphql-tag
Gathering detailed insights and metrics for graphql-tag
Gathering detailed insights and metrics for graphql-tag
Gathering detailed insights and metrics for graphql-tag
A JavaScript template literal tag that parses GraphQL queries
npm install graphql-tag
57.3
Supply Chain
100
Quality
79.9
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,330 Stars
321 Commits
177 Forks
43 Watching
15 Branches
72 Contributors
Updated on 20 Nov 2024
TypeScript (76.73%)
JavaScript (23.27%)
Cumulative downloads
Total Downloads
Last day
-3.4%
1,323,877
Compared to previous day
Last week
2.4%
6,877,372
Compared to previous week
Last month
10.5%
28,747,068
Compared to previous month
Last year
7%
318,319,126
Compared to previous year
1
1
Helpful utilities for parsing GraphQL queries. Includes:
gql
A JavaScript template literal tag that parses GraphQL query strings into the standard GraphQL AST./loader
A webpack loader to preprocess queriesgraphql-tag
uses the reference graphql
library under the hood as a peer dependency, so in addition to installing this module, you'll also have to install graphql
.
The gql
template literal tag can be used to concisely write a GraphQL query that is parsed into a standard GraphQL AST. It is the recommended method for passing queries to Apollo Client. While it is primarily built for Apollo Client, it generates a generic GraphQL AST which can be used by any GraphQL client.
1import gql from 'graphql-tag'; 2 3const query = gql` 4 { 5 user(id: 5) { 6 firstName 7 lastName 8 } 9 } 10`
The above query now contains the following syntax tree.
1{ 2 "kind": "Document", 3 "definitions": [ 4 { 5 "kind": "OperationDefinition", 6 "operation": "query", 7 "name": null, 8 "variableDefinitions": null, 9 "directives": [], 10 "selectionSet": { 11 "kind": "SelectionSet", 12 "selections": [ 13 { 14 "kind": "Field", 15 "alias": null, 16 "name": { 17 "kind": "Name", 18 "value": "user", 19 ... 20 } 21 } 22 ] 23 } 24 } 25 ] 26}
The gql
tag can also be used to define reusable fragments, which can easily be added to queries or other fragments.
1import gql from 'graphql-tag'; 2 3const userFragment = gql` 4 fragment User_user on User { 5 firstName 6 lastName 7 } 8`
The above userFragment
document can be embedded in another document using a template literal placeholder.
1const query = gql` 2 { 3 user(id: 5) { 4 ...User_user 5 } 6 } 7 ${userFragment} 8`
Note: While it may seem redundant to have to both embed the userFragment
variable in the template literal AND spread the ...User_user
fragment in the graphQL selection set, this requirement makes static analysis by tools such as eslint-plugin-graphql
possible.
GraphQL strings are the right way to write queries in your code, because they can be statically analyzed using tools like eslint-plugin-graphql. However, strings are inconvenient to manipulate, if you are trying to do things like add extra fields, merge multiple queries together, or other interesting stuff.
That's where this package comes in - it lets you write your queries with ES2015 template literals and compile them into an AST with the gql
tag.
This package only has one feature - it caches previous parse results in a simple dictionary. This means that if you call the tag on the same query multiple times, it doesn't waste time parsing it again. It also means you can use ===
to compare queries to check if they are identical.
To add support for importing .graphql
/.gql
files, see Webpack loading and preprocessing below.
Given a file MyQuery.graphql
1query MyQuery { 2 ... 3}
If you have configured the webpack graphql-tag/loader, you can import modules containing graphQL queries. The imported value will be the pre-built AST.
1import MyQuery from 'query.graphql'
You can also import query and fragment documents by name.
1query MyQuery1 { 2 ... 3} 4 5query MyQuery2 { 6 ... 7}
And in your JavaScript:
1import { MyQuery1, MyQuery2 } from 'query.graphql'
Preprocessing GraphQL queries and fragments into ASTs at build time can greatly improve load times.
GraphQL queries can be compiled at build time using babel-plugin-graphql-tag. Pre-compiling queries decreases script initialization time and reduces bundle sizes by potentially removing the need for graphql-tag
at runtime.
Try this custom transformer to pre-compile your GraphQL queries in TypeScript: ts-transform-graphql-tag.
Preprocessing queries via the webpack loader is not always possible. babel-plugin-import-graphql supports importing graphql files directly into your JavaScript by preprocessing GraphQL queries into ASTs at compile-time.
E.g.:
1import myImportedQuery from './productsQuery.graphql' 2 3class ProductsPage extends React.Component { 4 ... 5}
Using the included graphql-tag/loader
it is possible to maintain query logic that is separate from the rest of your application logic. With the loader configured, imported graphQL files will be converted to AST during the webpack build process.
Example webpack configuration
1{ 2 ... 3 loaders: [ 4 { 5 test: /\.(graphql|gql)$/, 6 exclude: /node_modules/, 7 loader: 'graphql-tag/loader' 8 } 9 ], 10 ... 11}
Preprocessing GraphQL imports is supported in create-react-app >= v2 using evenchange4/graphql.macro.
For create-react-app < v2, you'll either need to eject or use react-app-rewire-inline-import-graphql-ast.
Testing environments that don't support Webpack require additional configuration. For Jest use jest-transform-graphql.
With the webpack loader, you can import fragments by name:
In a file called query.gql
:
1fragment MyFragment1 on MyType1 { 2 ... 3} 4 5fragment MyFragment2 on MyType2 { 6 ... 7}
And in your JavaScript:
1import { MyFragment1, MyFragment2 } from 'query.gql'
Note: If your fragment references other fragments, the resulting document will
have multiple fragments in it. In this case you must still specify the fragment name when using the fragment. For example, with @apollo/client
you would specify the fragmentName
option when using the fragment for cache operations.
This package will emit a warning if you have multiple fragments of the same name. You can disable this with:
1import { disableFragmentWarnings } from 'graphql-tag'; 2 3disableFragmentWarnings()
This package exports an experimentalFragmentVariables
flag that allows you to use experimental support for parameterized fragments.
You can enable / disable this with:
1import { enableExperimentalFragmentVariables, disableExperimentalFragmentVariables } from 'graphql-tag';
Enabling this feature allows you to declare documents of the form.
1fragment SomeFragment ($arg: String!) on SomeType { 2 someField 3}
You can easily generate and explore a GraphQL AST on astexplorer.net.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 6/8 approved changesets -- score normalized to 7
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
10 existing vulnerabilities detected
Details
Score
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 More