question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TypeScript Resolvers] Apollo Server resolvers types mismatch

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:29 (8 by maintainers)

github_iconTop GitHub Comments

48reactions
esemeniuccommented, May 7, 2020

For anyone wondering where to put the useIndexSignature: true, my codegen.yml looks like this (see the last 2 lines):

overwrite: true
schema: "src/schema.graphql"
documents: null
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
    config:
      useIndexSignature: true
28reactions
kamilkisielacommented, Mar 22, 2019

It’s hidden behind a flag because the type safety is less strict then.

Try useIndexSignature: true

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found