Replies: 1 comment 3 replies
-
One way to do this is to actually modify the schema and wrap all resolver into a new function, which will perform the actions you need before executing the actual resolver. Here an example: import { readFileSync } from 'fs'
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { getStitchedSchemaFromSupergraphSdl } from '@graphql-tools/federation'
import { mapSchema, MapperKind } from '@graphql-tools/utils'
import { defaultFieldResolver } from 'graphql'
// This doesn't have to be from a file system, it can be fetched via HTTP from a schema registry
const supergraphSdl = readFileSync('./supergraph.graphql').toString()
const supergraphSchema = await getStitchedSchemaFromSupergraphSdl({ supergraphSdl })
const schema = mapSchema(supergraphSchema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const { resolve = defaultFieldResolver } = fieldConfig
return {
...fieldConfig,
async resolve(obj, args, ctx, info) {
// Perform any action you want here.
const resolvedValue = await resolve(obj, args, ctx, info)
// You can even do things after resolver execution and manipulate the resolved value
return resolvedValue
},
}
},
})
const yoga = createYoga({ schema: })
const server = createServer(yoga)
server.listen(4000, () => {
console.log(`🚀 Server ready at http://localhost:4000/graphql`)
}) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to add middleware that gets executed before every resolver call in above code. (like this: https://the-guild.dev/graphql/mesh/docs/transforms/resolvers-composition)
This middleware should allow to perform custom actions before the resolver is invoked. If anyone has insights into how to do this with given code using GraphQL Yoga, I would greatly appreciate your guidance.
is there any plugin hooks that might help here?
Thank you! 🙏
Beta Was this translation helpful? Give feedback.
All reactions