Skip to content

Commit 5374a14

Browse files
Allow to use get separately
Update README.md
1 parent 79435f7 commit 5374a14

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@ You can have one request and get any nested data you want
2323
npm i @nazariistrohush/gql-prisma-select
2424
```
2525

26+
## Types
27+
28+
### Arguments of `new GQLPrismaSelect(info, options)` constructor
29+
30+
- `info` - `GraphQLResolveInfo` object
31+
- `options` - `object` with options
32+
- `options.get` - `string | string[]` String split by `.` or an array to get custom path of selection (similar to lodash.get)
33+
- `options.exclude` - `string[]` Fields to exclude from selection
34+
- `__typename` excluded by default (because not exists in prisma model)
35+
36+
### Results of `new GQLPrismaSelect(info, options)`
37+
38+
- `include` - `object` with include object for Prisma
39+
- `select` - `object` with select object for Prisma
40+
- `originalInclude` - `object` with original include object (same as `include` in case `get` option is not used)
41+
- `originalSelect` - `object` with original select object (same as `select` in case `get` option is not used)
42+
43+
### Static methods
44+
45+
- `GQLPrismaSelect.get(selection, path)` - get some specific selections by path
46+
- `selection` - `object` with selection ({ include, select } result of `new GQLPrismaSelect(info, options)` constructor)
47+
- `path` - `string | string[]` String split by `.` or an array
48+
- Used to get specific selection from select/include object
49+
50+
E.g. get different selections from one GQLPrismaSelect constructor call
51+
52+
```ts
53+
const includeSelect = new GQLPrismaSelect(info);
54+
const { include, select } = GQLPrismaSelect.get(
55+
includeSelect,
56+
'collection.User'
57+
);
58+
const { include: includePosts, select: selectPosts } = GQLPrismaSelect.get(
59+
includeSelect,
60+
'collection.Post'
61+
);
62+
```
63+
2664
## Quick example
2765

2866
Get info from your request using `@nestjs/graphql`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nazariistrohush/gql-prisma-select",
3-
"version": "0.0.19",
3+
"version": "0.1.0",
44
"description": "Package to get selection according to gql query fields",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/GQLPrismaSelect.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export class GQLPrismaSelect<S = any, I = any> {
3131
this.originalInclude = res.include;
3232
this.originalSelect = res.select;
3333
// Get values in case we want to get a specific value by path or key
34-
const customSelection = this.get(params.get, res.select || res.include);
34+
const customSelection = GQLPrismaSelect.get(
35+
params.get,
36+
res.select || res.include
37+
);
3538
const { include, select } = this.selectOrInclude(customSelection);
3639
this.include = include;
3740
this.select = select;
@@ -102,7 +105,7 @@ export class GQLPrismaSelect<S = any, I = any> {
102105
return res;
103106
}
104107

105-
private get(_path?: string | string[], _obj?: any) {
108+
public static get(_path?: string | string[], _obj?: any) {
106109
if (!_path?.length) {
107110
return _obj;
108111
}
@@ -112,6 +115,6 @@ export class GQLPrismaSelect<S = any, I = any> {
112115
}
113116
const [key, ...rest] = path;
114117
const obj = _obj[key];
115-
return this.get(rest, obj.select || obj.include);
118+
return GQLPrismaSelect.get(rest, obj.select || obj.include);
116119
}
117120
}

0 commit comments

Comments
 (0)