🚀 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 - npm Package Compare versions

Comparing version

to
1.1.1

yarn.lock

17

dist/index.js

@@ -11,19 +11,2 @@ "use strict";

};
// declare global {
// interface Array<T> {
// /**
// * @param pattern glob pattern to match the result against
// * @returns the matched entries in the array
// */
// contains(pattern: string): boolean;
// }
// }
// /**
// * @param pattern glob pattern to match the result against
// * @returns the matched entries in the array
// */
// Array.prototype.contains = function(pattern: string) {
// const byPattern = (p: string) => picomatch(pattern)(p);
// return this.find(byPattern) !== undefined;
// };
exports.getPaths = function (info) {

@@ -30,0 +13,0 @@ return info.operation.selectionSet.selections.reduce(createReduceSelections("/"), []);

{
"name": "graphql-query-path",
"version": "1.1.0",
"version": "1.1.1",
"private": false,

@@ -17,5 +17,2 @@ "license": "MIT",

},
"dependencies": {
"picomatch": "^2.0.7"
},
"peerDependencies": {

@@ -22,0 +19,0 @@ "graphql": "^14.4.2"

65

README.md
# 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.
<!-- ![Version](https://img.shields.io/badge/version-0.0.1-blue.svg?cacheSeconds=2592000) [![Twitter: alber70g](https://img.shields.io/twitter/follow/alber70g.svg?style=social)](https://twitter.com/alber70g) -->

@@ -33,4 +37,10 @@

A library that allows you to smartly execute database queries by looking at the field selection. This can mitigate the N+1 problem of GraphQL queries.
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

@@ -44,3 +54,3 @@

Use it in your ts or js project like:
Use it in your graphql-resolver:

@@ -53,3 +63,4 @@ ```js

user(args, context, info) {
if (getPaths(info).contains('/user/posts/**')) {
const paths = getPaths(info);
if (paths.find((p) => p.indexOf('/user/posts/') > -1)) {
db.getUsersWithPosts();

@@ -63,2 +74,44 @@ } else {

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.
```sh
npm i graphql-query-paths-contains
```
```js
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/argument | type | description |
| ------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| **getPathsFromAST(ast)** | `string[][]` | Returns a list of subqueries with paths reflected in the sub query per subquery |
| <span style="align: right">ast</span> | `DocumentNode` [link](https://graphql.org/graphql-js/language/#parse) | The DocumentNode from `import { parse } from 'graphql'` |
| **getPaths(info)** | `string[]` | Returns a list of paths reflected in the query |
| info | `GraphQLResolveInfo` [link](https://graphql.org/graphql-js/type/#graphqlobjecttype) | The last argument in a resolver |
Library **graphql-query-paths-contains** extends the library above with a
`contains` function
| function/argument | type | description |
| ---------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Array.prototype.contains(glob)** | `boolean` | Extends Array with `contains` function. To know if a result contains a path you can execute `getPaths(info).contains("/user/**")`. This returns a boolean |
| glob | `string` | a string representing a glob to filter the array with |
## Author

@@ -75,3 +128,4 @@

Feel free to check [issues page](https://github.com/alber70g/graphql-query-path/issues).
Feel free to check
[issues page](https://github.com/alber70g/graphql-query-path/issues).

@@ -84,2 +138,3 @@ ## Show your support

_This README was generated by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_
_This README was generated by
[readme-md-generator](https://github.com/kefranabg/readme-md-generator)_

@@ -51,3 +51,3 @@ import { getPathsFromAST, getPaths } from './index';

test('paths.contains with GraphQLResolveInfo single depth', async () => {
test('getPaths with GraphQLResolveInfo single depth', async () => {
const res = await graphql(

@@ -78,4 +78,2 @@ buildSchema(

expect(paths).toEqual(['/user/', '/user/name']);
// expect(paths.contains('/user/posts/')).toEqual(false);
// expect(paths.contains('/user/**')).toBe(true);
return {};

@@ -88,3 +86,3 @@ },

test('paths.contains with GraphQLResolveInfo double depth', async () => {
test('getPaths with GraphQLResolveInfo double depth', async () => {
const res = await graphql(

@@ -123,3 +121,2 @@ buildSchema(

]);
// expect(paths.contains('/user/posts/')).toEqual(true);
return {};

@@ -126,0 +123,0 @@ },

@@ -8,4 +8,2 @@ import {

import * as picomatch from "picomatch";
export const getPathsFromAST = (ast: DocumentNode) => {

@@ -21,21 +19,2 @@ return ast.definitions.map(walkDefinitions);

// declare global {
// interface Array<T> {
// /**
// * @param pattern glob pattern to match the result against
// * @returns the matched entries in the array
// */
// contains(pattern: string): boolean;
// }
// }
// /**
// * @param pattern glob pattern to match the result against
// * @returns the matched entries in the array
// */
// Array.prototype.contains = function(pattern: string) {
// const byPattern = (p: string) => picomatch(pattern)(p);
// return this.find(byPattern) !== undefined;
// };
export const getPaths = (info: GraphQLResolveInfo) => {

@@ -42,0 +21,0 @@ return info.operation.selectionSet.selections.reduce(

@@ -12,4 +12,4 @@ {

},
"include": ["src/index.ts", "src/picomatch.d.ts"],
"include": ["src/index.ts"],
"exclude": ["**/*.spec.ts"]
}

Sorry, the diff of this file is not supported yet