Created
March 31, 2021 14:12
-
-
Save JoSuzuki/0acca276a0aaa108e101be35dd02c218 to your computer and use it in GitHub Desktop.
A custom plugin for graphql-codegen to generate mergeable types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { isInterfaceType, isObjectType } = require('graphql'); | |
module.exports = { | |
plugin: (schema, _documents, _config) => { | |
const typesMap = schema.getTypeMap(); | |
let fileString = | |
'/* This is an auto-generated file by generate-apollo-mergeable-types.js */\n\n'; | |
fileString += 'export const mergeableTypesArray = [\n'; | |
Object.keys(typesMap).forEach(typeName => { | |
const type = typesMap[typeName]; | |
if ( | |
(!typeName.startsWith('__') && isObjectType(type)) || | |
isInterfaceType(type) | |
) { | |
fileString += ` '${typeName}',\n`; | |
} | |
}); | |
fileString += '];\n'; | |
return fileString; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment