Skip to content

Commit

Permalink
Merge pull request #4855 from lizhineng/refact/mergeable-types
Browse files Browse the repository at this point in the history
refact: auto-generated mergeable types
  • Loading branch information
robertu7 authored Sep 10, 2024
2 parents 3fed4e6 + ebd18dc commit 431fb14
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
27 changes: 27 additions & 0 deletions bin/generateMergeableTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { isObjectType, isInterfaceType } = require('graphql')

module.exports = {
plugin: (schema) => {
const typesMap = schema.getTypeMap()

let contents =
'/* Generated by bin/generateApolloMergeableTypes.js; DO NOT EDIT. */\n\n'

contents += 'export const mergeables = [\n'

Object.keys(typesMap).forEach((typeName) => {
const type = typesMap[typeName]

if (
(!typeName.startsWith('__') && isObjectType(type)) ||
isInterfaceType(type)
) {
contents += ` '${typeName}',\n`
}
})

contents += ']\n'

return contents
},
}
5 changes: 5 additions & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const config: CodegenConfig = {
'src/gql/graphql.ts': {
plugins: ['typescript', 'typescript-operations'],
},
'src/gql/mergeables.ts': {
plugins: [
'./bin/generateMergeableTypes.js'
],
},
},
}

Expand Down
13 changes: 6 additions & 7 deletions src/common/utils/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '~/common/enums'
import introspectionQueryResultData from '~/common/gql/fragmentTypes.json'
import { randomString } from '~/common/utils'
import { mergeables } from '~/gql/mergeables'

import { getIsomorphicCookie } from './cookie'
import { resolvers } from './resolvers'
Expand Down Expand Up @@ -206,14 +207,12 @@ export const createApolloClient = (
headers?: IncomingHttpHeaders
) => {
const cache = new InMemoryCache({
possibleTypes: introspectionQueryResultData,
possibleTypes: {
...introspectionQueryResultData,
Mergeable: mergeables,
},
typePolicies: {
Liker: { merge: true },
Official: { merge: true },
Recommendation: { merge: true },
UserInfo: { merge: true },
UserStatus: { merge: true },
CommentConnection: { merge: true },
Mergeable: { merge: true },
},
}).restore(initialState || {})

Expand Down

0 comments on commit 431fb14

Please sign in to comment.