Skip to content

Commit 0e51884

Browse files
committed
docs(Add FAQ):
1 parent bf7d168 commit 0e51884

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,64 @@ You think that is to much code?
9797
I don't think so, because by default internally was created about 55 graphql types (for input, sorting, filtering). So you will need much much more lines of code to implement all these CRUD operations by hands.
9898

9999

100+
FAQ
101+
===
102+
### Can I get generated vanilla GraphQL types?
103+
```js
104+
const UserTC = composeWithMongoose(UserModel);
105+
UserTC.getType(); // returns GraphQLObjectType
106+
UserTC.getInputType(); // return GraphQLInputObjectType, eg. for args
107+
UserTC.get('languages').getType(); // get GraphQLObjectType for nested field
108+
UserTC.get('fieldWithNesting.subNesting').getType(); // get GraphQL type of deep nested field
109+
```
110+
111+
### How to add custom fields?
112+
```js
113+
UserTC.addFields({
114+
lonLat: TypeComposer.create('type LonLat { lon: Float, lat: Float }'),
115+
notice: 'String', // shortand definition
116+
noticeList: { // extended
117+
type: '[String]', // String, Int, Float, Boolean, ID, Json
118+
description: 'Array of notices',
119+
resolve: (source, args, context, info) => 'some value',
120+
},
121+
bio: {
122+
type: GraphQLString,
123+
description: 'Providing vanilla GraphQL type'
124+
}
125+
})
126+
```
127+
128+
### How to build nesting/relations?
129+
Suppose you Model has `friendsIds` field with array of user ids. So let build some relations:
130+
```js
131+
UserTC.addRelation(
132+
'friends',
133+
() => ({
134+
resolver: UserTC.getResolver('findByIds'),
135+
args: { // resolver `findByIds` has `_ids` arg, let provide value to it
136+
_ids: (source) => source.friendsIds,
137+
},
138+
projection: { friendsIds: 1 }, // point fields in source object, which should be fetched from DB
139+
})
140+
);
141+
UserTC.addRelation(
142+
'adultFriendsWithSameGender',
143+
() => ({
144+
resolver: UserTC.get('$findMany'), // shortand for `UserTC.getResolver('findMany')`
145+
args: { // resolver `findMany` has `filter` arg, we may provide mongoose query to it
146+
filter: (source) => ({
147+
_id: { $in: source.friendsIds },
148+
age: { $gt: 21 },
149+
gender: source.gender,
150+
}),
151+
},
152+
projection: { friendsIds: 1, gender: 1 }, // required fields from source object
153+
})
154+
);
155+
```
156+
157+
100158
Customization options
101159
=====================
102160
When we convert model `const UserTC = composeWithMongoose(UserModel, customizationOptions);` you may tune every piece of future derived types and resolvers.

0 commit comments

Comments
 (0)