Skip to content

Commit

Permalink
fix: ensure subscription queries are normalized closes #188
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Feb 8, 2023
1 parent 5da0919 commit 7a50249
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/villus/src/handleSubscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { normalizeQuery } from '../../shared/src/utils';
import { ClientPlugin, ClientPluginOperation, ObservableLike, StandardOperationResult } from './types';

export type SubscriptionForwarder<TData = any> = (
operation: ClientPluginOperation
operation: ClientPluginOperation & { query: string }
) => ObservableLike<StandardOperationResult<TData>>;

export function handleSubscriptions(forwarder: SubscriptionForwarder): ClientPlugin {
Expand All @@ -16,6 +17,11 @@ export function handleSubscriptions(forwarder: SubscriptionForwarder): ClientPlu
throw new Error('No subscription forwarder was set.');
}

useResult(forward(operation) as any, true);
const normalizedQuery = normalizeQuery(operation.query);
if (!normalizedQuery) {
throw new Error('A query must be provided.');
}

useResult(forward({ ...operation, query: normalizedQuery }) as any, true);
};
}
56 changes: 55 additions & 1 deletion packages/villus/test/useSubscription.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable jest/no-conditional-expect */
/* eslint-disable no-unused-expressions */
import flushPromises from 'flush-promises';
import gql from 'graphql-tag';
import { mount } from './helpers/mount';
import { makeObservable, tick } from './helpers/observer';
import { defaultPlugins, handleSubscriptions, useClient, useSubscription } from '../src/index';
import { computed, ref } from 'vue';
import { print } from 'graphql';

jest.useFakeTimers();

Expand Down Expand Up @@ -350,7 +352,7 @@ test('handles subscription errors', async () => {
<div v-if="messages && !error">
<span>{{ messages.id }}</span>
</div>
<span id="error" v-if="error">{{ error }}</span>
<span id="error" v-if="error">{{ error }}</span>
</div>
`,
});
Expand All @@ -360,3 +362,55 @@ test('handles subscription errors', async () => {
await flushPromises();
expect(document.querySelector('#error')?.textContent).toBeTruthy();
});

test('normalizes subscription queries', async () => {
const spy = jest.fn();
mount({
setup() {
useClient({
url: 'https://test.com/graphql',
use: [
handleSubscriptions(op => {
spy(op.query);

return makeObservable();
}),
...defaultPlugins(),
],
});

const { data, error } = useSubscription<Message>({
query: gql`
subscription {
newMessages
}
`,
variables: { id: 2 },
});

return { messages: data.value, error };
},
template: `
<div>
<div v-if="messages && !error">
<span>{{ messages.id }}</span>
</div>
<span id="error" v-if="error">{{ error }}</span>
</div>
`,
});

await flushPromises();
await tick(1);
await flushPromises();
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(
print(
gql`
subscription {
newMessages
}
`
)
);
});

0 comments on commit 7a50249

Please sign in to comment.