Links

The Context Object

The third argument passed to every resolver function is `context`. Here's what that object contains:

Databases:

context.db contains a mapping of databases (currently only entities exists).
context.db.entities - This is an object containing db helper methods:
context.db.entities.tables - This is an object of all tables. See examples below
context.db.entities.schema - This is a string with the schema name
context.db.entities.query(query: string, values: any[]) - This is a function that allows you to execute raw queries against your entities tables. It will return a list of rows.
context.db.entities.find(query: string, values: any[]) - This is a function that allows you to execute a raw query against your entitiy tables. It returns only the first matching row.

How to make queries:

context.db.entities.query(
`SELECT * FROM ${context.db.entities.tables.NFT} WHERE id = ?`,
[1]
)
Before each query, we set the search_path to the context.db.entities.schema, so you don't need to include the schema in your raw sql.

DB Tables:

In the tables object, you'll find multiple tables for each entity. For example, NFT and NFT_ALL. The _ALL suffixed tables are a historical record of changes to these entities. Searching NFT where id = 5 will return you 1 row (or 0 if that ID doesn't exist), but searching NFT_ALL where id = 5 will show you all changes ever made to that NFT entity.

Third party Libraries:

We offer some third party libraries on context.util, so you can use the pattern const {dateFns} = context.utils in your resolvers.
  • dateFns - The date-fns library
  • _ - The lodash library
  • uuid - The uuid library. Provides v1, v2, v3, v4 and v5 functions.
  • validator - The validator library.

Helpers

context.helpers contains the exact object that you provided as an export in helpers.ts. Keep in mind that if your helper needs a database connection, you'll have to pass that in from the resolver.