Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing arguments for resolvers in TypeScript with strictFunctionTypes #722

Closed
jupl opened this issue Apr 8, 2018 · 2 comments
Closed

Typing arguments for resolvers in TypeScript with strictFunctionTypes #722

jupl opened this issue Apr 8, 2018 · 2 comments

Comments

@jupl
Copy link

jupl commented Apr 8, 2018

I have been updating dependencies for a project and came up with an issue with TypeScript. Unfortunately the code below now results in TypeScript errors, specifically highlighting Mutation:

import {IResolvers} from 'graphql-tools'

interface Post {
  id: number
  title: string
  description: string
}

type AddPostArgs = Pick<Post, 'title' | 'description'>

export interface Resolvers extends IResolvers {
  Mutation: {
    addPost(root: {}, args: AddPostArgs): Post
  }
  Query: {
    posts(): Post[]
  }
}

I get a type error under Mutation:

Property 'Mutation' of type '{ addPost(root: {}, args: AddPostArgs): Post; }' is not assignable to string index type '(() => any) | GraphQLScalarType | IEnumResolver | IResolverObject<any, any>'.

If I remove the second args argument from addPost or cast it to something like {}, any, or {[x: string]: any} then it does not complain, but then I lose my typings with my implementation. Apparently it does not like it when I use AddPostArgs for args now? There was no issue with it before. Even if I change AddPostArgs to an interface or inline the type ({title: string, description: string}) it still does not work.

EDIT: Upon further examination, it does seem it's an issue with args:

Type '{ [argument: string]: any; }' is not assignable to type 'Pick<Post, "title" | "description">'.
  Property 'title' is missing in type '{ [argument: string]: any; }'.

EDIT 2: May have something to do with TypeScript's strictFunctionTypes, which is enabled when I have strict: true for tsconfig.json.

@jupl jupl changed the title Typing resolvers in TypeScript Typing arguments for resolvers in TypeScript Apr 8, 2018
@jupl jupl changed the title Typing arguments for resolvers in TypeScript Typing arguments for resolvers in TypeScript with strictFunctionTypes Apr 10, 2018
@dsebastien
Copy link

I concur, it is indeed related to strict mode..
It would be great to be able to simplify the function parameters :)

@stubailo
Copy link
Contributor

Hi! We're going to keep track of TypeScript improvements in #704

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants