{"id":13724904,"url":"https://github.com/mcollina/mercurius-auto-schema","last_synced_at":"2025-04-16T01:09:18.452Z","repository":{"id":37530281,"uuid":"377165787","full_name":"mcollina/mercurius-auto-schema","owner":"mcollina","description":null,"archived":false,"fork":false,"pushed_at":"2023-03-06T18:03:36.000Z","size":43,"stargazers_count":43,"open_issues_count":5,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-29T03:21:31.300Z","etag":null,"topics":[],"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/mcollina.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-06-15T13:06:12.000Z","updated_at":"2023-06-17T05:48:21.000Z","dependencies_parsed_at":"2024-01-10T22:09:51.497Z","dependency_job_id":"1b0df47c-0156-4e79-a617-4da71c75e05a","html_url":"https://github.com/mcollina/mercurius-auto-schema","commit_stats":{"total_commits":40,"total_committers":3,"mean_commits":"13.333333333333334","dds":0.25,"last_synced_commit":"0fac864daedf82ca4b17314651a2674c0b53ce56"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmercurius-auto-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmercurius-auto-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmercurius-auto-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmercurius-auto-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcollina","download_url":"https://codeload.github.com/mcollina/mercurius-auto-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248705787,"owners_count":21148590,"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":"2024-08-03T01:02:06.204Z","updated_at":"2025-04-16T01:09:18.427Z","avatar_url":"https://github.com/mcollina.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# mercurius-auto-schema\n\n`mercurius-auto-schema` allows you to create a GraphQL schema and endpoint from\nFastify own route definitions. By using this module, you would get a dynamic OpenAPI\nspecification of your API that will be used as the basis for the GraphQL implementation.\n\nThe main difference with other similar approaches is that `mercurius-auto-schema` does not\nperform any HTTP requests but leverages the high-throughput `fastify.inject()` utility for\nin-process request processing.\n\nBuilt upon:\n\n* [fastify-swagger](https://github.com/fastify/fastify-swagger)\n* [mercurius](https://github.com/mercurius-js/mercurius)\n* [openapi-graphql](https://github.com/mcollina/openapi-graphql)\n* [`fastify.inject()`](https://www.fastify.io/docs/latest/Testing/#benefits-of-using-fastifyinject)\n\nThis module is high level and it exposes all the behavior of its constituent parts.\nSee the individual modules for various docs and explanations.\n\n## Install\n\n```sh\nnpm i mercurius-auto-schema\n```\n\n## Example\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\nconst { printSchema } = require('graphql')\n\nfastify.register(require('mercurius-auto-schema'), {\n  definitions: {\n    openapi: {\n      info: {\n        title: 'Test OpenAPI',\n        description: 'testing the fastify openapi',\n        version: '0.1.0'\n      }\n    },\n    exposeRoute: true\n  },\n  graphql: {\n    graphiql: true\n  }\n})\n\nfastify.put('/some-route/:id', {\n  schema: {\n    description: 'post some data',\n    tags: ['user', 'code'],\n    summary: 'qwerty',\n    params: {\n      type: 'object',\n      properties: {\n        id: {\n          type: 'string',\n          description: 'user id'\n        }\n      }\n    },\n    body: {\n      type: 'object',\n      properties: {\n        hello: { type: 'string' },\n        obj: {\n          type: 'object',\n          properties: {\n            some: { type: 'string' }\n          }\n        }\n      }\n    },\n    response: {\n      201: {\n        description: 'Succesful response',\n        type: 'object',\n        properties: {\n          hello: { type: 'string' }\n        }\n      }\n    }\n  }\n}, (req, reply) =\u003e { reply.send({ hello: `Hello ${req.body.hello}` }) })\n\nfastify.get('/user/:id', {\n  schema: {\n    description: 'get a user',\n    params: {\n      type: 'object',\n      properties: {\n        id: {\n          type: 'string',\n          description: 'user id'\n        }\n      }\n    },\n    response: {\n      201: {\n        description: 'Succesful response',\n        type: 'object',\n        properties: {\n          name: { type: 'string' },\n          companyId: { type: 'string' }\n        }\n      }\n    }\n  },\n  links: {\n    201: {\n      company: {\n        operationId: 'getCompany',\n        parameters: {\n          id: '$request.path.id'\n        }\n      }\n    }\n  }\n}, () =\u003e 'hello world')\n\nfastify.get('/company/:id', {\n  schema: {\n    operationId: 'getCompany',\n    description: 'get a company',\n    params: {\n      type: 'object',\n      properties: {\n        id: {\n          type: 'string',\n          description: 'company id'\n        }\n      }\n    },\n    response: {\n      201: {\n        description: 'Succesful response',\n        type: 'object',\n        properties: {\n          name: { type: 'string' }\n        }\n      }\n    }\n  }\n}, () =\u003e 'hello world')\n\nfastify.listen(3000, err =\u003e {\n  if (err) throw err\n  console.log(printSchema(fastify.graphql.schema))\n})\n```\n\nThis will print:\n\n```\ntype Query {\n  \"\"\"\n  get a company\n\n  Equivalent to GET /company/{id}\n  \"\"\"\n  company(\n    \"\"\"company id\"\"\"\n    id: String!\n  ): Company\n\n  \"\"\"\n  get a user\n\n  Equivalent to GET /user/{id}\n  \"\"\"\n  user(\n    \"\"\"user id\"\"\"\n    id: String!\n  ): User\n}\n\n\"\"\"Succesful response\"\"\"\ntype Company {\n  name: String\n}\n\n\"\"\"Succesful response\"\"\"\ntype User {\n  company: Company\n  companyId: String\n  name: String\n}\n\ntype Mutation {\n  \"\"\"\n  post some data\n\n  Equivalent to PUT /some-route/{id}\n  \"\"\"\n  putSomeRouteId(\n    \"\"\"user id\"\"\"\n    id: String!\n    someRouteInput: SomeRouteInput\n  ): SomeRoute2\n}\n\n\"\"\"Succesful response\"\"\"\ntype SomeRoute2 {\n  hello: String\n}\n\ninput SomeRouteInput {\n  hello: String\n  obj: ObjInput\n}\n\ninput ObjInput {\n  some: String\n}\n```\n\n### Authorization headers \u0026 JWT support\n\nIf your application requires authenticating with JWT using a bearer token scheme,\nyou could specify a few options to create a smoother transformation.\n(If you are curious to what happens without these options, take a look at https://github.com/mcollina/openapi-graphql#authentication).\n\n```js\n'use strict'\n\nconst fastify = require('fastify')\nconst auto = require('mercurius-auto-schema')\nconst JWT = require('fastify-jwt')\n\nasync function run () {\n  const app = fastify({ logger: { level: 'error' } })\n\n  app.register(JWT, {\n    secret: 'CHANGEME!!'\n  })\n\n  app.register(auto, {\n    definitions: {\n      openapi: {\n        info: {\n          title: 'Test swagger',\n          description: 'testing the fastify swagger api',\n          version: '0.1.0'\n        },\n        components: {\n          securitySchemes: {\n            bearerAuth: {\n              type: 'http',\n              scheme: 'bearer',\n              format: 'JWT'\n            }\n          }\n        }\n      }\n    },\n\n    // Disable the auth-wrapping objects\n    viewer: false,\n\n    // You can use customizeHttpRequest to add other headers as well, e.g. OpenTelemetry.\n    customizeHttpRequest (opts, context) {\n      opts.headers.authorization = context.reply.request.headers.authorization\n      return opts\n    }\n  })\n\n  app.get('/user/:id', {\n    schema: {\n      operationId: 'user',\n      description: 'get a user',\n      params: {\n        type: 'object',\n        properties: {\n          id: {\n            type: 'string',\n            description: 'user id'\n          }\n        }\n      },\n      response: {\n        200: {\n          description: 'Succesful response',\n          type: 'object',\n          properties: {\n            name: { type: 'string' },\n            companyId: { type: 'string' }\n          }\n        }\n      },\n      security: [\n        { bearerAuth: [] }\n      ]\n    }\n  }, async (req) =\u003e {\n    await req.jwtVerify()\n    equal(req.user.name, 'foobar', 'jwt parsed correctly')\n    equal(req.params.id, 'foo42', 'user id matches')\n\n    return {\n      companyId: 42,\n      name: 'foo'\n    }\n  })\n\n  await app.ready()\n\n  {\n    const query = 'query { user (id: \"foo42\") { name, companyId } }'\n\n    const res = await app.inject({\n      method: 'POST',\n      url: '/graphql',\n      headers: {\n        Authorization: `Bearer ${app.jwt.sign({ name: 'foobar' })}`\n      },\n      body: {\n        query\n      }\n    })\n\n    console.log(res.json())\n    // {\n    //  data: {\n    //    user: {\n    //      name: 'foo',\n    //      companyId: 42\n    //    }\n    //  }\n    // }\n  }\n})\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Fmercurius-auto-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcollina%2Fmercurius-auto-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Fmercurius-auto-schema/lists"}