{"id":22300762,"url":"https://github.com/tsui66/egg-graphql-compose-mongoose","last_synced_at":"2026-04-18T04:32:15.663Z","repository":{"id":42091407,"uuid":"266923479","full_name":"tsui66/egg-graphql-compose-mongoose","owner":"tsui66","description":"🥚Toolkit for generating complex GraphQL schemas in egg.","archived":false,"fork":false,"pushed_at":"2023-01-24T02:43:17.000Z","size":1494,"stargazers_count":1,"open_issues_count":13,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-09-01T19:19:06.561Z","etag":null,"topics":["apollo-graphql","apollo-server","egg","egg-graphql","eggjs","graphql","graphql-compose","graphql-compose-mongoose","mongoose","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tsui66.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-26T02:15:03.000Z","updated_at":"2023-03-09T01:56:09.000Z","dependencies_parsed_at":"2023-01-31T16:01:24.036Z","dependency_job_id":null,"html_url":"https://github.com/tsui66/egg-graphql-compose-mongoose","commit_stats":null,"previous_names":["quinton/egg-graphql-mongoose"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tsui66/egg-graphql-compose-mongoose","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsui66%2Fegg-graphql-compose-mongoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsui66%2Fegg-graphql-compose-mongoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsui66%2Fegg-graphql-compose-mongoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsui66%2Fegg-graphql-compose-mongoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsui66","download_url":"https://codeload.github.com/tsui66/egg-graphql-compose-mongoose/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsui66%2Fegg-graphql-compose-mongoose/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31956858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T00:39:45.007Z","status":"online","status_checked_at":"2026-04-18T02:00:07.018Z","response_time":103,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apollo-graphql","apollo-server","egg","egg-graphql","eggjs","graphql","graphql-compose","graphql-compose-mongoose","mongoose","nodejs"],"created_at":"2024-12-03T18:13:51.665Z","updated_at":"2026-04-18T04:32:15.639Z","avatar_url":"https://github.com/tsui66.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# egg-graphql-mongoose\n\n[![NPM version][npm-image]][npm-url]\n[![build status][action-image]][action-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n![]()\n\n[npm-image]: https://img.shields.io/npm/v/egg-graphql-mongoose.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-graphql-mongoose\n[action-image]: https://github.com/Quinton/egg-graphql-mongoose/workflows/build/badge.svg\n[action-url]: https://github.com/Quinton/egg-graphql-mongoose/actions\n[codecov-image]: https://codecov.io/gh/Quinton/egg-graphql-mongoose/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/Quinton/egg-graphql-mongoose\n[david-image]: https://img.shields.io/david/Quinton/egg-graphql-mongoose.svg?style=flat-square\n[david-url]: https://david-dm.org/Quinton/egg-graphql-mongoose\n[snyk-image]: https://snyk.io/test/npm/egg-graphql-mongoose/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/egg-graphql-mongoose\n[download-image]: https://img.shields.io/npm/dm/egg-graphql-mongoose.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-graphql-mongoose\n\n[GraphQL ](http://facebook.github.io/graphql/)使用 Schema 来描述数据，并通过制定和实现 GraphQL 规范定义了支持 Schema 查询的 DSQL （Domain Specific Query Language，领域特定查询语言，由 FACEBOOK 提出。\n\n![graphql](http://upload-images.jianshu.io/upload_images/551828-8d055caea7562605.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n传统 web 应用通过开发服务给客户端提供接口是很常见的场景。而当需求或数据发生变化时，应用需要修改或者重新创建新的接口。长此以后，会造成服务器代码的不断增长，接口内部逻辑复杂难以维护。而 GraphQL 则通过以下特性解决这个问题：\n\n- 声明式。查询的结果格式由请求方（即客户端）决定而非响应方（即服务器端）决定。你不需要编写很多额外的接口来适配客户端请求\n- 可组合。GraphQL 的查询结构可以自由组合来满足需求。\n- 强类型。每个 GraphQL 查询必须遵循其设定的类型才会被执行。\n\n也就是说，通过以上的三个特性，当需求发生变化，客户端只需要编写能满足新需求的查询结构，如果服务端能提供的数据满足需求，服务端代码几乎不需要做任何的修改。\n\n目前 egg-graphql-mongoose 已经完全支持在 egg 中使用 GraphQL 查询语法，可直接查看文末参考链接，下文为插件设计。\n\n## 技术选型\n\n我们会使用 [graphql-compose-mongoose](https://github.com/graphql-compose/graphql-compose-mongoose) 扩展为插件；配合 eggjs 完成 GraphQL 服务的搭建。 GraphQL Tools 建立了一种 GraphQL-first 的开发哲学，主要体现在以下三个方面：\n\n- Mongoose model 即 GraphQLType，内置 CRUD resolvers。\n- 兼容使用官方的 GraphQL schema 进行编程。 GraphQL Tools 提供工具，让你可以书写标准的 GraphQL schema，并完全支持里面的特性。\n- 为很多特殊场景提供标准解决方案。最大限度标准化 GraphQL 应用。\n\n这些我们都会集成到 [egg-graphql-mongoose](https://github.com/Quinton/egg-graphql-mongoose) 插件中。\n\n- graphql-compose-mongoose 官方介绍\n  \u003e This is a plugin for graphql-compose, which derives GraphQLType from your mongoose model. Also derives bunch of internal GraphQL Types. Provide all CRUD \u003e resolvers, including graphql connection, also provided basic search via operators ($lt, $gt and so on).\n\n### 安装与配置\n\n安装对应的依赖 [egg-graphql-mongoose] ：\n\n```bash\n$ npm i --save egg-graphql-mongoose\n```\n\n开启插件：\n\n```js\n// config/plugin.js\nexports.graphql = {\n  enable: true,\n  package: 'egg-graphql-mongoose',\n};\n```\n\n在 `config/config.${env}.js` 配置提供 graphql 的路由。\n\n```js\n// config/config.${env}.js\nexports.graphql = {\n  router: '/graphql',\n  // 是否加载到 app 上，默认开启\n  app: true,\n  // 是否加载到 agent 上，默认关闭\n  agent: false,\n  // 是否加载开发者工具 graphiql, 默认开启。路由同 router 字段。使用浏览器打开该可见。\n  graphiql: true,\n  //是否设置默认的Query和Mutation, 默认关闭\n  defaultEmptySchema:true,\n  plugins: [{\n    requestDidStart(requestContext) {\n      requestContext.context.tracer.ctx.logger.info(`${JSON.stringify(requestContext.request.query, null, 2)}`);\n    },\n  }],\n  // graphQL 路由前的拦截器\n  onPreGraphQL: function* (ctx) {},\n  // 开发工具 graphiQL 路由前的拦截器，建议用于做权限操作(如只提供开发者使用)\n  onPreGraphiQL: function* (ctx) {},\n  // apollo server的透传参数，参考[文档](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#parameters)\n  apolloServerOptions: {\n    rootValue,\n    formatError,\n    formatResponse,\n    mocks,\n    schemaDirectives,\n    introspection,\n    playground,\n    debug,\n    validationRules,\n    tracing,\n    cacheControl,\n    subscriptions,\n    engine,\n    persistedQueries,\n    cors,\n  }\n};\n\n// 添加中间件拦截请求\nexports.middleware = [ 'graphql' ];\n```\n\n## 使用方式\n\n请将 graphql 相关逻辑放到 app/graphql 下，请参考 [egg-graphql-mongoose-example](https://github.com/Quinton/egg-graphql-mongoose-example)。\n\n目录结构如下\n\n```\n.\n├── app\n│   ├── graphql\n│   │   └── User.js\n│   ├── model\n│   │   └── User.js\n│   ├── public\n│   └── router.js\n\n```\n\n## 参考文章\n\n- [graphql 官网](http://facebook.github.io/graphql)\n\n- [如何在 egg 中使用 graphql](https://github.com/Quinton/egg-graphql-mongoose-example)\n\n- [graphql-compose](https://github.com/nodkz/graphql-compose)\n\n- [graphql-compose-mongoose](https://github.com/graphql-compose/graphql-compose-mongoose)\n\n## 协议\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsui66%2Fegg-graphql-compose-mongoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsui66%2Fegg-graphql-compose-mongoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsui66%2Fegg-graphql-compose-mongoose/lists"}