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;
}
Dependencies
- 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 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 Dependencies
- graphql: A Query Language and Runtime which can target any service.