0.1.3 • Published 5 years ago

graphql-query-match v0.1.3

Weekly downloads
10
License
ISC
Repository
github
Last release
5 years ago

graphql-query-match

Check the contents of GraphQL queries using other GraphQL queries.

This package is still under development, but if you want to try it, check out some of the test cases on github for examples.

API:

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);
    }
  }
};
0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago