Graphql-tools: Subscription - Property 'Subscription' is incompatible with index signature.

Created on 1 Aug 2017  Â·  2Comments  Â·  Source: ardatan/graphql-tools

Hello,

I got this error when I tried to execute this code. Do you have any idea?

src/index.ts(40,49): error TS2345: Argument of type '{ typeDefs: string; resolvers: { Query: { addCell: (: any, { type }: { type: any; }) => Prom...' is not assignable to parameter of type 'IExecutableSchemaDefinition'.
Types of property 'resolvers' are incompatible.
Type '{ Query: { addCell: (: any, { type }: { type: any; }) => Promise; }; Subscription: ...' is not assignable to type 'IResolvers'.
Property 'Subscription' is incompatible with index signature.
Type '{ newCell: { subscribe: (: any, { type }: { type: any; }) => AsyncIterator<{}>; }; }' is not assignable to type 'GraphQLScalarType | IResolverObject | (() => any)'.
Type '{ newCell: { subscribe: (: any, { type }: { type: any; }) => AsyncIterator<{}>; }; }' is not assignable to type '() => any'.
Type '{ newCell: { subscribe: (_: any, { type }: { type: any; }) => AsyncIterator<{}>; }; }' provides no match for the signature '(): any'.

import { PubSub } from 'graphql-subscriptions';
import { makeExecutableSchema } from 'graphql-tools';

const pubsub = new PubSub();

export const typeDefs = `

type Subscription {
  newCell: Cell
}

type Query {
  addCell(type: CellType!): Cell
}
[..]
schema {
  query: Query
  subscription: Subscription
}
`;

export const resolvers = {
  Query: {
    addCell: async (_, { type }) => {
      let cell = new Cell();
      cell.type = type;
      pubsub.publish('newCell', { newCell: cell });
      return cell;
    },
  },

  Subscription: {
    newCell: {
      subscribe: () =>
        pubsub.asyncIterator('newCell')
    }
  }
};

 let executableSchema = makeExecutableSchema({
      typeDefs,
      resolvers,
    });

The issue is related to the "subscribe" attribute.

Thanks

Most helpful comment

I'm getting a similar error:

export const resolvers: IResolvers = {
  Query: {
    hello: () => {
      return `Hello world`;
    },
    allLinks: () => {
      return [{
        id: "what",
        url: "dudewheresmycar.com",
        description: "Lekker bois.",
      }, {
        id: "numbertwo",
        url: "independence.com",
        description: "No man.",
      }];
    }
  },
  Subscription: {
    taskProgress: {
      subscribe: withFilter(() => pubsub.asyncIterator("taskProgress"), (payload, variables) => {
        return payload.taskId === variables.taskId;
      }),
    },
  },
};
TSError: ⨯ Unable to compile TypeScript
src\graphql\resolvers.ts (5,14): Type '{ Query: { hello: () => string; allLinks: () => { id: string; url: string; description: string; }...' is not assignable to type 'IResolvers'.
  Property 'Subscription' is incompatible with index signature.
    Type '{ taskProgress: { subscribe: Function; }; }' is not assignable to type '(() => any) | IResolverObject | GraphQLScalarType'.
      Type '{ taskProgress: { subscribe: Function; }; }' is not assignable to type 'GraphQLScalarType'.
        Property 'name' is missing in type '{ taskProgress: { subscribe: Function; }; }'. (2322)

It seems that the types are not working properly. As it's seeing taskProgress over there as a GraphQLScalarType instead of an IResolverObject.

I managed to make the above work by forcing it to see it as an IResolverObject, like so:

Subscription: {
    taskProgress: {
      subscribe: withFilter(() => pubsub.asyncIterator("taskProgress"), (payload, variables) => {
        return payload.taskId === variables.taskId;
      }),
    } as IResolverObject,
  },

All 2 comments

I'm getting a similar error:

export const resolvers: IResolvers = {
  Query: {
    hello: () => {
      return `Hello world`;
    },
    allLinks: () => {
      return [{
        id: "what",
        url: "dudewheresmycar.com",
        description: "Lekker bois.",
      }, {
        id: "numbertwo",
        url: "independence.com",
        description: "No man.",
      }];
    }
  },
  Subscription: {
    taskProgress: {
      subscribe: withFilter(() => pubsub.asyncIterator("taskProgress"), (payload, variables) => {
        return payload.taskId === variables.taskId;
      }),
    },
  },
};
TSError: ⨯ Unable to compile TypeScript
src\graphql\resolvers.ts (5,14): Type '{ Query: { hello: () => string; allLinks: () => { id: string; url: string; description: string; }...' is not assignable to type 'IResolvers'.
  Property 'Subscription' is incompatible with index signature.
    Type '{ taskProgress: { subscribe: Function; }; }' is not assignable to type '(() => any) | IResolverObject | GraphQLScalarType'.
      Type '{ taskProgress: { subscribe: Function; }; }' is not assignable to type 'GraphQLScalarType'.
        Property 'name' is missing in type '{ taskProgress: { subscribe: Function; }; }'. (2322)

It seems that the types are not working properly. As it's seeing taskProgress over there as a GraphQLScalarType instead of an IResolverObject.

I managed to make the above work by forcing it to see it as an IResolverObject, like so:

Subscription: {
    taskProgress: {
      subscribe: withFilter(() => pubsub.asyncIterator("taskProgress"), (payload, variables) => {
        return payload.taskId === variables.taskId;
      }),
    } as IResolverObject,
  },

I like the idea of rolling these up into a single issue about improving TypeScript definitions and compatibility. Let's do that in #704

Was this page helpful?
0 / 5 - 0 ratings

Related issues

udisun picture udisun  Â·  3Comments

tonyxiao picture tonyxiao  Â·  4Comments

alfaproject picture alfaproject  Â·  4Comments

stubailo picture stubailo  Â·  3Comments

Adherentman picture Adherentman  Â·  4Comments