graphql-psychologists-courses

command module
v0.0.0-...-f7dd486 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

Psychologists courses

This is a sandbox pet project with backend graphql api built on Go's gqlgen library.

Prerequisites

  • docker (for postgres)
  • Go
  • linux (for Makefile)

Tech stack

  • Go 1.22
  • gqlgen
  • bun for main database manipulations
  • pgxpool for migrations
  • Postgres (deployment)

Short explanation

Purpose of this api is to create, read and update entities that are specified by domain. Graphql has many purposes, but in this project it's used to interact with database.

Core entities:

  • psychologist
  • course
  • lesson

Psychologists and courses have many-to-many data model. So, psychologist can have many courses and vice versa.
Course and lessons have one-to-many data model. So, one course has many lessons.
Api has queries and mutations described in graphql schema, so client can get all the entities at once or single by id, insert by values or update by id.

Tables migration file here can be used as reference for entity relations. Data migration can be found here

Technical explanation

This project uses graphql library named gqlgen that generates code from graphql schema. Gqlgen configuration for generating is almost default for this library and stored in gqlgen.yml file. The most important change in this file is autobind property that leads to bunmodels folder and allows to use manually created bun models instead of generated. This approach simplifies interaction between graphql models and database and reuse one model for both sides. But this approach is used only for Query models and doesn't spread on Mutations, where models are still generated by gqlgen.

It's not so clear, but to generate field resolver - @goField(forceResolver: true) directive must be added in gql schema after field where it's needed.

Entry point of the entire program is server.go where described all initialization and dependency injection. The program has custom logger (zerolog), bun as ORM, entman as business logic layer and http server from default net/http package. Server runs generated code, which in turn runs resolvers. Resolvers in this project will be used for authorization and context manipulation in the future, but now are used only to call EntityManager that implements all the business logic.

Project preset to work with makefile, so envs examples can be seen here.

Local run

Current project requires data (at least tables) to be loaded in postgres before working.
Steps to deploy current project on your machine (some steps can be done simply using Makefile, but if you use Windows or system without make, you can use commands inside as cheatsheet):

  • deploy postgres using docker (make deploy-pg). Credentials are in deployment file, so you can use them for db connection
  • migrate data (make migrate). It will create necessary tables and fill them with data
  • run graphql server (make run) and try to retrieve/insert/update data. Now server can be accessed on http://localhost:8080

Local development

This section describes how to change logic and run changes.
Steps of execution:

  • [optionally] change graphql schema and migrations with data for your needs
  • execute first 2 points of Local run section (deploy pg and migrate data)
  • change resolvers and entity manager logic, but don't touch generated code as changes will be erased by generator
  • generate code (make gql-gen)
  • run graphql server (make run). Now server can be accessed on http://localhost:8080

schema.resolvers file can be changed by generator, but the library will allow you to save code for fundamentally changed types in graphql schema even after generation.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg
db

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL