{"id":15285697,"url":"https://github.com/rpvsilva/graphql-fastify-server","last_synced_at":"2025-04-09T20:04:02.021Z","repository":{"id":42494709,"uuid":"464522384","full_name":"rpvsilva/graphql-fastify-server","owner":"rpvsilva","description":"GraphQL Fastify Server is an implementation of GraphQL.","archived":false,"fork":false,"pushed_at":"2025-04-08T11:13:26.000Z","size":1191,"stargazers_count":39,"open_issues_count":11,"forks_count":6,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-08T12:25:30.119Z","etag":null,"topics":["fastify","graphql","hacktoberfest","nodejs","performance","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/rpvsilva.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"github":["rpvsilva"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":["paypal.me/rpvsilva"]}},"created_at":"2022-02-28T14:48:32.000Z","updated_at":"2025-01-21T00:02:59.000Z","dependencies_parsed_at":"2024-01-18T01:13:09.849Z","dependency_job_id":"6a4510bd-4908-4bbf-8262-8f938a1f3091","html_url":"https://github.com/rpvsilva/graphql-fastify-server","commit_stats":{"total_commits":305,"total_committers":7,"mean_commits":43.57142857142857,"dds":"0.45245901639344266","last_synced_commit":"0eb1c5447998fd51311838e373156b37728be3ed"},"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpvsilva%2Fgraphql-fastify-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpvsilva%2Fgraphql-fastify-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpvsilva%2Fgraphql-fastify-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpvsilva%2Fgraphql-fastify-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rpvsilva","download_url":"https://codeload.github.com/rpvsilva/graphql-fastify-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247842002,"owners_count":21005204,"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":["fastify","graphql","hacktoberfest","nodejs","performance","typescript"],"created_at":"2024-09-30T15:07:12.471Z","updated_at":"2025-04-09T20:04:01.990Z","avatar_url":"https://github.com/rpvsilva.png","language":"TypeScript","funding_links":["https://github.com/sponsors/rpvsilva","paypal.me/rpvsilva"],"categories":["TypeScript"],"sub_categories":[],"readme":"# GraphQL Fastify Server\n\n![build badge](https://github.com/rpvsilva/graphql-fastify-server/actions/workflows/on_push.yaml/badge.svg) \n[\u003cimg src=\"https://img.shields.io/npm/dt/graphql-fastify-server?color=brightgreen\u0026logo=npm\"\u003e](https://npmjs.com/package/graphql-fastify-server)\n![version](https://img.shields.io/npm/v/graphql-fastify-server?color=brightgreen\u0026label=version)\n[\u003cimg src=\"https://snyk.io/test/github/rpvsilva/graphql-fastify-server/badge.svg\"\u003e](https://snyk.io/test/github/rpvsilva/graphql-fastify-server)\n![license](https://img.shields.io/github/license/rpvsilva/graphql-fastify-server?color=blue\u0026logo=github)\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Using cache](#using-cache)\n  - [Middlewares](#middlewares)\n  - [Subscriptions](#subscriptions)\n- [Liveness \u0026 Readiness](#liveness--readiness)\n- [Contributing](#contributing)\n- [License](#license)\n\n\n## Installation\n\n```shell\nnpm install --save graphql-fastify-server\n```\n\n## Usage\n\n```javascript\nconst app = fastify();\n\nconst server = new GraphQLFastify({\n  schema,\n  context,\n  playground: {\n    introspection: !isProd,\n  },\n});\n\nserver.applyMiddleware({ app, path: '/' }).then(() =\u003e {\n  app.listen({ port: +PORT }, () =\u003e {\n    console.log(`Server listening on port http://0.0.0.0:${PORT}`);\n  });\n});\n\n```\n\n### Using cache\nOn GraphQL Fastify Server you can use two types of cache, one is memory cache and the other is using a Redis instance. Then you inject the cache variable into the GraphQLFastify instance.\n\n```javascript\nconst cache: Cache\u003cContextType, Resolvers\u003e = {\n  defaultTTL: 1000,\n  storage: 'memory',\n  policy: {\n    add: {\n      ttl: 1000,\n    },\n  },\n  extraCacheKeyData: (ctx) =\u003e {\n    const { locale } = ctx;\n\n    return locale;\n  },\n};\n\n// --- OR ---\n\nconst cache: Cache\u003cContextType, Resolvers\u003e = {\n  defaultTTL: 1000,\n  storage: new Redis({\n    host: 'localhost',\n    port: 6379,\n  }),\n  policy: {\n    add: {\n      ttl: 1000,\n    },\n  },\n  extraCacheKeyData: (ctx) =\u003e {\n    const { locale } = ctx;\n\n    return locale;\n  },\n};\n```\n\nAlso, you can define the query scope between `PUBLIC` and `PRIVATE` for caching. This tells the cache to use the authorization token from the headers to compute the cache key.\n\n```javascript\nconst policy: CachePolicy\u003cResolvers\u003e = {\n  add: {\n    ttl: 1000,\n    scope: 'PUBLIC',\n  },\n  sub: {\n    ttl: 1500,\n    scope: 'PRIVATE',\n  }\n}\n```\n\n### Middlewares\n\nYou can use middlewares at the Fastify and you can define in which operations you want to execute them.\n\n```javascript\nconst middlewares: Middlewares\u003cContextType, Resolvers\u003e = [\n  {\n    handler: (context) =\u003e {\n      const { isAutheticated } = context;\n\n      if (!isAutheticated) throw new HttpError(401, 'Not authenticated');\n    },\n    operations: ['add'],\n  },\n];\n```\n\n### Subscriptions\n\nTo enable subscriptions you need to set to `true` the property `subscriptions` on the server initialization \n\n```javascript\nconst server = new GraphQLFastify({\n  schema,\n  context,\n  subscriptions: true\n});\n```\n\nOn resolvers side, you can use the `pubsub` property available on context. With the following schema, you can use the subscriptions feature like:\n\n\u003e `pubsub` property is only available subscriptions are enabled\n\n\n```graphql\ntype Query {\n  add(x: Int!, y: Int!): Int!\n}\n\ntype Subscription {\n  added(x: Int!, y: Int!): Int!\n}\n```\n\n\n```javascript\nconst resolvers = {\n  Query: {\n    add: async (_: null, { x, y }: { x: number; y: number }, { pubsub }: ContextType): Promise\u003cnumber\u003e =\u003e {\n      await pubsub.publish(`add-${x}-${y}`, { add: x + y });\n\n      return x + y;\n    },\n  },\n  Subscription: {\n    added: {\n      subscribe: async (_: null, { x, y }: { x: number; y: number }, { pubsub }: ContextType) =\u003e {\n        return pubsub.subscribe(`add-${x}-${y}`);\n      },\n    },\n  },\n};\n```\n\n## Liveness \u0026 Readiness\n\nTo check the health of the server you can make a GET request to the endpoint `/server-health`\n\n## Contributing\nIf you have any doubt or to point out an issue just go ahead and create a new [issue](https://github.com/rpvsilva/graphql-fastify-server/issues/new). If you want to contribute, just [check](./CONTRIBUTING.md) how. \n\n## License\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpvsilva%2Fgraphql-fastify-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frpvsilva%2Fgraphql-fastify-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpvsilva%2Fgraphql-fastify-server/lists"}