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

fix response cache usage with subscriptions #3006

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-yoga/plugin-response-cache': patch
---
dependencies updates:
- Updated dependency [`@envelop/response-cache@^5.3.2`
↗︎](https://www.npmjs.com/package/@envelop/response-cache/v/5.3.2) (from `^5.3.0`, in
`dependencies`)
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 { createInMemoryCache, useResponseCache } from '@graphql-yoga/plugin-response-cache';
Expand Down Expand Up @@ -782,6 +782,61 @@ 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' } }));
});

it('should allow to create the cache outside of the plugin', async () => {
const onEnveloped = jest.fn();
const yoga = createYoga({
Expand Down
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.2"
},
"devDependencies": {
"graphql": "^16.6.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/response-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface Cache extends EnvelopCache {
export function useResponseCache(options: UseResponseCacheParameter): Plugin {
const buildResponseCacheKey: BuildResponseCacheKeyFunction =
options?.buildResponseCacheKey || defaultBuildResponseCacheKey;
const cache = options.cache ?? (createInMemoryCache() as Cache);
const cache = options.cache ?? createInMemoryCache();
const enabled = options.enabled ?? (() => true);
let logger: YogaLogger;
return {
Expand Down
Loading
Loading