Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

graphql-compose-mongoose

> 后端框架
Open source

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose

706 stars0 likes2 views
WebsiteGitHub

About

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose

graphql-compose-mongoose

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.

  • Installation
  • Intro video
  • Example
    • Working with Mongoose Collection Level Discriminators
  • Customization options
    • composeMongoose customization options
  • Resolvers' customization options
    • 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)
    • Description of common resolvers' options
      • FilterHelperArgsOpts
      • SortHelperArgsOpts
      • RecordHelperArgsOpts
      • LimitHelperArgsOpts
  • FAQ
    • Can I get generated vanilla GraphQL types?
    • How to add custom fields?
    • How to build nesting/relations?
    • Reusing the same mongoose Schema in embedded object fields
    • Access and modify mongoose doc before save
    • How can I restrict access to certain fields or documents?
    • How can I push/pop or add/remove values to arrays?
    • Is it possible to use several schemas?
    • Embedded documents has _id field and you don't need it?
    • Can field name in schema have different name in database?
  • Backers
  • Sponsors
  • License

Installation

bash
npm install graphql graphql-compose mongoose graphql-compose-mongoose --save

Modules 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.

Intro video

Viktor Kjartansson created a quite solid intro for graphql-compose-mongoose in comparison with graphql-tools:

(23 mins)

Example

Live demo:

Source code:

Small explanation for variables naming:

  • UserSchema - this is a mongoose schema
  • User - this is a mongoose model
  • UserTC - this is a ObjectTypeComposer instance for User. ObjectTypeComposer has GraphQLObjectType inside, available via method UserTC.getType().
  • Here and in all other places of code variables suffix ...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.

Working with Mongoose Collection Level Discriminators

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.
…

Customization options

composeMongoose customization options

When 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.

typescript
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[];
};

Resolvers' customization options

When you are creating resolvers from mongooseResolvers factory, you may provide customizationOptions to it:

typescript
UserTC.mongooseResolvers.findMany(opts);

connection(opts?: ConnectionResolverOpts)

typescript
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)

typescript
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)

typescript
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)

typescript
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)

typescript
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)

typescript
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)

typescript
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)

typescript
interface PaginationResolverOpts {
  name?: string;
  perPage?: number;
  countOpts?: CountResolverOpts;
  findManyOpts?: FindManyResolverOpts;
}

removeById(opts?: RemoveByIdResolverOpts)

typescript
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)

typescript
interface RemoveManyResolverOpts {
  /** If

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptgraphqlgraphql-composegraphql-compose-plugingraphql-types

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架