In this post I will share a great structure for structuring your Graphql application, using Typescript and Express.
The structure consists of these folders/layers (from outer to inner):
- IOC: Inversion of control provides testability and control over the dependencies in the system. For this, I use “Inversify”
- Schema: The definition of the GraphQL schemas defined as .gql files for making the schema definition language agnostic.
- Resolvers: The definition of the resolvers. These are split into query and mutation resolvers.
- Models: The use case models containing business information and being called by resolvers.
- Connectors: Layer for doing external requests.
- Interfaces: Interfaces proving type safety in the GraphQL app.
For testing, there is a tests folder containing a folder for unit tests and one for integration tests. I use Jest for running test as this works great with Typescript and contains a rich test framework with easy setup.
The code for a starter template with this structure can be found at my Github here.