Remove undefined variables from a GraphQL query
npm install graphql-query-vars-cleaner> Remove undefined variables from GraphQL queries.
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.
``bash`
yarn add graphql-query-vars-cleaner
`bash`
npm install graphql-query-vars-cleaner
> Note:
> GraphQL is a peer dependency. To run, also install graphql:
> Yarn: yarn add graphql npm install graphql
> NPM:
`js
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 } }
`
ts
import { getQuery, Variables } from "graphql-query-vars-cleaner";function getQuery(query: string, variables?: Variables): string
type Variables = {
[name: string]: string | undefined;
}
``- 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.
- @bconnorwhite/bob: Bob builds and watches typescript projects.
- graphql: A Query Language and Runtime which can target any service.
- jest: Delightful JavaScript Testing.
- graphql: A Query Language and Runtime which can target any service.