Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
This is a plugin for graphql-compose, which derives GraphQLType from your mongoose model. Also derives bunch of internal GraphQL Types. Provide all CRUD resolvers, including graphql connection, also provided basic search via operators ($lt, $gt and so on).
Release Notes for v9.0.0 contains a lot of improvements. It's strongly recommended for reading before upgrading from v8.
connection(opts?: ConnectionResolverOpts)count(opts?: CountResolverOpts)createMany(opts?: CreateManyResolverOpts)createOne(opts?: CreateOneResolverOpts)dataLoader(opts?: DataLoaderResolverOpts)dataLoaderMany(opts?: DataLoaderManyResolverOpts)findById(opts?: FindByIdResolverOpts)findByIds(opts?: FindByIdsResolverOpts)findMany(opts?: FindManyResolverOpts)findOne(opts?: FindOneResolverOpts)pagination(opts?: PaginationResolverOpts)removeById(opts?: RemoveByIdResolverOpts)removeMany(opts?: RemoveManyResolverOpts)removeOne(opts?: RemoveOneResolverOpts)updateById(opts?: UpdateByIdResolverOpts)updateMany(opts?: UpdateManyResolverOpts)updateOne(opts?: UpdateOneResolverOpts)_id field and you don't need it?npm install graphql graphql-compose mongoose graphql-compose-mongoose --saveModules graphql, graphql-compose, mongoose are in peerDependencies, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
Viktor Kjartansson created a quite solid intro for graphql-compose-mongoose in comparison with graphql-tools:
(23 mins)
Live demo:
Source code:
Small explanation for variables naming:
UserSchema - this is a mongoose schemaUser - this is a mongoose modelUserTC - this is a ObjectTypeComposer instance for User. ObjectTypeComposer has GraphQLObjectType inside, available via method UserTC.getType()....TC means that this is ObjectTypeComposer instance, ...ITC - InputTypeComposer, ...ETC - EnumTypeComposer.…That's all! You think that is to much code? 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.
Variable Namings
...DTC - Suffix for a DiscriminatorTypeComposer instance, which is also an instance of ObjectTypeComposer. All fields and Relations manipulations on this instance affects all registered discriminators and the Discriminator Interface.…composeMongoose customization optionsWhen you converting mongoose model const UserTC = composeMongoose(User, opts: ComposeMongooseOpts); you may tune every piece of future derived types – setup name and description for the main type, remove fields or leave only desired fields.
…This is opts.inputType options for default InputTypeObject which will be provided to all resolvers for filter and input args.
type TypeConverterInputTypeOpts = {
/**
* What should be input type name.
* By default: baseTypeName + 'Input'
*/
name?: string;
/**
* Provide arbitrary description for generated type.
*/
description?: string;
/**
* You can leave only whitelisted fields in type via this option.
* Any other fields will be removed.
*/
onlyFields?: string[];
/**
* You can remove some fields from type via this option.
*/
removeFields?: string[];
/**
* This option makes provided fieldNames as required
*/
requiredFields?: string[];
};When you are creating resolvers from mongooseResolvers factory, you may provide customizationOptions to it:
UserTC.mongooseResolvers.findMany(opts);connection(opts?: ConnectionResolverOpts)type ConnectionResolverOpts = {
/** See below **/
sort?: ConnectionSortMapOpts;
name?: string;
defaultLimit?: number | undefined;
edgeTypeName?: string;
edgeFields?: ObjectTypeComposerFieldConfigMap;
/** See below **/
countOpts?: CountResolverOpts;
/** See below **/
findManyOpts?: FindManyResolverOpts;
}The countOpts and findManyOpts props would be used to customize the internally created findMany and count resolver factories used by the connection resolver.
If not provided the default configuration for each of the resolver factories is assumed.
The sort prop is optional. When provided it is used to customize the sorting behaviour of the connection. When not provided, the sorting configuration is derived from the existing indexes on the model.
Please refer to the documentation of the graphql-compose-connection plugin for more details on the sorting customization parameter.
count(opts?: CountResolverOpts)interface CountResolverOpts {
/** If you want to generate different resolvers you may avoid Type name collision by adding a suffix to type names */
suffix?: string;
/** Customize input-type for `filter` argument. If `false` then arg will be removed. */
filter?: FilterHelperArgsOpts | false;
}createMany(opts?: CreateManyResolverOpts)interface CreateManyResolverOpts {
/** If you want to generate different resolvers you may avoid Type name collision by adding a suffix to type names */
suffix?: string;
/** Customize input-type for `records` argument. */
records?: RecordHelperArgsOpts;
/** Customize payload.recordIds field. If false, then this field will be removed. */
recordIds?: PayloadRecordIdsHelperOpts | false;
/** Customize payload.error field. If true, then this field will be removed. */
disableErrorField?: boolean;
}createOne(opts?: CreateOneResolverOpts)interface CreateOneResolverOpts {
/** If you want to generate different resolvers you may avoid Type name collision by adding a suffix to type names */
suffix?: string;
/** Customize input-type for `record` argument */
record?: RecordHelperArgsOpts;
/** Customize payload.recordId field. If false, then this field will be removed. */
recordId?: PayloadRecordIdHelperOpts | false;
/** Customize payload.error field. If true, then this field will be removed. */
disableErrorField?: boolean;
}dataLoader(opts?: DataLoaderResolverOpts)interface DataLoaderResolverOpts {
/**
* Enabling the lean option tells Mongoose to skip instantiating
* a full Mongoose document and just give you the plain JavaScript objects.
* Documents are much heavier than vanilla JavaScript objects,
* because they have a lot of internal state for change tracking.
* The downside of enabling lean is that lean docs don't have:
* Default values
* Getters and setters
* Virtuals
* Read more about `lean`: https://mongoosejs.com/docs/tutorials/lean.html
*/
lean?: boolean;
}dataLoaderMany(opts?: DataLoaderManyResolverOpts)interface DataLoaderManyResolverOpts {
/**
* Enabling the lean option tells Mongoose to skip instantiating
* a full Mongoose document and just give you the plain JavaScript objects.
* Documents are much heavier than vanilla JavaScript objects,
* because they have a lot of internal state for change tracking.
* The downside of enabling lean is that lean docs don't have:
* Default values
* Getters and setters
* Virtuals
* Read more about `lean`: https://mongoosejs.com/docs/tutorials/lean.html
*/
lean?: boolean;
}findById(opts?: FindByIdResolverOpts)interface FindByIdResolverOpts {
/**
* Enabling the lean option tells Mongoose to skip instantiating
* a full Mongoose document and just give you the plain JavaScript objects.
* Documents are much heavier than vanilla JavaScript objects,
* because they have a lot of internal state for change tracking.
* The downside of enabling lean is that lean docs don't have:
* Default values
* Getters and setters
* Virtuals
* Read more about `lean`: https://mongoosejs.com/docs/tutorials/lean.html
*/
lean?: boolean;
}findByIds(opts?: FindByIdsResolverOpts)…findMany(opts?: FindManyResolverOpts)…findOne(opts?: FindOneResolverOpts)…pagination(opts?: PaginationResolverOpts)interface PaginationResolverOpts {
name?: string;
perPage?: number;
countOpts?: CountResolverOpts;
findManyOpts?: FindManyResolverOpts;
}removeById(opts?: RemoveByIdResolverOpts)interface RemoveByIdResolverOpts {
/** If you want to generate different resolvers you may avoid Type name collision by adding a suffix to type names */
suffix?: string;
/** Customize payload.recordId field. If false, then this field will be removed. */
recordId?: PayloadRecordIdHelperOpts | false;
/** Customize payload.error field. If true, then this field will be removed. */
disableErrorField?: boolean;
}removeMany(opts?: RemoveManyResolverOpts)interface RemoveManyResolverOpts {
/** IfNo open issues yet, or sync has not completed.