Skip to content

Commit

Permalink
fix: Add support for 'union' keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Nov 27, 2017
1 parent 351e2e5 commit e89358e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
43 changes: 34 additions & 9 deletions src/graphqls2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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 =>
Expand Down Expand Up @@ -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.`)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 6 additions & 5 deletions test/browser/graphqls2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit e89358e

Please sign in to comment.