Resolving Queries
The examples on this page use the db.user and db.post tables from Relations.
Resolve a list or a single object
Pass the query that selects the rows to graph.resolve and return its result:
const resolvers = {
Query: {
posts: async (_parent, _args, context, info) => {
return await graph.resolve(db.post.order("id"), { context, info })
},
user: async (_parent, { id }, context, info) => {
return await graph.resolve(db.user.findOptional(id), { context, info })
},
},
}The graph keeps everything the query already has, such as conditions, order, and limits, and adds the columns and relations that the client selected. For fields that return a count or a column value, return the Orchid query directly from the resolver.
When TypeScript does not get the result type from the context, such as the return type of your resolver, pass it to graph.resolve:
const posts = await graph.resolve<Post[]>(db.post.order("id"), { context, info })Here Post is the type of the objects that your resolver returns, such as one generated from the GraphQL schema. Pass Post for a query of find and Post | undefined for one of findOptional. TypeScript checks that the type matches whether the query returns a list, a row, or an optional row, but not the fields of the type.
Return the result of a mutation
Write the data first, then resolve a query that reads it back. This example assumes that create_post takes text and author_id:
const resolvers = {
Mutation: {
async create_post(_parent, args, context, info) {
const post = await db.post.create(args)
return await graph.resolve(db.post.find(post.id), { context, info })
},
},
}Resolve an object nested in the result
When a root field returns an object that only partly comes from the database, such as a login result with a token and the user, resolve the database part with path:
type LoginResult {
token: String!
user: User!
}
type Mutation {
login(email: String!, password: String!): LoginResult!
}const resolvers = {
Mutation: {
async login(_parent, args, context, info) {
// Your own authentication.
const { token, userId } = await authenticate(args)
return {
token,
user: await graph.resolve(db.user.find(userId), {
context,
info,
path: ["user"],
}),
}
},
},
}path lists the GraphQL fields from the root field down to the object to resolve. When the client does not select user, graph.resolve runs no query and returns undefined.
Currently broken
When the client selects the field under an alias, such as member: user { id }, graph.resolve returns undefined instead of the user.
Register the types
Register in r.graph every GraphQL type whose objects your tables load: the types of root queries, of relations, and of page nodes. Do not register wrapper types that your own resolvers build, such as LoginResult above or the page types from Pagination.
Type the context
Pass the type of your GraphQL context to r.graph to type context in the callbacks of the graph, such as modify and transform:
interface Context {
userId?: number
isAdmin?: boolean
}
const graph = r.graph<Context>({
User: r.table(db.user),
Post: r.table(db.post),
})In the transform callback of r.table, use transform(instance, { context }) to access that GraphQL context.