{"id":26920691,"url":"https://github.com/apollo-server-integrations/apollo-server-integration-fastify","last_synced_at":"2025-05-16T11:03:50.618Z","repository":{"id":45709736,"uuid":"512707200","full_name":"apollo-server-integrations/apollo-server-integration-fastify","owner":"apollo-server-integrations","description":"An Apollo Server integration for use with Fastify.","archived":false,"fork":false,"pushed_at":"2025-05-08T08:46:55.000Z","size":1621,"stargazers_count":67,"open_issues_count":16,"forks_count":19,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-08T09:38:37.995Z","etag":null,"topics":["apollo","apollo-server","fastify","graphql","nodejs","typescript"],"latest_commit_sha":null,"homepage":"","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/apollo-server-integrations.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":"contributing.md","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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-07-11T10:12:52.000Z","updated_at":"2025-05-07T14:28:47.000Z","dependencies_parsed_at":"2023-10-04T03:29:29.600Z","dependency_job_id":"76b761ac-1aa5-4683-89e2-c3bc09202870","html_url":"https://github.com/apollo-server-integrations/apollo-server-integration-fastify","commit_stats":{"total_commits":314,"total_committers":8,"mean_commits":39.25,"dds":0.5031847133757962,"last_synced_commit":"aca6e1c0fe4d2e213ee112b8fea907db07dbe933"},"previous_names":["olyop/apollo-server-fastify"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo-server-integrations%2Fapollo-server-integration-fastify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo-server-integrations%2Fapollo-server-integration-fastify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo-server-integrations%2Fapollo-server-integration-fastify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollo-server-integrations%2Fapollo-server-integration-fastify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apollo-server-integrations","download_url":"https://codeload.github.com/apollo-server-integrations/apollo-server-integration-fastify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254518384,"owners_count":22084374,"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":["apollo","apollo-server","fastify","graphql","nodejs","typescript"],"created_at":"2025-04-01T22:39:35.447Z","updated_at":"2025-05-16T11:03:50.599Z","avatar_url":"https://github.com/apollo-server-integrations.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca href='https://www.apollographql.com/'\u003e\u003cimg src='https://avatars.githubusercontent.com/u/17189275?s=200' style=\"border-radius: 6px; margin-right: 6px\" height='100' alt='Apollo Server'\u003e\u003c/a\u003e\n\u003ca href='https://www.fastify.io/'\u003e\u003cimg src='https://avatars.githubusercontent.com/u/24939410?s=200' style=\"border-radius: 6px\" height='100' alt='Fastify'\u003e\u003c/a\u003e\n\n[![NPM version](https://badge.fury.io/js/@as-integrations%2Ffastify.svg)](https://www.npmjs.com/package/@as-integrations/fastify)\n[![NPM downloads](https://img.shields.io/npm/dm/@as-integrations/fastify.svg?style=flat)](https://www.npmjs.com/package/@as-integrations/fastify)\n\n# Apollo Server Integration for Fastify\n\n## **Introduction**\n\n**An Apollo Server integration for use with Fastify.**\n\nThis is a simple package that easily allows you to connect your own Fastify server implementation to an Apollo Server instance.\n\n## **Requirements**\n\n- **[Node.js v16](https://nodejs.org/)** or later\n- **[Fastify v4.4](https://www.fastify.io/)** or later\n- **[GraphQL.js v16](https://graphql.org/graphql-js/)** or later\n- **[Apollo Server v4](https://www.apollographql.com/docs/apollo-server/)** or later\n\n## **Installation**\n\n```bash\nnpm install @as-integrations/fastify @apollo/server graphql fastify\n```\n\n## **Usage**\n\nSetup [Fastify](https://www.fastify.io/) \u0026 [Apollo Server](https://www.apollographql.com/docs/apollo-server/) like you usually would and then connect the two by using the `fastifyApollo` plugin:\n\n```typescript\nimport Fastify from \"fastify\";\nimport { ApolloServer, BaseContext } from \"@apollo/server\";\nimport fastifyApollo, { fastifyApolloDrainPlugin } from \"@as-integrations/fastify\";\n// ...\n\nconst fastify = Fastify();\n\nconst apollo = new ApolloServer\u003cBaseContext\u003e({\n  typeDefs,\n  resolvers,\n  plugins: [fastifyApolloDrainPlugin(fastify)],\n});\n\nawait apollo.start();\n\n// ...\n\nawait fastify.register(fastifyApollo(apollo));\n```\n\nNote: **You must call and await `apollo.start()` before using the integration.**\n\nAlternatively you can use the exported function `fastifyApolloHandler` which can be passed into any [Fastify route handler](https://www.fastify.io/docs/latest/Reference/Routes/).\nThis allows you to explicitly set all routing options like the URL path and accepted methods.\n\nExamples shown below:\n\n```typescript\nimport { fastifyApolloHandler } from \"@as-integrations/fastify\";\n\n// ... setup Fastify \u0026 Apollo\n\nfastify.post(\"/graphql\", fastifyApolloHandler(apollo));\n// OR\nfastify.get(\"/api\", fastifyApolloHandler(apollo));\n// OR\nfastify.route({\n  url: \"/graphql\",\n  method: [\"POST\", \"OPTIONS\"],\n  handler: fastifyApolloHandler(apollo),\n});\n```\n\nPlease see the [example](https://github.com/apollo-server-integrations/apollo-server-integration-fastify/tree/main/example).\n\n## **Context**\n\nApollo Server v4 has moved context setup outside of the `ApolloServer` constructor.\n\nDefine your own context function and pass it in to the `context` option. The function accepts two arguments - request and reply. They are of type `FastifyRequest` and `FastifyReply` respectively. Whatever is returned from the function will be passed to the `context` argument in your resolvers.\n\nFor example:\n\n```typescript\nimport { ApolloServer } from \"@apollo/server\";\n\nimport fastifyApollo, {\n  fastifyApolloHandler,\n  ApolloFastifyContextFunction,\n} from \"@as-integrations/fastify\";\n// ...\n\ninterface MyContext {\n  authorization: JWTPayload | false;\n}\n\nconst apollo = new ApolloServer\u003cMyContext\u003e(...);\n\nconst myContextFunction: ApolloFastifyContextFunction\u003cMyContext\u003e = async (request, reply) =\u003e ({\n  authorization: await isAuthorized(request.headers.authorization),\n});\n\nawait fastify.register(fastifyApollo(apollo), {\n  context: myContextFunction,\n});\n\n// OR\n\nawait fastify.post(\"/graphql\", fastifyApolloHandler(apollo, {\n  context: myContextFunction,\n}));\n```\n\n```ts\n// Access the context in your resolvers\nexport const resolvers = {\n  Query: {\n    helloWorld: (parent, args, context, info) =\u003e {\n      if (!context.authorization) {\n        throw new Error(\"Not authorized\");\n      }\n      \n      return \"Hello world :)\";\n    },\n  },\n};\n```\n\n## **API**\n\nAll options and generics are optional other than passing in the `ApolloServer` instance.\n\n### `fastifyApollo`\n\n```typescript\nfunction fastifyApollo\u003cContext extends BaseContext = BaseContext\u003e(\n  apollo: ApolloServer\u003cContext\u003e,\n): FastifyPluginAsync\u003cApolloFastifyPluginOptions\u003cContext\u003e\u003e;\n```\n\n### `fastifyApolloHandler`\n\n```typescript\nfunction fastifyApolloHandler\u003cContext extends BaseContext = BaseContext\u003e(\n  apollo: ApolloServer\u003cContext\u003e,\n  options?: ApolloFastifyHandlerOptions\u003cContext\u003e,\n): RouteHandlerMethod;\n```\n\n### `ApolloFastifyContextFunction`\n\n```typescript\ntype ApolloFastifyContextFunction\u003cContext\u003e = (\n  request: FastifyRequest,\n  reply: FastifyReply,\n) =\u003e Promise\u003cContext\u003e;\n```\n\n### `ApolloFastifyHandlerOptions`\n\n```typescript\ninterface ApolloFastifyHandlerOptions\u003cContext extends BaseContext = BaseContext\u003e {\n  context?: ApolloFastifyContextFunction\u003cContext\u003e; // default: async () =\u003e ({})\n}\n```\n\n### `ApolloFastifyPluginOptions`\n\n```typescript\ninterface ApolloFastifyPluginOptions\u003cContext extends BaseContext = BaseContext\u003e\n  extends ApolloFastifyHandlerOptions\u003cContext\u003e {\n  path?: string; // default: \"/graphql\"\n  method?: HTTPMethod | HTTPMethod[]; // default: [\"GET\", \"POST\", \"OPTIONS\"]\n}\n```\n\n[`HTTPMethod`](https://www.fastify.io/docs/latest/Reference/TypeScript/#fastifyhttpmethods) is exported from Fastify.\n\n## **HTTPS/HTTP2**\n\nAll functions and types optionally allow you to pass in or infer a `Server` type from Fastify.\n\n## **Node.JS v16**\n\nPlease pass in `forceCloseConnections: true` to Fastify in combination with `fastifyApolloDrainPlugin` to correctly shutdown you're server on close and not hang incoming requests.\n\n## **Contributors**\n\n- Oliver Plummer ([olyop](https://github.com/olyop))\n- Trevor Scheer ([trevor-scheer](https://github.com/trevor-scheer))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-server-integrations%2Fapollo-server-integration-fastify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollo-server-integrations%2Fapollo-server-integration-fastify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-server-integrations%2Fapollo-server-integration-fastify/lists"}