From e89358e79c5c3df151c4a9e471f6dedfc0167a2a Mon Sep 17 00:00:00 2001 From: Nic Date: Tue, 28 Nov 2017 00:51:40 +1100 Subject: [PATCH] fix: Add support for 'union' keyword --- src/graphqls2s.js | 43 ++++++++++++++++++++++++++++++-------- test/browser/graphqls2s.js | 11 +++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/graphqls2s.js b/src/graphqls2s.js index 18cf26a..9757e03 100644 --- a/src/graphqls2s.js +++ b/src/graphqls2s.js @@ -53,18 +53,25 @@ const enumRegex = { regex: /enum\s(.*?){(.*?)_cr_([^#]*?)}/mg, type: 'enum' } const interfaceRegex = { regex: /(extend interface|interface)\s(.*?){(.*?)_cr_([^#]*?)}/mg, type: 'interface' } const abstractRegex = { regex: /(extend abstract|abstract)\s(.*?){(.*?)_cr_([^#]*?)}/mg, type: 'abstract' } const scalarRegex = { regex: /(.{1}|.{0})scalar\s(.*?)([^\s]*?)(?![a-zA-Z0-9])/mg, type: 'scalar' } +const unionRegex = { regex: /(.{1}|.{0})union([^\n]*?)\n/gm, type: 'union' } const getSchemaBits = (sch='') => { const escapedSchemaWithComments = escapeGraphQlSchemaPlus(sch, carrReturnEsc, tabEsc) - const escapedSchemaWithoutComments = escapeGraphQlSchemaPlus(sch.replace(/#(.*?)\n/g, ''), carrReturnEsc, tabEsc) - return _.flatten([typeRegex, inputRegex, enumRegex, interfaceRegex, abstractRegex, scalarRegex] + // We append '\n' to help isolating the 'union' + const schemaWithoutComments = ' ' + sch.replace(/#(.*?)\n/g, '') + '\n' + const escapedSchemaWithoutComments = escapeGraphQlSchemaPlus(schemaWithoutComments, carrReturnEsc, tabEsc) + return _.flatten([typeRegex, inputRegex, enumRegex, interfaceRegex, abstractRegex, scalarRegex, unionRegex] .map(rx => - chain((rx.type == 'scalar' ? escapedSchemaWithoutComments : escapedSchemaWithComments).match(rx.regex) || []) + chain(( + rx.type == 'scalar' ? escapedSchemaWithoutComments : + rx.type == 'union' ? schemaWithoutComments : + escapedSchemaWithComments).match(rx.regex) || []) .next(regexMatches => - rx.type == 'scalar' - ? regexMatches.filter(m => m.indexOf('scalar') == 0 || m.match(/^(?![a-zA-Z0-9])/)) - : regexMatches) + rx.type == 'scalar' ? regexMatches.filter(m => m.indexOf('scalar') == 0 || m.match(/^(?![a-zA-Z0-9])/)) : + rx.type == 'union' ? regexMatches.filter(m => m.indexOf('union') == 0 || m.match(/^(?![a-zA-Z0-9])/)) : regexMatches) .next(regexMatches => { - const transform = rx.type == 'scalar' ? breakdownScalarBit : breakdownSchemabBit + const transform = + rx.type == 'scalar' ? breakdownScalarBit : + rx.type == 'union' ? breakdownUnionBit : breakdownSchemabBit return regexMatches.map(str => transform(str)) }) .val())) @@ -88,7 +95,12 @@ const breakdownSchemabBit = str => { const breakdownScalarBit = str => { const block = (str.split(' ').slice(-1) || [])[0] - return { property: `scalar ${block}`, block: block, extend: false, scalar: true } + return { property: `scalar ${block}`, block: block, extend: false } +} + +const breakdownUnionBit = str => { + const block = str.replace(/(^union\s|\sunion\s|\n)/g, '').trim() + return { property: `union ${block}`, block: block, extend: false } } const getSchemaEntity = firstLine => @@ -263,6 +275,17 @@ const getSchemaObject = (definitions, typeName, nameRegEx, metadata) => inherits: null, implements: null } + else if (typeName == 'union') + return { + type: 'UNION', + extend: false, + name: d.block, + metadata: null, + genericType: false, + blockProps: [], + inherits: null, + implements: null + } else { const typeDefMatch = d.property.match(/(.*?){/) if (!typeDefMatch || typeDefMatch[0].indexOf('#') >= 0) throw new Error(`Schema error: Syntax error in '${d.property}'. Cannot any find schema type definition.`) @@ -318,6 +341,8 @@ const getEnums = (definitions, metadata) => getSchemaObject(definitions, 'enum', const getScalars = (definitions, metadata) => getSchemaObject(definitions, 'scalar', null, metadata) +const getUnions = (definitions, metadata) => getSchemaObject(definitions, 'union', null, metadata) + let memoizedExtendedObject = {} const getObjWithExtensions = (obj, schemaObjects) => { if (obj && schemaObjects && obj.inherits) { @@ -453,7 +478,7 @@ const buildSchemaString = schemaObjs => _(schemaObjs) .join('\n') const getSchemaParts = (graphQlSchema, metadata, includeNewGenTypes) => chain(getSchemaBits(graphQlSchema)) - .next(schemaBits => _([getInterfaces, getAbstracts, getTypes, getInputs, getEnums, getScalars] + .next(schemaBits => _([getInterfaces, getAbstracts, getTypes, getInputs, getEnums, getScalars, getUnions] .reduce((objects, getObjects) => objects.concat(getObjects(schemaBits, metadata)), []))) .next(firstSchemaBreakDown => _.toArray(firstSchemaBreakDown .map(obj => getObjWithExtensions(obj, firstSchemaBreakDown)) diff --git a/test/browser/graphqls2s.js b/test/browser/graphqls2s.js index d1402a3..b9db7eb 100644 --- a/test/browser/graphqls2s.js +++ b/test/browser/graphqls2s.js @@ -454,9 +454,10 @@ scalar Like scalar Like union Product = Bicycle | Racket +union Details = PriceDetails | RacketDetails # This is some description of - # what a Post object is plus an attemp to fool the scalar type. + # what a Post object is plus an attemp to fool the union type. type Post { id: ID! # A name is a property. @@ -477,7 +478,7 @@ scalar Like var schema_output_fdwfcds95d = ` # This is some description of - # what a Post object is plus an attemp to fool the scalar type. + # what a Post object is plus an attemp to fool the union type. type Post { id: ID! # A name is a property. @@ -494,18 +495,18 @@ scalar Like rating: Strength! } - union Product = Bicycle | Racket scalar Date scalar Like - scalar Strength` + scalar Strength + union Product = Bicycle | Racket + union Details = PriceDetails | RacketDetails` /*eslint-disable */ describe('graphqls2s', () => describe('#transpileSchema: SUPPORT FOR UNION', () => it('Should support union types.', () => { var output = transpileSchema(schema_input_fdwfcds95d) - console.log(output) var answer = compressString(output) var correct = compressString(schema_output_fdwfcds95d) assert.equal(answer,correct)