Skip to content

jeremy-deutsch/graphql-query-match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-query-match

Check the contents of GraphQL queries using other GraphQL queries. Works with compiled GraphQL queries as well as apollo-server query subtrees.

API examples:

import doesQueryMatch from "graphql-query-match";
import gql from "graphql-tag";

const myQuery = gql`
  {
    county {
      cities {
        parks {
          name
        }
        lakes {
          area
        }
      }
    }
  }
`;

// true
const queryHasParkNames = doesQueryMatch(
  gql`
    {
      county {
        cities {
          parks {
            name
          }
        }
      }
    }
  `,
  myQuery
);

// true
const queryHasCities = doesQueryMatch(
  gql`
    {
      county {
        cities
      }
    }
  `,
  myQuery
);

// false
const queryHasFairs = doesQueryMatch(
  gql`
    {
      county {
        fairs
      }
    }
  `,
  myQuery
);

The main use case for this is in apollo-server:

import doesQueryMatch from "graphql-query-match";
import gql from "graphql-tag";

const parksMatcher = gql`
  {
    parks
  }
`;

const resolvers = {
  County: {
    cities: (countyObj, vars, ctx, queryInfo) => {
      if (doesQueryMatch(parksMatcher, queryInfo)) {
        ctx.prefetchParks(countyObj.id);
      }
      return ctx.getCities(countyObj.id);
    }
  }
};

About

Validate the contents of GraphQL queries using other GraphQL queries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published