Resolver Hooks
Intro
Sometimes you might need to run function(s), pre
or post
your resolver. You could use Schema Directives with IdioDirectives. Resolver hooks allow you to achieve similar heights.
Hooks are available on any GraphQLNode or GraphQLType resolver exempt from Subscriptions.
new GraphQLNode({
name: "User",
typeDefs: gql`
type User {
name: String
}
type Query {
users: User
}
`,
resolvers: {
Query: {
users: {
pre: isRequestAdmin,
resolve: () => { ... },
post: updateAudit
}
}
}
})
new GraphQLType({
name: "Metadata",
typeDefs: gql`
type Metadata {
lastLogin: String
}
`,
resolvers: {
lastLogin: {
pre: isRequestAdmin,
resolve: () => { ... },
post: updateAudit
}
}
})
Multiple hooks
An array of functions to be resolved sequentiality.
Query: {
users: {
pre: [isRequestAdmin, canRequest(view("users"))],
resolve: () => { ... }
}
}
Async hooks
Query: {
users: {
resolve: () => { ... },
post: async (result) => {
await updateAudit(result)
}
}
}
Pre Hook
type PreHook = (
root: any,
args: { [k: string]: any },
context: Context,
info: DocumentNode
) => void;
Resolve Hook
type Resolve = (
root: any,
args: { [k: string]: any },
context: Context,
info: DocumentNode
) => void;
Post Hook
type PostHook = (
resolve: any,
root: any,
args: { [k: string]: any },
context: Context,
info: DocumentNode
) => void;