Skip to content

Commit 0aa9b99

Browse files
committed
doc(README.md): add discriminator example and use case
1 parent e8a6b1c commit 0aa9b99

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,129 @@ You think that is to much code?
122122
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.
123123

124124

125+
### Working with Mongoose Collection Level Discriminators
126+
Variable Namings
127+
* `...DTC` - Suffix for a `DiscriminatorTypeComposer` instance, which is also an instance of `TypeComposer`. All fields and Relations manipulations on this instance affects all registered discriminators and the Discriminator Interface.
128+
129+
```js
130+
import mongoose from 'mongoose';
131+
import { schemaComposer } from 'graphql-composer';
132+
import { composeWithMongooseDiscriminators } from 'graphql-compose-mongoose';
133+
134+
// pick a discriminatorKey
135+
const DKey = 'type';
136+
137+
const enumCharacterType = {
138+
PERSON: 'Person',
139+
DROID: 'Droid',
140+
};
141+
142+
// DEFINE BASE SCHEMA
143+
const CharacterSchema = new mongoose.Schema({
144+
// _id: field...
145+
name: String,
146+
147+
type: {
148+
type: String,
149+
require: true,
150+
enum: (Object.keys(enumCharacterType): Array<string>),
151+
},
152+
153+
friends: [String], // another Character
154+
appearsIn: [String], // movie
155+
});
156+
157+
// DEFINE DISCRIMINATOR SCHEMAS
158+
const DroidSchema = new Schema({
159+
makeDate: Date,
160+
modelNumber: Number,
161+
primaryFunction: [String],
162+
});
163+
164+
const PersonSchema = new Schema({
165+
dob: Number,
166+
starShips: [String],
167+
totalCredits: Number,
168+
});
169+
170+
// set discriminator Key
171+
CharacterSchema.set('discriminatorKey', DKey);
172+
173+
// create base Model
174+
const CharacterModel = mongoose.model('Character', CharacterSchema);
175+
176+
// create mongoose discriminator models
177+
const DroidModel = CharacterModel.discriminator(enumCharacterType.DROID, DroidSchema);
178+
const PersonModel = CharacterModel.discriminator(enumCharacterType.PERSON, PersonSchema);
179+
180+
// create DiscriminatorTypeComposer
181+
// discrimatorOptions
182+
const baseOptions = {
183+
customizationOptions: { // regular TypeConverterOptions, passed to composeWithMongoose
184+
fields: {
185+
remove: ['friends'],
186+
}
187+
}
188+
}
189+
const CharacterDTC = composeWithMongooseDiscriminators(CharacterModel, baseOptions);
190+
191+
// create Discriminator Types
192+
const droidTypeConverterOptions = { // this options will be merged with baseOptions -> customisationsOptions
193+
fields: {
194+
remove: ['makeDate'],
195+
}
196+
};
197+
const DroidTC = CharacterDTC.discriminator(DroidModel, droidTypeConverterOptions);
198+
const PersonTC = CharacterDTC.discriminator(PersonModel); // baseOptions -> customisationsOptions applied
199+
200+
// You may now use CharacterDTC to add fields to all Discriminators
201+
// Use DroidTC, `PersonTC as any other TypeComposer.
202+
schemaComposer.rootMutation().addFields({
203+
droidCreate: DroidTC.getResolver('createOne'),
204+
personCreate: PersonTC.getResolver('createOne'),
205+
});
206+
207+
const schema = schemaComposer.buildSchema();
208+
209+
describe('createOne', () => {
210+
it('should create child document without specifying DKey', async () => {
211+
const res = await graphql.graphql(
212+
schema,
213+
`mutation CreateCharacters {
214+
droidCreate(record: {name: "Queue XL", modelNumber: 360 }) {
215+
record {
216+
__typename
217+
type
218+
name
219+
modelNumber
220+
}
221+
}
222+
223+
personCreate(record: {name: "mernxl", dob: 57275272}) {
224+
record {
225+
__typename
226+
type
227+
name
228+
dob
229+
}
230+
}
231+
}`
232+
);
233+
234+
expect(res).toEqual({
235+
data: {
236+
droidCreate: {
237+
record: { __typename: 'Droid', type: 'Droid', name: 'Queue XL', modelNumber: 360 },
238+
},
239+
personCreate: {
240+
record: { __typename: 'Person', type: 'Person', name: 'mernxl', dob: 57275272 },
241+
},
242+
},
243+
});
244+
});
245+
});
246+
```
247+
125248
## FAQ
126249

127250
### Can I get generated vanilla GraphQL types?

0 commit comments

Comments
 (0)