I'm using apollo server with typescript and I'm having trouble getting the context parameter inside of my resolver to pick up that the name property on context is a string. Right now it's typed as any and I'd like it to be typed as string. I also see that the context parameter is of type any, when I'd like it to be a specific interface. Is there anyway to tell context and it's properties what type I want it to be rather than them all being typed as any?
const server = new ApolloServer({
typeDefs: gql`
type Query {
test: String
}
`,
resolvers: {
Query: {
test(parent: any, args: any, context, info: any) {
context.name // name is typed as "any" when I'd like it to be typed as "string"
}
}
},
context() {
return {
name: 'John Doe'
}
}
})
I tried to do something like this, but that's throwing an error.
context<{ name: string }>() {
return {
name: 'John Doe'
}
}
I did get it to work by explicitly telling context what it needs to be, but this way seems a bit tedious as I have to import Context into every resolver file and cast manually.
interface Context {
name: string
}
const server = new ApolloServer({
...
resolvers: {
Query: {
test(parent: any, args: any, context: Context, info: any) {
context.name // name is typed as "string" here because I used "context: Context" explicitly
}
}
}
...
})