A loader is an utility to avoid the 1 + N query problem of GraphQL. Each defined loader will register a resolver that coalesces each of the request and combines them into a single, bulk query. Moreover, it can also cache the results, so that other parts of the GraphQL do not have to fetch the same data.
Each loader function has the signature loader(queries, context)
.
queries
is an array of objects defined as { obj, params }
where
obj
is the current object and params
are the GraphQL params (those
are the first two parameters of a normal resolver). The context
is the
GraphQL context, and it includes a reply
object.
Example:
const loaders = {
Dog: {
async owner(queries, { reply }) {
return queries.map(({ obj }) => owners[obj.name])
}
}
}
app.register(mercurius, {
schema,
resolvers,
loaders
})
It is also possible disable caching with:
const loaders = {
Dog: {
owner: {
async loader(queries, { reply }) {
return queries.map(({ obj }) => owners[obj.name])
},
opts: {
cache: false
}
}
}
}
app.register(mercurius, {
schema,
resolvers,
loaders
})
Disabling caching has the advantage to avoid the serialization at the cost of more objects to fetch in the resolvers.
Internally, it uses single-user-cache.