Skip to content

Commit 9bdae5d

Browse files
authored
feat(graphcache): make offline exchange optional (#476)
* make offline exchange optional * update tests * switch to proper import * update assertions * fix
1 parent 64ba0e1 commit 9bdae5d

6 files changed

Lines changed: 30 additions & 18 deletions

File tree

.changeset/fresh-cooks-learn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/typescript-urql-graphcache': minor
3+
---
4+
5+
Add setting to choose for the offlineExchange, otherwise this bugs out the types for folks not using
6+
the offlineExchange

dev-test/githunt/types.urql.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IntrospectionQuery } from 'graphql';
22
import gql from 'graphql-tag';
33
import * as Urql from 'urql';
4-
import { offlineExchange } from '@urql/exchange-graphcache';
4+
import { cacheExchange } from '@urql/exchange-graphcache';
55
import {
66
OptimisticMutationResolver as GraphCacheOptimisticMutationResolver,
77
Resolver as GraphCacheResolver,
@@ -1293,7 +1293,7 @@ export type GraphCacheUpdaters = {
12931293
};
12941294
};
12951295

1296-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
1296+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
12971297
updates?: GraphCacheUpdaters;
12981298
keys?: GraphCacheKeysConfig;
12991299
optimistic?: GraphCacheOptimisticUpdaters;

packages/plugins/typescript/urql-graphcache/src/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ import { RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-c
33
/**
44
* @description This plugin generates a generic for the `@urql/exchange-graphcache` (https://github.com/FormidableLabs/urql/exchanges/graphcache) config.
55
*/
6-
export type UrqlGraphCacheConfig = RawClientSideBasePluginConfig & {};
6+
export type UrqlGraphCacheConfig = RawClientSideBasePluginConfig & {
7+
offlineExchange?: boolean;
8+
};

packages/plugins/typescript/urql-graphcache/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ function getOptimisticUpdatersConfig(
277277

278278
function getImports(config: UrqlGraphCacheConfig): string {
279279
return [
280-
"import { offlineExchange } from '@urql/exchange-graphcache';",
280+
`import { ${
281+
config.offlineExchange ? 'offlineExchange' : 'cacheExchange'
282+
} } from '@urql/exchange-graphcache';`,
281283
`${
282284
config.useTypeImports ? 'import type' : 'import'
283285
} { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';\n`,
@@ -325,7 +327,9 @@ export const plugin: PluginFunction<UrqlGraphCacheConfig, Types.ComplexPluginOut
325327
`${typeUpdateResolvers.join(',\n')}` +
326328
',\n};',
327329

328-
'export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {\n' +
330+
`export type GraphCacheConfig = Parameters<typeof ${
331+
config.offlineExchange ? 'offlineExchange' : 'cacheExchange'
332+
}>[0] & {\n` +
329333
' updates?: GraphCacheUpdaters,\n' +
330334
' keys?: GraphCacheKeysConfig,\n' +
331335
' optimistic?: GraphCacheOptimisticUpdaters,\n' +

packages/plugins/typescript/urql-graphcache/tests/__snapshots__/urql.spec.ts.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`urql graphcache Should correctly name GraphCacheResolvers & GraphCacheOptimisticUpdaters with nonstandard mutationType names 1`] = `
4-
"import { offlineExchange } from '@urql/exchange-graphcache';
4+
"import { cacheExchange } from '@urql/exchange-graphcache';
55
import { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
66
77
export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };
@@ -40,7 +40,7 @@ export type GraphCacheUpdaters = {
4040
},
4141
};
4242
43-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
43+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
4444
updates?: GraphCacheUpdaters,
4545
keys?: GraphCacheKeysConfig,
4646
optimistic?: GraphCacheOptimisticUpdaters,
@@ -49,7 +49,7 @@ export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
4949
`;
5050
5151
exports[`urql graphcache Should correctly output GraphCacheOptimisticUpdaters when there are no mutations 1`] = `
52-
"import { offlineExchange } from '@urql/exchange-graphcache';
52+
"import { cacheExchange } from '@urql/exchange-graphcache';
5353
import { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
5454
5555
export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };
@@ -84,7 +84,7 @@ export type GraphCacheUpdaters = {
8484
},
8585
};
8686
87-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
87+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
8888
updates?: GraphCacheUpdaters,
8989
keys?: GraphCacheKeysConfig,
9090
optimistic?: GraphCacheOptimisticUpdaters,
@@ -93,7 +93,7 @@ export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
9393
`;
9494
9595
exports[`urql graphcache Should output the cache-generic correctly (with interfaces) 1`] = `
96-
"import { offlineExchange } from '@urql/exchange-graphcache';
96+
"import { cacheExchange } from '@urql/exchange-graphcache';
9797
import { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
9898
9999
export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };
@@ -156,7 +156,7 @@ export type GraphCacheUpdaters = {
156156
},
157157
};
158158
159-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
159+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
160160
updates?: GraphCacheUpdaters,
161161
keys?: GraphCacheKeysConfig,
162162
optimistic?: GraphCacheOptimisticUpdaters,
@@ -165,7 +165,7 @@ export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
165165
`;
166166
167167
exports[`urql graphcache Should output the cache-generic correctly (with typesPrefix and typesSuffix) 1`] = `
168-
"import { offlineExchange } from '@urql/exchange-graphcache';
168+
"import { cacheExchange } from '@urql/exchange-graphcache';
169169
import { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
170170
171171
export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };
@@ -227,7 +227,7 @@ export type GraphCacheUpdaters = {
227227
},
228228
};
229229
230-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
230+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
231231
updates?: GraphCacheUpdaters,
232232
keys?: GraphCacheKeysConfig,
233233
optimistic?: GraphCacheOptimisticUpdaters,
@@ -236,7 +236,7 @@ export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
236236
`;
237237
238238
exports[`urql graphcache Should output the cache-generic correctly (with unions) 1`] = `
239-
"import { offlineExchange } from '@urql/exchange-graphcache';
239+
"import { cacheExchange } from '@urql/exchange-graphcache';
240240
import { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
241241
242242
export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };
@@ -286,7 +286,7 @@ export type GraphCacheUpdaters = {
286286
},
287287
};
288288
289-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
289+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
290290
updates?: GraphCacheUpdaters,
291291
keys?: GraphCacheKeysConfig,
292292
optimistic?: GraphCacheOptimisticUpdaters,
@@ -295,7 +295,7 @@ export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
295295
`;
296296
297297
exports[`urql graphcache Should output the cache-generic correctly 1`] = `
298-
"import { offlineExchange } from '@urql/exchange-graphcache';
298+
"import { cacheExchange } from '@urql/exchange-graphcache';
299299
import { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
300300
301301
export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };
@@ -357,7 +357,7 @@ export type GraphCacheUpdaters = {
357357
},
358358
};
359359
360-
export type GraphCacheConfig = Parameters<typeof offlineExchange>[0] & {
360+
export type GraphCacheConfig = Parameters<typeof cacheExchange>[0] & {
361361
updates?: GraphCacheUpdaters,
362362
keys?: GraphCacheKeysConfig,
363363
optimistic?: GraphCacheOptimisticUpdaters,

packages/plugins/typescript/urql-graphcache/tests/urql.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe('urql graphcache', () => {
166166
const result = mergeOutputs([await plugin(schema, [], { useTypeImports: true })]);
167167

168168
expect(result).toBeSimilarStringTo(`\
169-
import { offlineExchange } from '@urql/exchange-graphcache';
169+
import { cacheExchange } from '@urql/exchange-graphcache';
170170
import type { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';
171171
`);
172172
});

0 commit comments

Comments
 (0)