Skip to content

Instantly share code, notes, and snippets.

@JoSuzuki
Created March 31, 2021 14:12
Show Gist options
  • Save JoSuzuki/0acca276a0aaa108e101be35dd02c218 to your computer and use it in GitHub Desktop.
Save JoSuzuki/0acca276a0aaa108e101be35dd02c218 to your computer and use it in GitHub Desktop.
A custom plugin for graphql-codegen to generate mergeable types
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