Reputation: 2597
Using apollo-link on the client, and PubSub from apollo-server-express on the server. Getting strange result in a mocha test talking to the GraphQL API:
mocha:
import { execute, makePromise } from 'apollo-link';
const uri = 'http://localhost:3001/graphql';
const link = new HttpLink({ uri, fetch });
const subscribe = (query, handlers) => {
const operation = {
query: gql`${query}`,
};
return execute(link, operation).subscribe(handlers);
};
const handlers = {
next: (data) => {
console.log(`received data: ${Date.now()}, ${JSON.stringify(data, null, 2)}`);
},
error: error => console.log(`received error ${error}`),
complete: () => console.log('complete'),
};
it('subscribe', async () => {
const query = `subscription {
info
}`;
subscribe(query, handlers);
});
Server:
try {
console.log('subscription =>| ', Date.now(), '|', line);
worker.pubsub.publish('infoTopic', { info: line });
} catch (e) {
console.error(e);
}
Here's what I'm seeing (from test):
received data: 1545013826838, { "errors": [ { "message": "Cannot return null for non-nullable field Subscription.info.",...
(from server):
subscription =>| 1545013826887 | info depth 1 seldepth 1 ...
Subscriber is receiving at 826838, but publisher is sending at 826887
What the heck?
Upvotes: 0
Views: 129
Reputation: 314
I just recently came upon this error. I found a solution to be I needed the field name of the object published match the field name of the subscription. To illustrate, notice the field name newPost
in the schema matches the name in the resolver, as well as the field name of the object being published to the channel and the subscription operation:
// schema
type Subscription {
newPost: Post!
}
// subscription resolver
newPost: {
subscribe(parent, args, { pubsub }, info)
return pubsub.asyncIterator('new post')
}
}
// in the event publisher
pubsub.publish('new post', { newPost: post })
// the subscription operation
subscription {
newPost {
id
title
body
published
}
}
Upvotes: 1