https://github.com/ericclemmons/graphql-context-services
GraphQL example using Context & Services
https://github.com/ericclemmons/graphql-context-services
apollo apollo-server context express graphql nodejs rest
Last synced: about 1 month ago
JSON representation
GraphQL example using Context & Services
- Host: GitHub
- URL: https://github.com/ericclemmons/graphql-context-services
- Owner: ericclemmons
- Created: 2018-10-18T01:44:56.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-18T04:09:24.000Z (over 6 years ago)
- Last Synced: 2025-03-10T19:03:19.440Z (about 2 months ago)
- Topics: apollo, apollo-server, context, express, graphql, nodejs, rest
- Language: JavaScript
- Size: 709 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GraphQL `Context` and Services
> Simple example showing how to use `Context` as a `class` within GraphQL,
> along with `context.services` for abstracting away downstream **API
> complexity.**
>
> 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)
> with **built in caching**!## Demo
[](https://codesandbox.io/s/github/ericclemmons/graphql-context-services?module=README.md)
Use the following example query:
```graphql
query MyGitHubRateLimit {
# Example of getting properties from context
ip# Example of using context.services.GitHub
github {
rateLimit {
limit
# 👇 Cached automatically!
remaining
}
}# Returning the app version is handy 🤷♂️
version
}
```## Rationale
- Using `Context` (instead of a plain `{...}` object) moves
complexity from within your middleware to a separate, testable layer:```js
.use(
"/graphql",
graphql((req, res) => {
const context = new Context({ req })return {
context,
graphiql: true,
pretty: true,
schema,
}
})
```- `Context` can have a strict, testable API for your resolvers to use,
instead of ad-hoc reliance on `req.query` or `req.body`:```js
// Before
ip: (parent, args, context, info) => {
if (req.header("x-forwarded-for")) {
return req
.header("x-forwarded-for")
.split(",")
.shift()
}if (req.connection.remoteAddress) {
return req.connection.remoteAddress
}return req.ip
}// After
ip: (parent, args, context, inf0) => {
return context.ip
}
```- API calls within resolvers are simplified:
```js
rateLimit: (parent, args, context, info) => {
const { GitHub } = context.servicesreturn GitHub.getRateLimit()
}
```- The same as Apollo's `dataSources`, but works with the standard `express-graphql` library:
>
## Author
Eric Clemmons ([@ericclemmons](https://twitter.com/ericclemmons))