Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
feat(graphiql-fetcher): better detection for subscription and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Urigo committed Mar 18, 2017
1 parent 0053c93 commit 0e97395
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/graphiql-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { SubscriptionClient } from './client';

const hasSubscriptionOperation = (graphQlParams: any) => {
const query = graphQlParams.query;
const operationName = graphQlParams.operationName;

if (operationName && operationName !== '') {
return query.includes(`subscription ${operationName}`);
}

return query.includes('subscription {') ||
query.includes('subscription{');
};

export const graphQLFetcher = (subscriptionsClient: SubscriptionClient, fallbackFetcher: Function) => {
let activeSubscriptionId: number | null = null;

Expand All @@ -8,7 +20,7 @@ export const graphQLFetcher = (subscriptionsClient: SubscriptionClient, fallback
subscriptionsClient.unsubscribe(activeSubscriptionId);
}

if (subscriptionsClient && graphQLParams.query.startsWith('subscription')) {
if (subscriptionsClient && hasSubscriptionOperation(graphQLParams)) {
return {
subscribe: (observer: { error: Function, next: Function }) => {
observer.next('Your subscription data will appear here after server publication!');
Expand Down
70 changes: 70 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
GRAPHQL_SUBSCRIPTIONS,
} from '../protocols';

import { graphQLFetcher } from '../graphiql-fetcher';

import {createServer, IncomingMessage, ServerResponse} from 'http';
import {SubscriptionServer} from '../server';
import {SubscriptionClient} from '../client';
Expand Down Expand Up @@ -1268,3 +1270,71 @@ describe('Server', function () {
};
});
});

describe('GraphiQL Fetcher', () => {
let subscriptionsClient = {
subscribe: sinon.spy(),
unsubscribe: sinon.spy(),
};

let fallbackFetcher = sinon.spy();

let fetcher: Function;

beforeEach(() => {
subscriptionsClient.subscribe.reset();
subscriptionsClient.unsubscribe.reset();
fallbackFetcher.reset();
fetcher = graphQLFetcher(<any>subscriptionsClient, fallbackFetcher);
});

it('should use subscriptions fetcher when using with named operation', () => {
const ret = fetcher({
query: 'subscription commentAdded { field }',
operationName: 'commentAdded',
});

expect(ret.subscribe).not.to.equals(undefined);
});

it('should use subscriptions fetcher when using with named operation with fragments', () => {
const ret = fetcher({
query: 'fragment f { field } subscription commentAdded { ...f }',
operationName: 'commentAdded',
});

expect(ret.subscribe).not.to.equals(undefined);
});

it('should use subscriptions fetcher when using with operation with fragments', () => {
const ret = fetcher({
query: 'fragment f { field } subscription { ...f }',
});

expect(ret.subscribe).not.to.equals(undefined);
});

it('should use subscriptions fetcher when using with operation', () => {
const ret = fetcher({
query: 'subscription { ...f }',
});

expect(ret.subscribe).not.to.equals(undefined);
});

it('should use fallback fetcher when using query', () => {
fetcher({
query: 'query { field }',
});

assert(fallbackFetcher.calledOnce);
});

it('should use fallback fetcher when using mutations', () => {
fetcher({
query: 'mutation { field }',
});

assert(fallbackFetcher.calledOnce);
});
});

0 comments on commit 0e97395

Please sign in to comment.