Codebase Explained
GraphJin code is made up of a number of packages. We have done our best to keep each package small and focused. Let us begin by looking at some of these packages.
- qcode - GraphQL lexer and parser.
- psql - SQL generator
- serv - HTTP Endpoint, Configs, CLI, etc
- rails - Rails cookie and session store decoders
#
QCODEThis package contains the core of the GraphQL compiler it handling the lexing and parsing of the GraphQL query transforming it into an internal representation called
QCode
.
This is the first step of the compiling process the func NewCompiler(c Config)
function creates a new instance of this compiler which has it's own config.
Keep in mind QCode has no knowledge of the Database structure it is designed to be a fast GraphQL parser. Care is taken to keep memory allocations to a minimum.
But before the incoming GraphQL query can be turned into QCode it must first be tokenzied by the lexer lex.go
. As the tokenzier walks the bytes of the query it generates tokens item
structs which are then consumed by the next step the parser parse.go
.
For exmple a simple query like query getUser { user { id } }
will be converted into several tokens like below.
These tokens are then fed into the parser parse.go
the parser does the work of generating an abstract syntax tree (AST) from the tokens. This AST is an internal representation (data structure) and is not exposed outside the package. Since the AST is a tree a stack stack.go
is used to walk the tree and generate the QCode AST. The QCode data structure is also a tree (represented as an array). This is then returned to the caller of the compile function.
#
PSQLThis package is responsible for generating Postgres SQL from the QCode AST. There are various GraphQL query types (Query, Mutation, etc). And several more sub types like single root or multi-root queries, various types of mutations (insert, update delete, bulk insert, etc). This package is designed to be able to generate SQL for all of those types.
In addition to QCode variable data is also passed to the compile function within this package. Variables are decoded to derive what is being inserted and what kind of insert is it single or bulk. This information is not available in the GraphQL query its passed in seperatly via variables. This package is able to put all this together and generate the right SQL code.
The entry point of this package is in query.go
. The database schema must be passed in the config object when creating a new compiler instance NewCompiler
. The functions to extract this schema from the database are also part of this package tables.go
. The GetTables
functions fetches all the tables from the database and GetColumns
fetches columns and relationship information.
GraphQL, input is first converted to QCode.
SQL, in reality the generated SQL is far more complex single it has to be very efficient, leverage the power of Postgres, support RBAC (Role based access control) and all of this must be done in a single SQL query.
#
SERVThe serv
package constains most of code that turns the above compiler into an HTTP service. It also includes authentication middleware, remote join resolvers, config parsering, database migrations and seeding commands.
Another big feature that this package handles is the allow.list
management code. In production mode parsing the allow list file and registering prepared statements to adding GraphQL queries to this file in development mode.
Currently the following global variables are referrenced across the package. In future I'd prefer to move these into a context struct and pass that around instead.
#
TestingThere are several unit tests and benchmark tests parse_test.go
) included. There are also scripts included for memory pprof_cpu.sh
and cpu pprof_cpu.sh
profiling.
You can run tests within each package or across the entire app. It is usually the fastest to first write a test and then build the feature to satisfy it.
Memory profiling can help find where allocations are happining within the package code.
#
BenchmarkingMost packages contain benchmark tests to ensure new features don't introduce a significant regression to performance.
#
Reach outIf you'd like me to explain other parts of the code please reach out over Twitter or Discord. I'll keep adding to this doc as I get time.
#
Developing GraphJinIf you want to build and run GraphJin from code then the below commands will build the web ui and launch GraphJin in developer mode with a watcher to rebuild on code changes. And the demo rails app is also launched to make it easier to test changes.