graphql-query-vars-cleaner

Remove undefined variables from a GraphQL query

Usage no npm install needed!

<script type="module">
  import graphqlQueryVarsCleaner from 'https://cdn.skypack.dev/graphql-query-vars-cleaner';
</script>

README

graphql-query-vars-cleaner

npm typescript GitHub stars Twitter Follow

Remove undefined variables from GraphQL queries.

Why?

Having a large number of undefined variables in a GraphQL can hurt performance on some backends. For a query backing a table, you might have a filter and an order variable for each column. Often it is easier to leave these variables as undefined, so instead graphql-query-vars-cleaner will filter out these undefined variables.

Installation

yarn add graphql-query-vars-cleaner
npm install graphql-query-vars-cleaner

Note:
GraphQL is a peer dependency. To run, also install graphql:
Yarn: yarn add graphql
NPM: npm install graphql

API

import { getQuery } from "graphql-query-vars-cleaner";

const query = `
query ($var1: Int, $var2: Int) {
  field(where: $var1, and: $var2) {
    x
    y
  }
}`;

const variables = {
  var1: 1
};

console.log(getQuery(query, variables));

// Output:
// query ($var1: Int) { field (where: $var1) { x y } }

Types

import { getQuery, Variables } from "graphql-query-vars-cleaner";

function getQuery(query: string, variables?: Variables): string

type Variables = {
  [name: string]: string | undefined;
}

Dependenciesdependencies

  • graphql-tag: A JavaScript template literal tag that parses GraphQL queries
  • is-obj-empty: Check if an object is empty
  • json-to-graphql-query: This is a simple module that takes a JavaScript object and turns it into a GraphQL query to be sent to a GraphQL server.

Dev Dependenciesdev dependencies

  • @bconnorwhite/bob: Bob builds and watches typescript projects.
  • graphql: A Query Language and Runtime which can target any service.
  • jest: Delightful JavaScript Testing.

Peer Dependenciespeer dependencies

  • graphql: A Query Language and Runtime which can target any service.

License license

MIT