apollo-resolvers
Expressive and composable resolvers for Apollostack's GraphQL server
Overview
When standing up a GraphQL backend, one of the first design decisions you will undoubtedly need to make is how you will handle authentication, authorization, and errors. GraphQL resolvers present an entirely new paradigm that existing patterns for RESTful APIs fail to adequately address. Many developers end up writing duplicitous authorization checks in a vast majority of their resolver functions, as well as error handling logic to shield the client from encountering exposed internal errors. The goal of apollo-resolvers
is to simplify the developer experience in working with GraphQL by abstracting away many of these decisions into a nice, expressive design pattern.
apollo-resolvers
provides a pattern for creating resolvers that work, essentially, like reactive middleware. By creating a chain of resolvers to satisfy individual parts of the overall problem, you are able to compose elegant streams that take a GraphQL request and bind it to a model method or some other form of business logic with authorization checks and error handling baked right in.
With apollo-resolvers
, data flows between composed resolvers in a natural order. Requests flow down from parent resolvers to child resolvers until they reach a point that a value is returned or the last child resolver is reached. Thrown errors bubble up from child resolvers to parent resolvers until an additional transformed error is either thrown or returned from an error callback or the last parent resolver is reached.
In addition to the design pattern that apollo-resolvers
provides for creating expressive and composible resolvers, there are also several provided helper methods and classes for handling context creation and cleanup, combining resolver definitions for presentation to graphql-tools
via makeExecutableSchema
, and more.
Example from Apollo Day
Quick start
Install the package:
npm install apollo-resolvers
Create a base resolver for last-resort error masking:
;; const UnknownError = ; const baseResolver = ;
Create a few child resolvers for access control:
; ; const ForbiddenError = ; const AuthenticationRequiredError = ; const isAuthenticatedResolver = baseResolver; const isAdminResolver = isAuthenticatedResolver
Create a profile update resolver for our user type:
;; const NotYourUserError = ; const updateMyProfile = isAuthenticatedResolver; Mutation: updateMyProfile ;
Create an admin resolver:
;; const ExposedError = ; const banUser = isAdminResolver; Mutation: banUser ;
Combine your resolvers into a single definition ready for use by graphql-tools
:
; ;; /* This combines our multiple resolver definition objects into a single definition object*/const resolvers = ; ;
Conditional resolvers:
; ;; const banResolver = UserModel; // Will execute banResolver if either isFooResolver or isBarResolver successfully resolve// If none of the resolvers succeed, the error from the last conditional resolver will// be returnedconst orBanResolver = banResolver; // Will execute banResolver if both isFooResolver and isBarResolver successfully resolve// If one of the condition resolvers throws an error, it will stop the execution and// return the errorconst andBanResolver = banResolver; // In both cases, conditions are evaluated from left to right
Resolver context
Resolvers are provided a mutable context object that is shared between all resolvers for a given request. A common pattern with GraphQL is inject request-specific model instances into the resolver context for each request. Models frequently reference one another, and unbinding circular references can be a pain. apollo-resolvers
provides a request context factory that allows you to bind context disposal to server responses, calling a dispose
method on each model instance attached to the context to do any sort of required reference cleanup necessary to avoid memory leaks:
;;;;;;;;const UnknownError =;const formatError = {let e = ;if e instanceof GraphQLErrore =;return e;};const app = ;app;app;app;;