Skip to content

GraphQL query optimizer that evaluates "@include" and "@Skip" client side.

Notifications You must be signed in to change notification settings

tw00/graphql-optimize-query

Repository files navigation

graphql-optimize-query

npm version

A lightweight library to optimize graphql queries by evaluating @include and @skip directives client-side.

Usage

Install graphql-optimize-query from npm with:

npm install graphql-optimize-query

Import with:

const { optimizeQuery } = require('graphql-optimize-query')
// or
import { optimizeQuery } from 'graphql-optimize-query';

Example

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

query GetUser($userID: ID!) {
  user(id: $userID) {
    other
  }
}

License

This project is licensed under the MIT License - see the LICENSE file for details