🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

graphql-query-path

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-query-path

A library that allows you to smartly execute database queries by looking at the field selection. This can mitigate the N+1 and even 1+1 problem of GraphQL queries.

1.1.1
Source
npm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

graphql-query-path

A library that allows you to smartly execute database queries by looking at the field selection. This can mitigate the N+1 and even 1+1 problem of GraphQL queries.

follow on Twitter

This repo contains two projects:

  • graphql-query-path that has two functions: getPaths and getPathsFromAST. They return a list of paths reflecting the graphql-query
  • graphql-query-path-contains the same as above and extends Array with contains(glob: string): boolean method that you can use to do glob matching. This one is ~17k bigger because of a dependency on picomatch.

Usage

Install the package

npm i graphql-query-path

Use it in your graphql-resolver:

import { getPaths } from 'graphql-query-paths';
// or
// const { getPaths } = require('graphql-query-paths');
const resolvers = {
  user(args, context, info) {
    const paths = getPaths(info);
    if (paths.find((p) => p.indexOf('/user/posts/') > -1)) {
      db.getUsersWithPosts();
    } else {
      db.getUsers();
    }
  },
};

Use the extended version to match glob pattern with contains from graphql-query-paths-contains. This includes picomatch but increases the lib size by ~17k.

npm i graphql-query-paths-contains
import { getPaths } from 'graphql-query-paths-contains';
// or
// const { getPaths } = require('graphql-query-paths-contains');
const resolvers = {
  user(args, context, info) {
    if (getPaths(info).contains("/users/posts/"))) {
      db.getUsersWithPosts();
    } else {
      db.getUsers();
    }
  },
};

Interface docs

Library graphql-query-paths

function/argumenttypedescription
getPathsFromAST(ast)string[][]Returns a list of subqueries with paths reflected in the sub query per subquery
astDocumentNode linkThe DocumentNode from import { parse } from 'graphql'
getPaths(info)string[]Returns a list of paths reflected in the query
infoGraphQLResolveInfo linkThe last argument in a resolver

Library graphql-query-paths-contains extends the library above with a contains function

function/argumenttypedescription
Array.prototype.contains(glob)booleanExtends Array with contains function. To know if a result contains a path you can execute getPaths(info).contains("/user/**"). This returns a boolean
globstringa string representing a glob to filter the array with

Author

Albert Groothedde

Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

This README was generated by readme-md-generator

FAQs

Package last updated on 01 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts