[TypeScript Resolvers] Apollo Server resolvers types mismatch
See original GitHub issueI use typed resolvers generation and define my resolvers like:
import { QueryResolvers, MutationResolvers, SubscriptionResolvers, User } from './generated/graphql'
const query: QueryResolvers.Resolvers = {
allUsers: (root, args, ctx) => {
const result: User[] = []
return result
}
}
const mutation: MutationResolvers.Resolvers = {
signup: (root, args, ctx) => {
const res = ctx.gql.mutation.createUser({
input: {
user: {
firstName: 'John Doe'
}
}
})
const result: User = { firstname: 'asdf', lastname: 'asdfasdf' }
return result
}
}
export default {
mutation,
query
}
When it comes to configure apollo-server like this:
import resolvers from './app/resolvers'
const server = new ApolloServer({
typeDefs,
resolvers,
mocks: true,
context: (c: any) => ({
...c,
orm: connection,
gql: postgraphileConnection
})
})
then apollo-server gives an error about types mismatch:
Type '{ mutation: Resolvers<GqlContext, {}>; query: Resolvers<GqlContext, {}>; }' is not assignable to type 'IResolvers<any, any>'.
Property 'mutation' is incompatible with index signature.
Type 'Resolvers<GqlContext, {}>' is not assignable to type '(() => any) | GraphQLScalarType | IEnumResolver | IResolverObject<any, any> | IResolverOptions<any, any>'.
Type 'Resolvers<GqlContext, {}>' is not assignable to type 'IResolverObject<any, any>'.
Index signature is missing in type 'Resolvers<GqlContext, {}>'.ts(2322)
types.d.ts(28, 5): The expected type comes from property 'resolvers' which is declared here on type 'Config & { cors?: boolean | CorsOptions; }'
What should be done in order to solve it?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:29 (8 by maintainers)
Top Results From Across the Web
buildSubgraphSchema Typescript typing conflict with resolver
Hi there! I'm integrating a federated schema in typescript and I believe there might be an issue with the typing?
Read more >Typescript types for resolvers - Help - Apollo GraphQL
Hi all, how do you write the resolvers with typescript? I saw this example const resolvers = { Query: { // Our resolvers...
Read more >Apollo Server 4: a lightweight and easier-to-use Apollo Server
Apollo Server 4 enables you to write code that can statically check that your resolver types match their schema types, using a schema-first ......
Read more >Full Stack Error Handling with GraphQL and Apollo
Different types of GraphQL errors; Best practices to deal with those ... networkError : Errors that are thrown outside of your resolvers.
Read more >Resolvers - Apollo GraphQL Docs
Each resolver function belongs to whichever type its corresponding field belongs to. Handling arguments. Now let's say our server defines this schema:.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For anyone wondering where to put the
useIndexSignature: true
, mycodegen.yml
looks like this (see the last 2 lines):It’s hidden behind a flag because the type safety is less strict then.
Try
useIndexSignature: true