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:
- ./src/common/schema.graphql.ts - main schema, shared among server and client
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:
- ./src/client/routes/team/teams.graphql.ts - team data
- ./src/client/routes/profile/user.graphql.ts - for a specific user
- ./src/client/routes/manage/hackers.graphql.ts - hacker table
- ./src/client/me.graphql.ts - personal logged in user