https://github.com/tw00/graphql-optimize-query
GraphQL query optimizer that evaluates "@include" and "@skip" client side.
https://github.com/tw00/graphql-optimize-query
graphql graphql-client graphql-directive
Last synced: 23 days ago
JSON representation
GraphQL query optimizer that evaluates "@include" and "@skip" client side.
- Host: GitHub
- URL: https://github.com/tw00/graphql-optimize-query
- Owner: tw00
- Created: 2021-07-03T00:56:57.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-06T21:19:36.000Z (almost 4 years ago)
- Last Synced: 2025-04-20T07:42:26.230Z (about 2 months ago)
- Topics: graphql, graphql-client, graphql-directive
- Language: TypeScript
- Homepage:
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# graphql-optimize-query
[](https://badge.fury.io/js/graphql-optimize-query)
A lightweight library to optimize graphql queries by evaluating `@include` and `@skip` directives client-side.
# Usage
Install graphql-optimize-query from npm with:
```bash
npm install graphql-optimize-query
```Import with:
```js
const { optimizeQuery } = require('graphql-optimize-query')
// or
import { optimizeQuery } from 'graphql-optimize-query';
```## Example
```js
const { print } = require('graphql');
const gql = require('graphql-tag');
const { optimizeQuery } = require('graphql-optimize-query');const query = gql`
query GetUser($userID: ID!, $auth: Boolean!) {
user(id: $userID) {
name @include(if: $auth) {
firstName
lastName
}
... on Asset @include(if: $auth) {
data
}
other
}
}
`;const editedAST = optimizeQuery(query, { auth: false });
console.log(print(editedAST));
```## Expected output
```gql
query GetUser($userID: ID!) {
user(id: $userID) {
other
}
}
```# License
This project is licensed under the MIT License - see the LICENSE file for details