Skip to content

Commit

Permalink
fix(client): Only query is required in the subscribe payload
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Sep 10, 2020
1 parent 87c3b41 commit e892530
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export interface SubscribeMessage {
}

export interface SubscribePayload {
readonly operationName: string;
readonly operationName?: string | null;
readonly query: string | DocumentNode;
readonly variables: Record<string, unknown>;
readonly variables?: Record<string, unknown> | null;
}

export interface NextMessage {
Expand Down Expand Up @@ -100,10 +100,12 @@ export function isMessage(val: unknown): val is Message {
return (
hasOwnStringProperty(val, 'id') &&
hasOwnObjectProperty(val, 'payload') &&
hasOwnStringProperty(val.payload, 'operationName') &&
(!hasOwnProperty(val.payload, 'operationName') ||
hasOwnStringProperty(val.payload, 'operationName')) &&
(hasOwnStringProperty(val.payload, 'query') || // string query
hasOwnObjectProperty(val.payload, 'query')) && // document node query
hasOwnObjectProperty(val.payload, 'variables')
(!hasOwnProperty(val.payload, 'variables') ||
hasOwnObjectProperty(val.payload, 'variables'))
);
case MessageType.Next:
return (
Expand Down
17 changes: 4 additions & 13 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ describe('query operation', () => {

client.subscribe(
{
operationName: 'ValueGetter',
query: `query ValueGetter {
query: `query {
getValue
}`,
variables: {},
},
{
next: (result) => {
Expand Down Expand Up @@ -151,13 +149,11 @@ describe('subscription operation', () => {
const completeFnForBananas = jest.fn();
const disposeBananas = client.subscribe(
{
operationName: 'BoughtBananas',
query: `subscription BoughtBananas {
query: `subscription {
boughtBananas {
name
}
}`,
variables: {},
},
{
next: nextFnForBananas,
Expand Down Expand Up @@ -268,13 +264,11 @@ describe('"concurrency"', () => {

client.subscribe(
{
operationName: 'BoughtBananas',
query: `subscription BoughtBananas {
query: `subscription {
boughtBananas {
name
}
}`,
variables: {},
},
{
next: nextFnForBananas,
Expand Down Expand Up @@ -340,13 +334,11 @@ describe('lazy', () => {

client.subscribe(
{
operationName: 'BoughtBananas',
query: `subscription BoughtBananas {
query: `subscription {
boughtBananas {
name
}
}`,
variables: {},
},
{
next: noop,
Expand Down Expand Up @@ -377,7 +369,6 @@ describe('lazy', () => {
name
}
}`,
variables: {},
},
{
next: noop,
Expand Down
5 changes: 5 additions & 0 deletions src/tests/fixtures/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const schema = new GraphQLSchema({
fields: {
becameHappy: {
type: personType,
args: {
secret: {
type: new GraphQLNonNull(GraphQLString),
},
},
subscribe: () => {
return pubsub.asyncIterator('becameHappy');
},
Expand Down

0 comments on commit e892530

Please sign in to comment.