Skip to content

Commit

Permalink
fix response cache usage with subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin committed Sep 20, 2023
1 parent dd2d21e commit 9e2740d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 38 deletions.
57 changes: 56 additions & 1 deletion packages/plugins/response-cache/__tests__/response-cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSchema, createYoga } from 'graphql-yoga';
import { createSchema, createYoga, Repeater } from 'graphql-yoga';
import { cacheControlDirective } from '@envelop/response-cache';
import { useDeferStream } from '@graphql-yoga/plugin-defer-stream';
import { useResponseCache } from '@graphql-yoga/plugin-response-cache';
Expand Down Expand Up @@ -781,3 +781,58 @@ describe('should support async results', () => {
});
});
});

it('should allow subscriptions and ignore it', async () => {
const source = (async function* foo() {
yield { hi: 'hi' };
yield { hi: 'hello' };
yield { hi: 'bonjour' };
})();

const schema = createSchema({
typeDefs: /* GraphQL */ `
type Subscription {
hi: String!
}
type Query {
hi: String!
}
`,
resolvers: {
Subscription: {
hi: {
subscribe: () => source,
},
},
},
});

const yoga = createYoga({
schema,
plugins: [
useResponseCache({
session: () => null,
}),
],
});

const response = await yoga.fetch('http://yoga/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'text/event-stream',
},
body: JSON.stringify({
query: /* GraphQL */ `
subscription {
hi
}
`,
}),
});

const result = await response.text();
expect(result).toContain(JSON.stringify({ data: { hi: 'hi' } }));
expect(result).toContain(JSON.stringify({ data: { hi: 'hello' } }));
expect(result).toContain(JSON.stringify({ data: { hi: 'bonjour' } }));
});
2 changes: 1 addition & 1 deletion packages/plugins/response-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"graphql-yoga": "^4.0.4"
},
"dependencies": {
"@envelop/response-cache": "5.3.0"
"@envelop/response-cache": "5.3.1-alpha-20230920124022-44708a76"
},
"devDependencies": {
"graphql": "^16.6.0",
Expand Down
72 changes: 36 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9e2740d

Please sign in to comment.