Releases: graphql-compose/graphql-compose-mongoose
Releases · graphql-compose/graphql-compose-mongoose
v4.5.0
4.5.0 (2018-08-09)
Features
- TypeScript: added definition files (thanks @toverux) (723ae7c)
- FieldConverter: now support deeply nested arrays (504aec0)
For field someDeepField
will be returned type [SomeDeepFieldPeriods]
instead of JSON
:
const UserSchema = new Schema({
someDeepField: {
periods: [{ from: Number, to: Number }],
},
});
v4.4.2
v4.4.1
v4.4.0
4.4.0 (2018-07-17)
Features
- discriminators: add composeWithMongooseDiscriminators (df8d4f4) Thanks a lot @mernxl for amazing work! 👍
How to use
import mongoose from 'mongoose';
import { schemaComposer } from 'graphql-compose';
import { composeWithMongooseDiscriminators } from 'graphql-compose-mongoose';
// pick a discriminatorKey
const DKey = 'type';
const enumCharacterType = {
PERSON: 'Person',
DROID: 'Droid',
};
// DEFINE BASE SCHEMA
const CharacterSchema = new mongoose.Schema({
// _id: field...
type: {
type: String,
require: true,
enum: (Object.keys(enumCharacterType): Array<string>),
description: 'Character type Droid or Person',
},
name: String,
height: Number,
mass: Number,
films: [String],
});
// DEFINE DISCRIMINATOR SCHEMAS
const DroidSchema = new mongoose.Schema({
makeDate: String,
primaryFunction: [String],
});
const PersonSchema = new mongoose.Schema({
gender: String,
hairColor: String,
starships: [String],
});
// set discriminator Key
CharacterSchema.set('discriminatorKey', DKey);
// create base Model
const CharacterModel = mongoose.model('Character', CharacterSchema);
// create mongoose discriminator models
const DroidModel = CharacterModel.discriminator(enumCharacterType.DROID, DroidSchema);
const PersonModel = CharacterModel.discriminator(enumCharacterType.PERSON, PersonSchema);
// create DiscriminatorTypeComposer
const baseOptions = { // regular TypeConverterOptions, passed to composeWithMongoose
fields: {
remove: ['friends'],
}
}
const CharacterDTC = composeWithMongooseDiscriminators(CharacterModel, baseOptions);
// create Discriminator Types
const droidTypeConverterOptions = { // this options will be merged with baseOptions -> customisationsOptions
fields: {
remove: ['makeDate'],
}
};
const DroidTC = CharacterDTC.discriminator(DroidModel, droidTypeConverterOptions);
const PersonTC = CharacterDTC.discriminator(PersonModel); // baseOptions -> customisationsOptions applied
// You may now use CharacterDTC to add fields to all Discriminators
// Use DroidTC, `PersonTC as any other TypeComposer.
schemaComposer.Mutation.addFields({
droidCreate: DroidTC.getResolver('createOne'),
personCreate: PersonTC.getResolver('createOne'),
});
const schema = schemaComposer.buildSchema();
Live demo
v4.3.0
v4.2.1
v4.2.0
v4.1.1
v4.1.0
4.1.0 (2018-04-20)
Features
- MongoID: allow to override build-in MongoID type (720f0ba)
From new 4.1.0 version you may override build-in MongoID
type by your GraphQL class type implementation (related issue #98):
import { schemaComposer } from 'graphql-compose';
import { composeWithMongoose } from 'graphql-compose-mongoose';
schemaComposer.set('MongoID', YOUR_CLASS_TYPE_IMPLEMENTATION);
const UserTC = composeWithMongoose(UserModel, { schemaComposer });