UNPKG

@graphql-codegen/typescript-nhost

Version:
41 lines (40 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = void 0; const typescript_1 = require("@graphql-codegen/typescript"); const introspection_1 = require("@urql/introspection"); const plugin = (schema, documents, config) => { // * We don't allow the other @graphql-codegen/typescript options as they could break the generator and the types. const { scalars } = config; const conf = { scalars, declarationKind: 'type', enumsAsTypes: true, // * Add underscore to args type to avoid conflicts with the generated types // * See: https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#addunderscoretoargstype addUnderscoreToArgsType: true, }; const minifiedData = (0, introspection_1.minifyIntrospectionQuery)((0, introspection_1.getIntrospectedSchema)(schema), { includeDirectives: false, includeEnums: true, includeInputs: true, includeScalars: true, }); const { prepend, content } = (0, typescript_1.plugin)(schema, documents, conf); // * https://stackoverflow.com/questions/2008279/validate-a-javascript-function-name const types = Array.from(content.matchAll(/^export type ([$A-Z_][0-9A-Z_$]*) = /gim)) .map(([, name]) => `\n${name}: ${name}`) .join(','); const result = ` export default { introspection: ${JSON.stringify(minifiedData, null, 2).replace(/^/gm, ' ').slice(2)} as const, types: {} as {${types.replace(/^/gm, ' ').slice(4)} } }`; return { prepend, content: [content, result].join('\n'), }; }; exports.plugin = plugin; exports.default = exports.plugin;