{"id":16181797,"url":"https://github.com/ericclemmons/graphql-context-services","last_synced_at":"2025-08-09T03:39:09.660Z","repository":{"id":66108025,"uuid":"153547128","full_name":"ericclemmons/graphql-context-services","owner":"ericclemmons","description":"GraphQL example using Context \u0026 Services","archived":false,"fork":false,"pushed_at":"2018-10-18T04:09:24.000Z","size":726,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-10T19:03:19.440Z","etag":null,"topics":["apollo","apollo-server","context","express","graphql","nodejs","rest"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ericclemmons.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2018-10-18T01:44:56.000Z","updated_at":"2020-04-20T08:16:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"c576b62e-969c-479f-a83d-efc1b9f95084","html_url":"https://github.com/ericclemmons/graphql-context-services","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fgraphql-context-services","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fgraphql-context-services/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fgraphql-context-services/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fgraphql-context-services/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericclemmons","download_url":"https://codeload.github.com/ericclemmons/graphql-context-services/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243960361,"owners_count":20375102,"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","context","express","graphql","nodejs","rest"],"created_at":"2024-10-10T06:27:45.440Z","updated_at":"2025-03-19T01:31:17.034Z","avatar_url":"https://github.com/ericclemmons.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL `Context` and Services\n\n\u003e Simple example showing how to use `Context` as a `class` within GraphQL,\n\u003e along with `context.services` for abstracting away downstream **API\n\u003e complexity.**\n\u003e\n\u003e For extra credit, this also shows off Apollo's [REST Data Source](https://www.apollographql.com/docs/apollo-server/features/data-sources.html#REST-Data-Source)\n\u003e with **built in caching**!\n\n## Demo\n\n[![Example of a GraphQL query](graphql-context-services.png)](https://codesandbox.io/s/github/ericclemmons/graphql-context-services?module=README.md)\n\nUse the following example query:\n\n```graphql\nquery MyGitHubRateLimit {\n  # Example of getting properties from context\n  ip\n\n  # Example of using context.services.GitHub\n  github {\n    rateLimit {\n      limit\n      # 👇 Cached automatically!\n      remaining\n    }\n  }\n\n  # Returning the app version is handy 🤷‍♂️\n  version\n}\n```\n\n## Rationale\n\n- Using `Context` (instead of a plain `{...}` object) moves\n  complexity from within your middleware to a separate, testable layer:\n\n  ```js\n  .use(\n    \"/graphql\",\n    graphql((req, res) =\u003e {\n      const context = new Context({ req })\n\n      return {\n        context,\n        graphiql: true,\n        pretty: true,\n        schema,\n      }\n    })\n  ```\n\n- `Context` can have a strict, testable API for your resolvers to use,\n  instead of ad-hoc reliance on `req.query` or `req.body`:\n\n  ```js\n  // Before\n  ip: (parent, args, context, info) =\u003e {\n    if (req.header(\"x-forwarded-for\")) {\n      return req\n        .header(\"x-forwarded-for\")\n        .split(\",\")\n        .shift()\n    }\n\n    if (req.connection.remoteAddress) {\n      return req.connection.remoteAddress\n    }\n\n    return req.ip\n  }\n\n  // After\n  ip: (parent, args, context, inf0) =\u003e {\n    return context.ip\n  }\n  ```\n\n- API calls within resolvers are simplified:\n\n  ```js\n  rateLimit: (parent, args, context, info) =\u003e {\n    const { GitHub } = context.services\n\n    return GitHub.getRateLimit()\n  }\n  ```\n\n- The same as Apollo's `dataSources`, but works with the standard `express-graphql` library:\n\n  \u003e \u003chttps://www.apollographql.com/docs/apollo-server/features/data-sources.html#Accessing-data-sources-from-resolvers\u003e\n\n## Author\n\nEric Clemmons ([@ericclemmons](https://twitter.com/ericclemmons))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericclemmons%2Fgraphql-context-services","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericclemmons%2Fgraphql-context-services","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericclemmons%2Fgraphql-context-services/lists"}