The GraphQL Bundle is an abstract way to load your Type Definitions, Resolvers, Scalars, Context Transformers/Reducers in a unified place. We made this design choice to be able to hook it with any kind of GraphQL server without making any change to your code. For example, if there's another bundle that instantiates a GraphQL server, you can later swap it for let's say a
serverless bundle without making any changes to your code.
Besides this loading strategy this bundle comes with:
- Mechanism to automatically load GraphQL files based on file system conventions
- A composition strategy for your resolvers to re-use code
It does automatic type and resolver merging and you can also load context manipulators and schema directives.
Register it easily:
This would happen when you want to instantiate your server:
Given that you store your resolvers in:
resolvers.ts or in
*.resolvers.ts, and your types in
*.graphql.ts, you are able to extract the loading module like this:
You also have the ability to store both resolvers and types or things such as context reducers and schema directives. You should use the
A resolver's job is usually:
- Check if inputs are fine (Validation)
- Check security and permission rights (Authorisation)
- Execute the command delegated to a service (Delegation)
- Manipulate the response to fit the client's request (Response Manipulation)
Let's imagine our resolver, to add a post:
postAdd gets transformed to an array of functions:
A more concrete example:
A plugin, is a function that returns a resolver function.
You can also write response manipulators, for example your function returns undefined/false, but you want to return a success response:
When you're creating logic you're most likely want to reuse it, this is why we introduce bundling plugins:
When we are dealing with a GraphQL resolver, there are 2 things we need types for usually: arguments and context. While context is the same from resolver to resolver, arguments change.
You can have additional context reducers which extend your context, to type them, use the following strategy:
For arguments, you either use a generator to transform your GraphQL types into TypeScript. Which can be good but that's an extra process you have to take care of. Usually you would use models that can be validated and the arguments should look like
register(input: RegisterInput!): String. This is why you can do, in most cases, something like: