{"id":28714373,"url":"https://github.com/graphql-compose/graphql-compose-modules","last_synced_at":"2025-06-15T01:07:03.128Z","repository":{"id":36335624,"uuid":"222890400","full_name":"graphql-compose/graphql-compose-modules","owner":"graphql-compose","description":null,"archived":false,"fork":false,"pushed_at":"2023-05-25T06:31:37.000Z","size":1525,"stargazers_count":15,"open_issues_count":16,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-01T08:13:43.518Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/graphql-compose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["nodkz"],"patreon":null,"open_collective":"graphql-compose","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-11-20T08:41:24.000Z","updated_at":"2021-12-21T19:24:06.000Z","dependencies_parsed_at":"2024-01-11T22:00:06.892Z","dependency_job_id":"cf31a709-4340-4085-acfd-840e354a2a20","html_url":"https://github.com/graphql-compose/graphql-compose-modules","commit_stats":{"total_commits":69,"total_committers":3,"mean_commits":23.0,"dds":0.05797101449275366,"last_synced_commit":"5960c0b76bb16b00cda704d49124de27d143ff88"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-modules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-modules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-modules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-modules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-compose","download_url":"https://codeload.github.com/graphql-compose/graphql-compose-modules/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-compose%2Fgraphql-compose-modules/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259280659,"owners_count":22833432,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2025-06-15T01:07:02.377Z","updated_at":"2025-06-15T01:07:03.081Z","avatar_url":"https://github.com/graphql-compose.png","language":"TypeScript","funding_links":["https://github.com/sponsors/nodkz","https://opencollective.com/graphql-compose"],"categories":[],"sub_categories":[],"readme":"# graphql-compose-modules\n\n[![npm](https://img.shields.io/npm/v/graphql-compose-modules.svg)](https://www.npmjs.com/package/graphql-compose-modules)\n[![Codecov coverage](https://img.shields.io/codecov/c/github/graphql-compose/graphql-compose-modules.svg)](https://codecov.io/github/graphql-compose/graphql-compose-modules)\n[![Github Actions](https://github.com/graphql-compose/graphql-compose-modules/workflows/Build%20CI/badge.svg)](https://github.com/graphql-compose/graphql-compose-modules/actions)\n[![Trends](https://img.shields.io/npm/dt/graphql-compose-modules.svg)](http://www.npmtrends.com/graphql-compose-modules)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n![TypeScript compatible](https://img.shields.io/badge/typescript-compatible-brightgreen.svg)\n[![Backers on Open Collective](https://opencollective.com/graphql-compose/backers/badge.svg)](#backers)\n[![Sponsors on Open Collective](https://opencollective.com/graphql-compose/sponsors/badge.svg)](#sponsors)\n\nThis is a toolkit for creating big GraphQL schemas with code-first approach in JavaScript.\n\n## Quick demo\n\nYou may find a simple GraphQL server example in the following folder: [examples/simple](./examples/simple).\n\n## GraphQL schema entrypoints from a file structure\n\nWhen you are using code-first approach in GraphQL Schema construction you may face problem when you cannot understand which entrypoints your schema has. And where exactly the code is placed which serve this or that entrypoint.\n\n![overview](./docs/diagrams/overview.drawio.svg)\n\n`graphql-compose-modules` uses a file-system based schema entrypoint definition (something like NextJS does with its pages concept for routing). You just create folder `schema/` and put inside it the following sub-folders (root directories): `query`, `mutation` and `subscription`. Inside these folders you may put `.js` or `.ts` files with FieldConfigs which describes entrypoints. Assume you create the following directory structure:\n\n```bash\nschema/\n  query/\n    articleById.ts\n    articlesList.ts\n  mutation/\n    createArticle.ts\n    updateArticle.ts\n    removeArticle.ts\n  subscription/\n    onArticleChange.ts\n```\n\nWith this directory structure `graphql-compose-modules` will use file names as field names for your root types and you get the following GraphQL schema:\n\n```graphql\ntype Query {\n  articleById: ...\n  articlesList: ...\n}\n\ntype Mutation {\n  createArticle: ...\n  updateArticle: ...\n  removeArticle: ...\n}\n\ntype Subscription {\n  onArticleChange: ...\n}\n```\n\nIf you want rename field `articlesList` to `articles` in your schema just rename `articlesList.ts` file. If you want to add a new field to Schema – just add a new file to `Query`, `Mutation`, `Subscription` folders. **This simple approach helps you understand entrypoints of your schema without launching the GraphQL server – what you see in folders that you get in GraphQL Schema**.\n\n## Describing Entrypoints in files\n\nEvery Entrypoint (FieldConfig definition) is described in a separate file. This file contains information about input args, output type, resolve function and additional fields like description, deprecationReason, extensions. As an example let's create `schema/Query/sum.ts` and put inside the following content:\n\n```ts\nexport default {\n  type: 'Int!',\n  args: {\n    a: 'Int!',\n    b: 'Int!',\n  },\n  resolve: (source, args, context, info) =\u003e {\n    return args.a + args.b;\n  },\n  description: 'This method sums two numbers',\n  deprecationReason: 'This method is deprecated and will be removed soon.',\n  extensions: {\n    someExtraParam: 'Can be used for AST transformers',\n  },\n};\n```\n\nIf you are familiar with [graphql-js FieldConfig definition](https://graphql.org/graphql-js/type/#examples) then you may notice that `type` \u0026 `args` properties are defined in SDL format. This syntax sugar is provided by [graphql-compose](https://github.com/graphql-compose/graphql-compose#examples) package.\n\n## Entrypoints with namespaces for big schemas\n\nIf your GraphQL Schema has a lot of entrypoints you may create sub-folders for grouping them under Namespaces:\n\n```bash\nschema/\n  query/\n    articles/\n      byId.ts\n      list.ts\n    ...\n  mutation/\n    articles/\n      create.ts\n      update.ts\n      remove.ts\n    ...\n```\n\nWith such structure you will get the following schema – namespace types `QueryArticles` \u0026 `MutationArticles` are created automatically:\n\n```graphql\ntype Query {\n  articles: QueryArticles\n}\n\ntype Mutation {\n  articles: MutationArticles\n}\n\ntype QueryArticles {\n  byId: ...\n  list: ...\n}\n\ntype MutationArticles {\n  create: ...\n  update: ...\n  remove: ...\n}\n```\n\nYou may use namespaces (sub-folders) for `Query` \u0026 `Mutation` and all servers supports this feature. But for `Subscription` most current server implementations (eg. [apollo-server](https://www.apollographql.com/docs/apollo-server/data/subscriptions/)) does not support this yet.\n\n## GraphQLSchema construction\n\nIn `schema` folder create a file `index.ts` with the following content which traverses `query`, `mutation`, `subscription` folders and creates a `GraphQLSchema` instance for you:\n\n```ts\nimport { buildSchema } from 'graphql-compose-modules';\n\nexport const schema = buildSchema(module);\n```\n\nAfter that you may create a GraphQL server:\n\n```ts\nimport { ApolloServer } from 'apollo-server';\nimport { schema } from './schema';\n\nconst server = new ApolloServer({ schema });\n\nserver.listen().then(({ url }) =\u003e {\n  console.log(`🚀 Server ready at ${url}`);\n});\n```\n\n## Advanced GraphQLSchema construction\n\nIf you want transform AST of entrypoints (e.g. for adding authorization, logging, tracing) and for merging with another schemas distributed via npm packages – you may use the following advanced way for schema construction:\n\n```ts\nimport { directoryToAst, astToSchema, astMerge } from 'graphql-compose-modules';\nimport { addQueryToMutations } from './transformers/addQueryToMutations';\nimport { remoteServiceAST } from '@internal/some-service';\n\n// traverse `query`, `mutation`, `subscription` folders placed near this module\nlet ast = directoryToAst(module);\n\n// apply transformer which uses astVisitor() method under the hood\naddQueryToMutations(ast);\n\n// merge with other ASTs distributed via npm packages\nast = astMerge(ast, remoteServiceAST);\n\n// construct SchemaComposer\nconst sc = astToSchema(ast);\n\n// construct GraphQLSchema instance and export it\nexport const schema = sc.buildSchema();\n```\n\n## Writing own transformer for entrypoints\n\nFor writing your own transformers you need to use `astVisitor()` method. For instance let's implement `addQueryToMutations` transformer which adds `query: Query` field to all your mutations:\n\n```ts\nimport { astVisitor, VISITOR_SKIP_CHILDREN, AstRootNode } from 'graphql-compose-modules';\nimport { ObjectTypeComposer, SchemaComposer } from 'graphql-compose';\n\nexport function addQueryToMutations(\n  ast: AstRootNode,\n  schemaComposer: SchemaComposer\u003cany\u003e\n): void {\n  astVisitor(ast, schemaComposer, {\n    // skip `query` \u0026 `subscriptions` root types\n    ROOT_TYPE: (info) =\u003e {\n      if (!info.isMutation) {\n        return VISITOR_SKIP_CHILDREN;\n      }\n      return;\n    },\n    // for every file in `mutation` folder try to add `query` field if it does not exists\n    FILE: (info) =\u003e {\n      // get FieldConfig from loaded file in `schema` folder\n      const fieldConfig = info.fieldConfig || {};\n\n      // if `resolve` method does not exist then skip this transformation\n      const next = fieldConfig.resolve;\n      if (!next) return;\n\n      // if output type isn't an object then skip this transformation\n      if (!info.isOutputTypeIsObject()) return;\n\n      const outputTC = info.getOutputUnwrappedOTC();\n      if (outputTC.hasField('query')) return;\n      outputTC.setField('query', {\n        description: 'Sub-query which have to be executed after mutation.',\n        type: schemaComposer.Query,\n      });\n\n      fieldConfig.resolve = async (s: any, args: any, context: any, i: any) =\u003e {\n        const result = await next(s, args, context, i);\n        return {\n          query: {},\n          ...result,\n        };\n      };\n    },\n  });\n}\n```\n\n## API\n\n### Main API method:\n\n- `buildSchema(module: NodeModule, opts: BuildOptions): GraphQLSchema` – use this method for creating graphql schema from directory\n\n### Advanced API methods:\n\nThe following methods help to use schema composition, applying middlewares and schema transformation via visitor pattern:\n\n![overview](./docs/diagrams/ast-transformation.drawio.svg)\n\n- `directoryToAst(module: NodeModule, options: DirectoryToAstOptions): AstRootNode` – traverses directories and construct AST for your graphql entrypoints\n- `astToSchema(ast: AstRootNode, opts: AstToSchemaOptions): SchemaComposer` – converts AST to GraphQL Schema\n- `astMerge(...asts: Array\u003cAstRootNode\u003e): AstRootNode` – combines several ASTs to one AST (helps compose several graphql schemas which may be distributed via npm packages)\n- `astVisitor(ast: AstRootNode, schemaComposer: SchemaComposer, visitor: AstVisitor): void` – modify AST via visitor pattern. This method is used for construction of your AST transformers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-compose%2Fgraphql-compose-modules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-compose%2Fgraphql-compose-modules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-compose%2Fgraphql-compose-modules/lists"}