Removes unnecessary white space from graphql queries.
npm install babel-plugin-transform-compress-graphql> Removes unnecessary white space from GraphQL queries using
> graphql-query-compress.






``sh`
npm install --save-dev babel-plugin-transform-compress-graphql
Compress GraphQL queries by simply adding a / GraphQL / comment or tagging them withgql
the template tag. The tag name or comment text can be customized with an option.
In
`js
const query = gql
people(uuid:"${uuid}") {
uuid,
fullName,
dateOfBirth,
nextBirthday,
age,
relations {
description,
type,
person {
uuid,
fullName
}
}
events {
uuid,
name,
description,
startDate, startTime,
endDate, endTime
}
};`
Which is pretty big literal right?
Out
`jspeople(uuid:"${uuid}"){uuid,fullName,dateOfBirth,nextBirthday,age,relations{description,type,person{uuid,fullName}}events{uuid,name,description,startDate,startTime,endDate,endTime}}
const query = ;`
Which saves quite some white space which has no use in production builds.
Since 1.3.0 it's possible to use a comment to mark a template literals as a GraphQL query.
`js
const query = / GraphQL /
people {
fullName,
age
};`
The value of the comment can be changed using the comment option. The comment is being/ graphql /
matched in a case-insensitive manner so is also fine.
Important: The comment _only_ works with template literals. Normal strings will not
be compressed.
.babelrc
`json`
{
"env": {
"production": {
"plugins": ["transform-compress-graphql"]
}
}
}
`sh`
babel --plugins transform-compress-graphql script.js
`js`
require("@babel/core").transform("code", {
plugins: ["transform-compress-graphql"],
});
Options can be passed by wrapping the plugin name in an array and adding an
object literal as the second item in that array.
For example in your .babelrc:
`json`
{
"env": {
"production": {
"plugins": [
[
"transform-compress-graphql",
{
"tagName": "gql",
"tagFunction": "",
"comment": "graphql",
"removeComments": true
}
]
]
}
}
}
The following options are available:
- tagName - String with the name of the tag to search for. Defaults"gql"
to .tagFunction
- - String with the name of the tag to replace the tag with.""
Defaults to: which means the tag will be removed from the output.removeComments
- - Boolean when set to false will leave any matched comments intrue
place. Defaults to .comment
- - String with the case-insensitive text matched in any leading comments"graphql"
for a template literal. The entire string will be expected. Defaults to .
?For template literals with a tag named gql (or with the same name as the tagNametagFunction
option) the scope will be checked for an existing binding of that variable and will not
be replaced with if it exists.
GraphQL literals will be compressed with graphql-query-compress`.
Any issues related to the compressed query result should be posted as an issue in the
issue tracker of that package.
MIT