Skip to content

Resolver doesn't work with subscription #38

@josephktcheung

Description

@josephktcheung

Subscription fails if a field is resolved by a resolver.

Reproduction Steps

demo code available at https://github.com/josephktcheung/vesper/tree/feature/resolver_subscription, run sample 11 with npx ts-node sample/typescript/sample11-subscribers/index.ts

  1. Add a new test field for Message
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Message {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    text: string;

    @Column()
    receiver: number;

    test: string;
}
  1. Add a resolver for the new field.
import { Resolve, Resolver, ResolverInterface } from "../../../../src";
import { EntityManager } from "typeorm";
import { Message } from "../entity/Message";

@Resolver(Message)
export class MessageResolver implements ResolverInterface<Message> {
    constructor(private entityManager: EntityManager) {
    }

    @Resolve()
    async test(messages: Message[]) {
        return messages.map(m => 'test');
    }
}
  1. Include resolver in bootstrap:
import { bootstrap } from "../../../src";
import { MessageController } from "./controller/MessageController";
import { Message } from "./entity/Message";
import { PubSub } from "graphql-subscriptions";
import { MessageResolver } from "./resolver/MessageResolver";

const pubSub = new PubSub();

bootstrap({
    port: 3000,
    controllers: [MessageController],
    resolvers: [MessageResolver],
    entities: [Message],
    schemas: [__dirname + "/schema/**/*.graphql"],
    setupContainer: container => container.set(PubSub, pubSub),
    subscriptionAsyncIterator: triggers => pubSub.asyncIterator(triggers)
});
  1. Add test field to Message.graphql
type Message {
    id: Int
    text: String
    receiver: Int
    test: String
}
  1. Subscribe to messageSent with new test field:
subscription {
  messageSent(me: 1) {
    id
    text
    test
  }
}
  1. Create a new message
mutation {
  messageSave(id: 1, receiver: 1, text: "Hi") {
    id
    text
    test
  }
}
  1. Following error appears in console:
TypeError: Cannot read property 'Message' of undefined
    at resolvers.(anonymous function).(anonymous function) (/Users/josephcheung/workspace/zeep/repos/vesper/src/SchemaBuilder.ts:374:45)
    at /Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql-tools/src/schemaGenerator.ts:683:22
    at resolveFieldValueOrError (/Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:531:18)
    at resolveField (/Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:495:16)
    at /Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:364:18
    at Array.reduce (<anonymous>)
    at executeFields (/Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:361:42)
    at collectAndExecuteSubfields (/Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:772:10)
    at completeObjectValue (/Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:762:10)
    at completeValue (/Users/josephcheung/workspace/zeep/repos/vesper/node_modules/graphql/execution/execute.js:660:12)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions