GraphQL and MongoDB

General Architecture

Note: you must have a MongoDB/Atlas instance and the env variable MONGODB_BASE_URL set to its connection string.

Schema

A schema is how to define the structure of the data for GraphQL. We use an addon to autogenerate Typescript types (and only types!) for MongoDB objects. It is important to specify which GraphQL objects are intended to reflect MongoDB documents (usually with the @entity tag) - additional info https://graphql-code-generator.com/docs/plugins/typescript-mongodb/. The schema is found as follows:

Models

Models specify which MongoDB collections we have. Their types are autogenerated and can be found in server/generated/graphql.ts. In this file, we create the actual MongoDB collections and provide them their names. It is located as follows:

Resolvers

Resolvers produce the appropriate Javascript object/representation of our GraphQL objects. Their types are autogenerated and can be found in server/generated/graphql.ts. These types are imported into the implementation file, where you must define each resolver:

Codegen

As a reminder, we use Graphql Code Generator to generate Typescript definitions for GraphQL resolvers and their MongoDB objects. In order for GraphQL resolvers to return MongoDB objects instead of their default types, you must change:

Example Queries from the Client

Some example query documents can be found here: