Skip to content

Commit b62cb34

Browse files
committed
fix(typescript-urql-graphcache): improve typing
- resolve a duplicate import - improve typing for "empty object" - respect `defaultScalarType`
1 parent e5ceb5b commit b62cb34

4 files changed

Lines changed: 424 additions & 37 deletions

File tree

.changeset/spicy-paths-lose.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-urql-graphcache': patch
3+
---
4+
5+
Resolve a duplicate import, improve typing for "empty object" and respect `defaultScalarType`

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,9 @@ function getOptimisticUpdatersConfig(
276276
}
277277

278278
function getImports(config: UrqlGraphCacheConfig): string {
279-
return [
280-
`import { ${
281-
config.offlineExchange ? 'offlineExchange' : 'cacheExchange'
282-
} } from '@urql/exchange-graphcache';`,
283-
`${
284-
config.useTypeImports ? 'import type' : 'import'
285-
} { Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';\n`,
286-
].join('\n');
279+
return `${config.useTypeImports ? 'import type' : 'import'} { ${
280+
config.offlineExchange ? 'offlineExchange' : 'cacheExchange'
281+
}, Resolver as GraphCacheResolver, UpdateResolver as GraphCacheUpdateResolver, OptimisticMutationResolver as GraphCacheOptimisticMutationResolver } from '@urql/exchange-graphcache';\n`;
287282
}
288283

289284
export const plugin: PluginFunction<UrqlGraphCacheConfig, Types.ComplexPluginOutput> = (
@@ -305,24 +300,24 @@ export const plugin: PluginFunction<UrqlGraphCacheConfig, Types.ComplexPluginOut
305300
return {
306301
prepend: [imports],
307302
content: [
308-
`export type WithTypename<T extends { __typename?: any }> = Partial<T> & { __typename: NonNullable<T['__typename']> };`,
303+
`export type WithTypename<T extends { __typename?: ${config.defaultScalarType || 'any'} }> = Partial<T> & { __typename: NonNullable<T['__typename']> };`,
309304

310305
keys,
311306

312307
'export type GraphCacheResolvers = {\n' + resolvers.join(',\n') + '\n};',
313308

314309
'export type GraphCacheOptimisticUpdaters = ' +
315-
(optimisticUpdaters ? '{\n ' + optimisticUpdaters.join(',\n ') + '\n};' : '{};'),
310+
(optimisticUpdaters ? '{\n ' + optimisticUpdaters.join(',\n ') + '\n};' : 'object;'),
316311

317312
'export type GraphCacheUpdaters = {\n' +
318313
` ${(queryType && queryType.name) || 'Mutation'}?: ` +
319-
(queryUpdaters ? `{\n ${queryUpdaters.join(',\n ')}\n }` : '{}') +
314+
(queryUpdaters ? `{\n ${queryUpdaters.join(',\n ')}\n }` : 'object') +
320315
',\n' +
321316
` ${(mutationType && mutationType.name) || 'Mutation'}?: ` +
322-
(mutationUpdaters ? `{\n ${mutationUpdaters.join(',\n ')}\n }` : '{}') +
317+
(mutationUpdaters ? `{\n ${mutationUpdaters.join(',\n ')}\n }` : 'object') +
323318
',\n' +
324319
` ${(subscriptionType && subscriptionType.name) || 'Subscription'}?: ` +
325-
(subscriptionUpdaters ? `{\n ${subscriptionUpdaters.join(',\n ')}\n }` : '{}') +
320+
(subscriptionUpdaters ? `{\n ${subscriptionUpdaters.join(',\n ')}\n }` : 'object') +
326321
',\n' +
327322
`${typeUpdateResolvers.join(',\n')}` +
328323
',\n};',

0 commit comments

Comments
 (0)