Backend architecture

database

datamodel

Type definitions in GraphQL SDL for our database. These types are roughly translated as SQL tables by Prisma.

prisma.yml

Prisma configuration file. Learn more about it here.

seed.graphql

Initial values that will be loaded in our database by Prisma.

src

directives

GraphQL schema directives can be defined in this directory. We use them for authentication.

errors

Custom error definitions can be placed here. They should all inherit the default ApolloError provided in order to keep consistency.

generated

This folder will contain all the GraphQL generated schema. Specifically, our database schema generated by Prisma will be downloaded here and will be consumed by prisma-bindings.

middleware

Express.js middleware is located here. Middleware runs right after a request is received and before GraphQL resolvers start. It is a good place to check authentication tokens.

models

Every type in our app should have a model that defines all its actions such as createItem, updateItem, publishItem, etc. This is where most of the DB-interaction logic lives. Ownership checks can be done here.

resolvers

Contains all the GraphQL resolvers for queries, mutations and subscriptions. Think of resolvers as thin controllers that call actions defined in models.

schema

Our server's API definition (not our database!) lives here in shape of GraphQL SDL. We separate every GraphQL entity in a different file: types, directives, inputs, scalars, etc. The index.graphql will contain the queries and mutations.

utils

Several utility functions.

db.js

Exports the database connection object that can be used in resolvers or any other service.

apollo-server.js

Manages configurations specific to apollo-server (or apollo-server-express) such as subscriptions, mocks, Apollo engine, etc.

pubsub.js

Exports the PubSub implementation for GraphQL subscriptions. By default, an in-memory PubSub is used for development but can be easily updated when scaling the project (Redis, RabbitMQ, etc.).

index.js

Main entry point of our server. Connects all the previous pieces and starts the HTTP server.

.graphqlconfig.yml

Configuration for our GraphQL development environment. More info here.