I'm writing my own graphql queries and mutations, however I couldn't figure out how I can get types on this kind of resolver function, all arguments including
context
is untyped:
export const CustomResolver = async (obj, args, context, info) => {
console.log('id received:', args)
console.log('context', context)
return {
result: 'PASS',
}
}
I've done a payload import and typed the destructured context like this, not sure if there's a cleaner approach though
{ payload }: { payload: typeof ImportedPayload }
@noheadphones would you mind sharing the code of your custom resolver?
What exactly are you looking to do? I may be able to provide a better example
Basically I'm just trying to learn how the response object should be formatted or how you enabled types here.
Sure, here's an example of a custom query (mutation is pretty much similar)
payload config, something like this
CheckQuizAnswers: CheckQuizAnswers(GraphQL, payload),
This is what the function actually is:
import { CheckQuizAnswersResolver } from '../resolvers/CheckQuizAnswersResolver'
import { default as ImportedGraphQL } from 'graphql'
import { Payload } from 'payload'
export const CheckQuizAnswers = (GraphQL: typeof ImportedGraphQL, payload: Payload) => {
return {
args: {
answers: {
type: new GraphQL.GraphQLNonNull(new GraphQL.GraphQLList(GraphQL.GraphQLString)),
},
},
resolve: CheckQuizAnswersResolver,
type: new GraphQL.GraphQLObjectType({
name: 'result',
fields: {
result: {
type: GraphQL.GraphQLString,
},
},
}),
}
}
And then your resolver
CheckQuizAnswersResolver
is what actually handles the logic, in this function ^^^ type is a return type of a valid GraphQL object, so in this case it's something custom, if I want to return a payload collection though I set the
type
to
payload.collections['articles'].graphQL?.type
for example. The args then is what arguments you might have for your query or mutation, each arg will need a
type
as well: my type here is complex as its a mandatory (nonnull) array (list) of strings
Finally my resolver function looks a bit like this
export const CheckQuizAnswersResolver = async (obj, args: ResolverArgs, context, info) => {
// do some logic here
return {
result: result ? 'PASS' : 'FAIL',
}
}
note that I've manually typed my ResolverArgs, so its a custom type every time, there's no inference right now as far as I can tell, its possible to do it though
Hopefully that makes sense @TheFrontend
Awesome, thanks!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.