npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-query-merger

v1.0.3

Published

Merge and unify multiple GraphQL queries into a single operation with field and variable conflict resolution.

Downloads

238

Readme

GraphQL Query Merger

GraphQL Query Merger is a utility library designed to merge multiple GraphQL queries into a single, cohesive query. This helps developers streamline their GraphQL requests by combining related operations efficiently, avoiding redundancy and improving maintainability.


Table of Contents


Features

  • Merge GraphQL Queries: Seamlessly combine multiple queries into one.
  • Variable Deduplication: Automatically handles conflicts by renaming variables.
  • Supports Conditional Queries: Add queries dynamically based on runtime logic.
  • Customizable Query Names: Optionally name the resulting query.

Installation

To install the library, use npm or yarn:

npm install graphql-query-merger graphql

Note: graphql-query-merger has a peer dependency on graphql. Ensure that the graphql package is installed alongside it.


Usage

Importing the Library

To use the library, import the necessary functions:

const { mergeQueries } = require('graphql-query-merger');
const { print } = require('graphql');
const { gql } = require('graphql-tag');
  • mergeQueries(): Initializes a new query merger instance.
  • push(query, variables?): Adds a query to the merger. Optionally accepts a variables object.
  • print(): Converts the resulting query to a printable string format.

Combining Queries

Basic Query Combination

You can combine queries written as strings or gql-tagged templates.

const query_1 = /* GraphQL */ `
  {
    user {
      id
      name
    }
  }
`;

const query_2 = /* GraphQL */ gql`
  {
    post {
      id
      title
    }
  }
`;

const combined = mergeQueries().push(query_1).push(query_2);

console.log(print(combined.query));

Output:

{
  user {
    id
    name
  }
  post {
    id
    title
  }
}

Combining Queries with Variables

When combining queries that include variables, the library automatically renames conflicting variable names to maintain uniqueness.

const query_1 = /* GraphQL */ gql`
  query ($id: ID!) {
    user(id: $id) {
      id
      name
    }
  }
`;

const query_2 = /* GraphQL */ `
  query ($id: ID!) {
    post(id: $id) {
      id
      title
    }
  }
`;

const combined = mergeQueries().push(query_1, { id: 1 }).push(query_2, { id: 2 });

console.log(print(combined.query));
console.log(combined.variables);

Output:

query ($id_1: ID!, $id_2: ID!) {
  user(id: $id_1) {
    id
    name
  }
  post(id: $id_2) {
    id
    title
  }
}

Variables:

{
  "id_1": 1,
  "id_2": 2
}
  • Automatic Variable Renaming: Conflicting variable names (id) are renamed (id_1, id_2).

Conditional Query Combination

You can dynamically add queries based on runtime conditions.

const combined = mergeQueries('optionalQueryName');

combined.push(
  /* GraphQL */ `
    query ($email: String!) {
      user(email: $email) {
        id
        name
      }
    }
  `,
  { email: '[email protected]' },
);

if (true) {
  combined.push(
    /* GraphQL */ `
      query ($id: ID!) {
        post(id: $id) {
          id
          title
          date
        }
      }
    `,
    { id: 10 },
  );
}

console.log(print(combined.query));
console.log(combined.variables);

Output:

query optionalQueryName($email_1: String!, $id_2: ID!) {
  user(email: $email_1) {
    id
    name
  }
  post(id: $id_2) {
    id
    title
    date
  }
}

Variables:

{
  "email_1": "[email protected]",
  "id_2": 10
}

Notes

  1. Variable Scope: The library renames variables in the combined query to ensure no conflicts occur between different queries.
  2. Query Formats: Queries can be provided as:
    • String literals
    • gql-tagged templates (recommended for syntax highlighting and validation).
  3. Error Handling: Ensure all variables required by the queries are provided when invoking push(). Missing variables will lead to runtime errors.