Graphql-yoga: Type Checking for resolvers seems broken with the latest TypeScript version

Created on 26 Jun 2018  路  15Comments  路  Source: dotansimha/graphql-yoga

Apparently the latest version of TypeScript has stricter type checking. This is causing issues with the basic example on the README...

const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || "World"}`
  }
};

const server = new GraphQLServer({ typeDefs, resolvers });

This error is reported...

[ts]
Argument of type '{ typeDefs: string; resolvers: { Query: { hello: (_: any, { name }: { name: any; }) => string; };...' is not assignable to parameter of type 'Props<any, any, any>'.
  Types of property 'resolvers' are incompatible.
    Type '{ Query: { hello: (_: any, { name }: { name: any; }) => string; }; }' is not assignable to type 'IResolvers | undefined'.
      Type '{ Query: { hello: (_: any, { name }: { name: any; }) => string; }; }' is not assignable to type 'IResolvers'.
        Property 'Query' is incompatible with index signature.
          Type '{ hello: (_: any, { name }: { name: any; }) => string; }' is not assignable to type '(() => any) | IResolverObject | GraphQLScalarType'.
            Type '{ hello: (_: any, { name }: { name: any; }) => string; }' is not assignable to type 'GraphQLScalarType'.
              Property 'name' is missing in type '{ hello: (_: any, { name }: { name: any; }) => string; }'.
statustale

Most helpful comment

If you want to disable type-checking on the resolvers input to the GraphQLServer constructor, you can also do this instead:

const server = new GraphQLServer({
  typeDefs,
  resolvers: resolvers as any
});

All 15 comments

@cruhl can you put up a repro?

The reason this fails is because everywhere GraphQLFieldResolver<any, any> is used it should be
GraphQLFieldResolver<any, any, any> instead.

See the generic types here: type GraphQLFieldResolver<TSource, TContext, TArgs = { [argName: string]: any }>
To reproduce use a resolver that doesn't use arguments conforming to { [argName: string]: any }

note that the following won't compile (this might be what's new in typescript)

interface A { foo: string; }
const a: A = { foo: 'bar' };
const b: { [name: string]: string; } = a;

@nmaro Based on that explanation my PR may fix this issue then: https://github.com/prismagraphql/graphql-yoga/pull/369.

@jasonkuhrt I would, but I'm away from my computer for the holidays, sorry! Thanks for looking into this everyone...

418 also addresses this

any update on this? or a workaround ?

@cport1 #418

@cport1 My current workaround is to "erase" the types:

const Query: QueryResolvers.Resolvers = {
    async search() {
        return {
            count: 0,
            hits: []
        };
    }
};

// Work around for https://github.com/prisma/graphql-yoga/pull/418
const resolvers: {} = { Query };

const server = new GraphQLServer({
    typeDefs,
    resolvers
});

If you want to disable type-checking on the resolvers input to the GraphQLServer constructor, you can also do this instead:

const server = new GraphQLServer({
  typeDefs,
  resolvers: resolvers as any
});

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

Still requires the workaround

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

Hey :wave:, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of graphql-yoga we'll close it.
Feel free to reopen it at any time if you believe we should futher discuss its content. :slightly_smiling_face:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kv-pawar picture kv-pawar  路  3Comments

AhmadEl-Banna picture AhmadEl-Banna  路  5Comments

anthonymetzler picture anthonymetzler  路  4Comments

CaptainChemist picture CaptainChemist  路  4Comments

playerx picture playerx  路  5Comments