Replies: 1 comment 3 replies
-
Hi, can you share more of your setup code ? Like the code creating the yoga server ? The idea to share context type in your server is to define you context once and use it for every plugin. Here a basic example: import { YogaInitialContext, createYoga, createSchema } from 'graphql-yoga';
import { loadFilesSync } from '@graphql-tools/load-files';
import { otherPluginUsingContext } from './otherPlugin'
import { Resolvers } from './schema.graphql' // import the Resolver type of your generated file
interface GraphqlContext {
amazonTraceId?: string
// add any other custom context fields that are used by other plugins
}
type Context = GraphQLContext & YogaInitialContext
const resolvers: Resolver<Context> = {
exampleResolver: (_, __, ctx) => {
// Here, ctx have the right type, including `amazonTraceId` field
}
}
const amazonTraceIdPlugin: Plugin<Context> = {
onPluginInit: ({ addPlugin }) => {
addPlugin(useExtendContext(({ request }) => ({
amazonTraceId: request?.headers?.get('x-amzn-trace-id')
})),
addPlugin(usePayloadFormatter((result, { contextValue }) => {
result.extensions = { ...result.extensions, amazonTraceId: contextValue.amazonTraceId }
return result;
}))
}
}
const yoga = createYoga<YogaInitialContext, GraphqlContext>({
schema: createSchema({
typeDefs: loadFilesSync('./schema.graphql'),
resolvers
}),
plugins: [
amazonTraceIdPlugin,
otherPluginUsingContext<GraphqlContext>(),
]
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.
-
for example, how to fix following error, given this plugin:
Also: after usage of several plugins, how to get final context type in resolvers ? (through graphql codegen)
Beta Was this translation helpful? Give feedback.
All reactions