The Context Object
The third argument passed to every resolver function is `context`. Here's what that object contains:
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 belowcontext.db.entities.schema
- This is a string with the schema namecontext.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.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.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.We offer some third party libraries on
context.util
, so you can use the pattern const {dateFns} = context.utils
in your resolvers.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.