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

Improve TypeScript support and definitions #704

Closed
dsebastien opened this issue Apr 1, 2018 · 8 comments
Closed

Improve TypeScript support and definitions #704

dsebastien opened this issue Apr 1, 2018 · 8 comments

Comments

@dsebastien
Copy link

Hello,

In my project I'm currently giving Apollo Server (express) a try and am having some difficulties finding how to write type-safe mutation resolvers.

Given the following schema:

type Query {
    ...
}

type Mutation {
    reserveDeal(input: ReserveDealInput!): ReserveDealPayload
}

input ReserveDealInput {
    dealId: String!
}

type ReserveDealPayload {
    result: Boolean
}

If I write the following then my code compiles and I do have some type safety with the reserveDeal input

const resolvers = {
    Query: {
        deals: () => dummyDeals
    },
    Mutation: {
        //{ input: ReserveDealInput }
        reserveDeal: (root: any, arg: { input: ReserveDealInput } | any): ReserveDealPayload => {
            console.log("Reserving deal: ", arg.input.dealId);
            return {result: true};
        }
    }
};

The problem is the | any that I had to add to work around the following compilation issue with makeExecutableSchema:

src/index.ts(81,59): error TS2345: Argument of type '{ typeDefs: string; resolvers: { Query: { deals: () => { id: string; title: string; description: ...' is not assignable to parameter of type 'IExecutableSchemaDefinition<any>'.
  Types of property 'resolvers' are incompatible.
    Type '{ Query: { deals: () => { id: string; title: string; description: string; }[]; }; Mutation: { res...' is not assignable to type 'IResolvers<any, any> | IResolvers<any, any>[] | undefined'.
      Type '{ Query: { deals: () => { id: string; title: string; description: string; }[]; }; Mutation: { res...' is not assignable to type 'IResolvers<any, any>[]'.
        Property 'includes' is missing in type '{ Query: { deals: () => { id: string; title: string; description: string; }[]; }; Mutation: { res...'.

Do you have any ideas/pointers to help me improve this code towards better type safety?

@sundeer
Copy link

sundeer commented May 14, 2018

same here - what's the 'includes' property?

@lucasbento
Copy link

lucasbento commented May 24, 2018

I'm getting the same, did anyone manage to solve this?

@lucasbento
Copy link

Similar issues: #372, #583 & DefinitelyTyped/DefinitelyTyped#21359.

@stubailo stubailo changed the title makeExecutableSchema & TypeScript Improve TypeScript support and definitions Jul 13, 2018
@stubailo
Copy link
Contributor

Also related: #404

@affanshahid
Copy link

Updates?

@FedeBev
Copy link

FedeBev commented Jan 8, 2019

Any news?

Should be enough to change IFieldResolver definition from

export declare type IFieldResolver<TSource, TContext, TArgs = {
    [argument: string]: any;
}> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
    mergeInfo: MergeInfo;
}) => any;

to

export declare type IFieldResolver<TSource, TContext, TArgs = any> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
    mergeInfo: MergeInfo;
}) => any;

Shouldn't be?

@yaacovCR
Copy link
Collaborator

Pull requests now welcome!

@yaacovCR
Copy link
Collaborator

yaacovCR commented Apr 1, 2020

This looks potentially fixed in #1307, closing for now, please check out graphql-tools@next and reopen as necessary.

@yaacovCR yaacovCR closed this as completed Apr 1, 2020
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

7 participants