Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Subscribe message query must be a string #45

Merged
merged 3 commits into from
Nov 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: query can be only of string type
  • Loading branch information
enisdenjo committed Nov 3, 2020
commit d36b8664051621a0cfa3145ae1fe2b3fab5a4863
7 changes: 3 additions & 4 deletions src/message.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
*
*/

import { GraphQLError, ExecutionResult, DocumentNode } from 'graphql';
import { GraphQLError, ExecutionResult } from 'graphql';
import {
isObject,
areGraphQLErrors,
@@ -41,7 +41,7 @@ export interface SubscribeMessage {

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

@@ -104,8 +104,7 @@ export function isMessage(val: unknown): val is Message {
val.payload.operationName === undefined ||
val.payload.operationName === null ||
typeof val.payload.operationName === 'string') &&
(hasOwnStringProperty(val.payload, 'query') || // string query or persisted query id
hasOwnObjectProperty(val.payload, 'query')) && // document node query
hasOwnStringProperty(val.payload, 'query') &&
(!hasOwnProperty(val.payload, 'variables') ||
val.payload.variables === undefined ||
val.payload.variables === null ||
3 changes: 1 addition & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -567,11 +567,10 @@ export function createServer(
}

const { operationName, query, variables } = message.payload;
const document = typeof query === 'string' ? parse(query) : query;
execArgs = {
schema,
operationName,
document,
document: parse(query),
variableValues: variables,
};

45 changes: 0 additions & 45 deletions src/tests/server.ts
Original file line number Diff line number Diff line change
@@ -1164,51 +1164,6 @@ describe('Subscribe', () => {
});
});

it('should execute the query of `DocumentNode` type, "next" the result and then "complete"', async () => {
const { url } = await startTServer({
schema,
});

const client = await createTClient(url);
client.ws.send(
stringifyMessage<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);

await client.waitForMessage(({ data }) => {
expect(parseMessage(data).type).toBe(MessageType.ConnectionAck);
client.ws.send(
stringifyMessage<MessageType.Subscribe>({
id: '1',
type: MessageType.Subscribe,
payload: {
operationName: 'TestString',
query: parse(`query TestString {
getValue
}`),
variables: {},
},
}),
);
});

await client.waitForMessage(({ data }) => {
expect(parseMessage(data)).toEqual({
id: '1',
type: MessageType.Next,
payload: { data: { getValue: 'value' } },
});
});

await client.waitForMessage(({ data }) => {
expect(parseMessage(data)).toEqual({
id: '1',
type: MessageType.Complete,
});
});
});

it('should execute the query and "error" out because of validation errors', async () => {
const { url } = await startTServer({
schema,