Registry / devops / graphql-combine-query

graphql-combine-query

JSON →
library1.2.4jsnpmunverified

A utility to combine multiple GraphQL query or mutation documents into a single document, with support for variable merging and aliasing. Current stable version is 1.2.4, released on npm. It targets Hasura-like servers that benefit from batched mutations in a single transaction, and it works with GraphQL.js v15 or v16. Unlike simple string concatenation, it properly merges arguments, variables, and top-level selections. It also supports adding multiple instances of the same query/mutation with index aliasing via addN. Ships TypeScript type definitions.

npm install graphql-combine-query
INSTALL
IMPORT
SIG · GRAPHQL-COMBINE-QU
G
graphql-combine-query
devopsjavascriptv1.2.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import combineQuery from 'graphql-combine-query'
const { combineQuery } = require('graphql-combine-query')
Default export is a function, not a named export. CommonJS require must use .default or default import.
combineQuery
import combineQuery from 'graphql-combine-query'
import { combineQuery } from 'graphql-combine-query'
There is no named export 'combineQuery'; it is the default export.
type
import type { CombineQueryResult } from 'graphql-combine-query' // or import from 'graphql-combine-query' and use typeof
import { CombineQueryResult } from 'graphql-combine-query'
TypeScript types are exported, but the type CombineQueryResult is not a runtime value; to import it you must use type import syntax.

Creates a combined query document by merging two separate queries with their variables, demonstrating .add() usage.

import combineQuery from 'graphql-combine-query'; import gql from 'graphql-tag'; const query1 = gql` query Foo($foo: String!) { getFoo(foo: $foo) } `; const query2 = gql` query Bar($bar: String!) { getBar(bar: $bar) } `; const { document, variables } = combineQuery('CombinedQuery') .add(query1, { foo: 'val1' }) .add(query2, { bar: 'val2' }); console.log(require('graphql/language').print(document)); /* query CombinedQuery($foo: String!, $bar: String!) { getFoo(foo: $foo) getBar(bar: $bar) } */ console.log(variables); // { foo: 'val1', bar: 'val2' }
Debug
Known issues
breakinggraphql peer dependency must be >=15.1.0 || ^16.0.0. Using older versions will cause runtime errors.
fix
Install graphql@^15.1.0 or ^16.0.0 alongside this package.
affects: <1.0.0
deprecatedThe .addN() method appends indexes to variable names, which could conflict if you have similarly named variables already.
fix
Ensure variable names do not end with '_0', '_1', etc., or rename them after combining.
affects: all
gotchaWhen using with Apollo, you must pass the combined document directly to client.query() or client.mutate(); do not try to split back.
fix
Use the combined document as a single query/mutation.
affects: all
gotchaThe package does not handle fragments inside queries currently; fragments must be inlined or considered duplicate fields.
fix
Avoid using fragments, or convert them to inline fragments manually before combining.
affects: all
Errors
Common errors & fixes
Cannot find module 'graphql'
Missing GraphQL peer dependency.
fix
Install graphql: npm install graphql
export 'default' (imported as 'combineQuery') was not found in 'graphql-combine-query' (possible exports: )
Using named import syntax (import { combineQuery }) instead of default import.
fix
Use import combineQuery from 'graphql-combine-query'
TypeError: combineQuery is not a function
Not importing the default export correctly (e.g., using require without .default).
fix
Use require('graphql-combine-query').default or switch to ES module import.
Upgrade
Version history
1.2.4latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for GraphQL v15 or v16. This package only works with one of these versions.
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-combine-query — npm install graphql-combine-query · libregistry