|
1 | 1 | /* @flow */ |
2 | 2 |
|
3 | | -import { GraphQLInt, GraphQLInputObjectType } from 'graphql/type'; |
| 3 | +import { GraphQLInt, GraphQLInputObjectType, GraphQLNonNull } from 'graphql/type'; |
4 | 4 | import { toDottedObject } from '../../utils'; |
5 | 5 | import type { |
6 | 6 | GraphQLFieldConfigArgumentMap, |
7 | 7 | ExtendedResolveParams, |
8 | 8 | } from '../../definition'; |
9 | 9 |
|
10 | | -export const filterHelperArgsGen = (): GraphQLFieldConfigArgumentMap => { |
| 10 | +export type filterHelperArgsGenOpts = { |
| 11 | + filterTypeName: string, |
| 12 | + isRequired?: boolean, |
| 13 | +}; |
| 14 | + |
| 15 | +export const filterHelperArgsGen = ( |
| 16 | + model: MongooseModelT, |
| 17 | + opts: filterHelperArgsGenOpts, |
| 18 | +): GraphQLFieldConfigArgumentMap => { |
| 19 | + if (!opts.filterTypeName) { |
| 20 | + throw new Error('You should provide `filterTypeName` in options.'); |
| 21 | + } |
| 22 | + |
| 23 | + const filterType = new GraphQLInputObjectType({ |
| 24 | + name: opts.filterTypeName, |
| 25 | + fields: { |
| 26 | + age: { |
| 27 | + name: 'age', |
| 28 | + type: GraphQLInt, // TODO just mock, should be changed in future |
| 29 | + }, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
11 | 33 | return { |
12 | 34 | filter: { |
13 | 35 | name: 'filter', |
14 | | - type: new GraphQLInputObjectType({ |
15 | | - name: 'InputFilterSomeName', |
16 | | - fields: { |
17 | | - age: { |
18 | | - name: 'age', |
19 | | - type: GraphQLInt, // TODO just mock, should be changed in future |
20 | | - }, |
21 | | - }, |
22 | | - }), |
| 36 | + type: opts.isRequired ? new GraphQLNonNull(filterType): filterType, |
23 | 37 | description: 'Filter by indexed fields', |
24 | 38 | }, |
25 | 39 | }; |
|
0 commit comments