Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hikiko4ern/swc-plugin-minify-graphql
GraphQL query and schema minimizer plugin for SWC
https://github.com/hikiko4ern/swc-plugin-minify-graphql
graphql swc-plugin
Last synced: 2 months ago
JSON representation
GraphQL query and schema minimizer plugin for SWC
- Host: GitHub
- URL: https://github.com/hikiko4ern/swc-plugin-minify-graphql
- Owner: hikiko4ern
- License: unlicense
- Created: 2024-10-15T14:12:59.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2024-10-23T00:22:26.000Z (3 months ago)
- Last Synced: 2024-10-25T06:38:31.055Z (3 months ago)
- Topics: graphql, swc-plugin
- Language: Rust
- Homepage: https://npm.im/swc-plugin-minify-graphql
- Size: 267 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# swc-plugin-minify-graphql
[GraphQL] query and schema minimizer plugin for [SWC]
## Compatibility
Since WASM plugins are not backward compatible (see [swc-project/swc#5060][swc-wasm-compat-issue], [Selecting the version - SWC][selecting-swc-core]), use the table below to select the correct plugin version:
| plugin version | `swc_core` version |
| :------------: | :------------------------------------------------------: |
| `0.1` | [`0.98.0` - *](https://plugins.swc.rs/versions/range/16) |## Usage
Install the package:
```sh
npm i swc-plugin-minify-graphql
```and add it to your SWC config.
### Basic
The plugin handles string literals and template literals marked with the GraphQL comment
```ts
const QUERY = /* GraphQL */ `
query ($id: ID!) {
image (id: $id) {
...img
}
}fragment img on Image {
id
url
}
`;
```minifying them:
```ts
const QUERY = /* GraphQL */ `query($id:ID!){image(id:$id){...img}}fragment img on Image{id url}`;
```Minification is supported not only for entire queries and schemas, but also for their individual parts (e.g., fragments):
```ts
const IMAGE_FIELDS = /* GraphQL */ `
id
url
`;
const IMAGE_FRAGMENT = /* GraphQL */ `
fragment image on Image {
id
url
}
`;// becomes
const IMAGE_FIELDS = /* GraphQL */ `id url`;
const IMAGE_FRAGMENT = /* GraphQL */ `fragment image on Image{id url}`;
```GraphQL comments are case-insensitive and can have any number of whitespace characters and asterisks at the beginning and end. `/* graphql */`, `/* GraphQL */`, `/** GraphQL */` and even `/* *** * gRaPhQl * *** */` will work.
### Template literals with expressions
Expressions within template literals are also supported:
```ts
const IMAGE = /* GraphQL */ `
id
url
`;const ENTITY = /* GraphQL */ `
id
image {
${IMAGE}
previewUrl
}
`;// becomes
const IMAGE = /* GraphQL */ `id url`;
const ENTITY = /* GraphQL */ `id image{${IMAGE} previewUrl}`;
```But with a single exception: expressions cannot break GraphQL tokens. The following code is invalid:
```ts
const LONG = 'Long';const FIELD = /* GraphQL */ `
id
some${LONG}FieldName
`;// becomes
const FIELD = /* GraphQL */ `id some ${LONG} FieldName`;
// instead of
const FIELD = /* GraphQL */ `id some${LONG}FieldName`;
```While the minified code may be correct in some cases, this usage is not intended and can be broken at any time.
### `gql` tag support
`gql`/`graphql` tagged template literals are not currently supported, and there are no plans to add support. You can use other plugins like [`graphql-tag-swc-plugin`] that support minification.
## Credits
- [`graphql-minify`](https://github.com/dan-lee/graphql-minify-rs): a re-implementation of [`stripIgnoredCharacters`](https://graphql-js.org/api/function/stripignoredcharacters/) from the [GraphQL.js reference implementation](https://github.com/graphql/graphql-js) in Rust
[GraphQL]: https://graphql.org
[SWC]: https://swc.rs
[swc-wasm-compat-issue]: https://github.com/swc-project/swc/issues/5060
[selecting-swc-core]: https://swc.rs/docs/plugin/selecting-swc-core
[`graphql-tag-swc-plugin`]: https://github.com/rishabh3112/graphql-tag-swc-plugin