Skip to content

Commit d85b02b

Browse files
committed
Add Customization options to read
1 parent 0be4b64 commit d85b02b

File tree

1 file changed

+126
-6
lines changed

1 file changed

+126
-6
lines changed

README.md

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,27 @@ const UserSchema = new Schema({
3939
},
4040
});
4141
const UserModel = mongoose.model('UserModel', UserSchema);
42-
export default UserModel;
4342

4443

4544

4645
// STEP 2: CONVERT MONGOOSE MODEL TO GraphQL PIECES
47-
const typeComposer = mongooseToTypeComposer(UserModel);
46+
const customizationOptions = {}; // left it empty for simplicity
47+
const typeComposer = mongooseToTypeComposer(UserModel, customizationOptions);
4848
// get list of 12 Resolvers (findById, updateMany and others)
4949
const resolvers = typeComposer.getResolvers();
5050

5151
// typeComposer from (graphql-compose) provide bunch if useful methods
52-
// for modifying GraphQL Types (eg. add/remove fields, relate with other types)
52+
// for modifying GraphQL Types (eg. add/remove fields, relate with other types,
53+
// restrict access due context).
54+
// Also will be available such plugins
55+
// - GraphQL Connection Type
56+
// - Relay Type Wrapper (adds clientMutationId, node interface, generate global ids)
57+
// - DataLoader Wrapper and much much more
5358

5459

5560

5661
// STEP 3: CREATE CRAZY GraphQL SCHEMA WITH ALL CRUD USER OPERATIONS
62+
// via graphql-compose it will be much much easier, with less typing
5763
const graphqlSchema = new GraphQLSchema({
5864
query: new GraphQLObjectType({
5965
name: 'RootQuery',
@@ -79,10 +85,124 @@ const graphqlSchema = new GraphQLSchema({
7985
}),
8086
});
8187
```
88+
That's all!
89+
You think that is to much code?
90+
I don't think so, because by default internally was created about 30 graphql types (for input, sorting, filtering). So you will need much-much more lines of code to implement all this CRUD operations by hands.
8291

83-
mongooseToTypeComposer options
84-
==============================
85-
will be described
92+
93+
Customization options
94+
=====================
95+
When we convert model `const typeComposer = mongooseToTypeComposer(UserModel, customizationOptions);` you may tune every piece of future derived types and resolvers.
96+
97+
### Here is flow typed definition of this options:
98+
99+
This is top level of customization options. Here you setup name and description for main type, remove fields or leave only desired fields from mongoose model.
100+
```js
101+
export type typeConverterOpts = {
102+
name?: string,
103+
description?: string,
104+
fields?: {
105+
only?: string[],
106+
remove?: string[],
107+
},
108+
inputType?: typeConverterInputTypeOpts,
109+
resolvers?: false | typeConverterResolversOpts,
110+
};
111+
```
112+
113+
This is `opts.inputType` level of options for default InputTypeObject which will be provided to all resolvers for `filter` and `input` args.
114+
```js
115+
export type typeConverterInputTypeOpts = {
116+
name?: string,
117+
description?: string,
118+
fields?: {
119+
only?: string[],
120+
remove?: string[],
121+
required?: string[]
122+
},
123+
};
124+
```
125+
126+
This is `opts.resolvers` level of options.
127+
If you set option to `false` it will disable resolver or some of its input args.
128+
Every resolver's arg has it own options. They described below.
129+
```js
130+
export type typeConverterResolversOpts = {
131+
findById?: false,
132+
findByIds?: false | {
133+
limit?: limitHelperArgsOpts | false,
134+
sort?: sortHelperArgsOpts | false,
135+
},
136+
findOne?: false | {
137+
filter?: filterHelperArgsOpts | false,
138+
sort?: sortHelperArgsOpts | false,
139+
skip?: false,
140+
},
141+
findMany?: false | {
142+
filter?: filterHelperArgsOpts | false,
143+
sort?: sortHelperArgsOpts | false,
144+
limit?: limitHelperArgsOpts | false,
145+
skip?: false,
146+
},
147+
updateById?: false | {
148+
input?: inputHelperArgsOpts | false,
149+
},
150+
updateOne?: false | {
151+
input?: inputHelperArgsOpts | false,
152+
filter?: filterHelperArgsOpts | false,
153+
sort?: sortHelperArgsOpts | false,
154+
skip?: false,
155+
},
156+
updateMany?: false | {
157+
input?: inputHelperArgsOpts | false,
158+
filter?: filterHelperArgsOpts | false,
159+
sort?: sortHelperArgsOpts | false,
160+
limit?: limitHelperArgsOpts | false,
161+
skip?: false,
162+
},
163+
removeById?: false,
164+
removeOne?: false | {
165+
filter?: filterHelperArgsOpts | false,
166+
sort?: sortHelperArgsOpts | false,
167+
},
168+
removeMany?: false | {
169+
filter?: filterHelperArgsOpts | false,
170+
},
171+
createOne?: false | {
172+
input?: inputHelperArgsOpts | false,
173+
},
174+
count?: false | {
175+
filter?: filterHelperArgsOpts | false,
176+
},
177+
};
178+
```
179+
180+
This is `opts.resolvers.[resolverName].[filter|sort|input|limit]` level of options.
181+
You may tune every resolver's args independently as you wish.
182+
Here you may setup every argument and override some fields from the default input object type, described above in `opts.inputType`.
183+
```js
184+
export type filterHelperArgsOpts = {
185+
filterTypeName?: string, // type name for `filter`
186+
isRequired?: boolean, // set `filter` arg as required (wraps in GraphQLNonNull)
187+
onlyIndexed?: boolean, // leave only that fields, which is indexed in mongodb
188+
requiredFields?: string | string[], // provide fieldNames, that should be required
189+
};
190+
191+
export type sortHelperArgsOpts = {
192+
sortTypeName?: string, // type name for `sort`
193+
};
194+
195+
export type inputHelperArgsOpts = {
196+
inputTypeName?: string, // type name for `input`
197+
isRequired?: boolean, // set `input` arg as required (wraps in GraphQLNonNull)
198+
removeFields?: string[], // provide fieldNames, that should be removed
199+
requiredFields?: string[], // provide fieldNames, that should be required
200+
};
201+
202+
export type limitHelperArgsOpts = {
203+
defaultValue?: number, // set your default limit, if it not provided in query (default: 1000)
204+
};
205+
```
86206

87207

88208
TODO

0 commit comments

Comments
 (0)