diff --git a/src/file_loader.js b/src/file_loader.js index ef21ae7..0ac94f5 100644 --- a/src/file_loader.js +++ b/src/file_loader.js @@ -3,26 +3,25 @@ import path from 'path'; import isGlob from 'is-glob'; import Glob from 'glob'; -const recursiveReadDirSync = dir => - fs.readdirSync(dir) - .reduce((files, file) => ( - fs.statSync(path.join(dir, file)).isDirectory() ? - files.concat(recursiveReadDirSync(path.join(dir, file))) : - files.concat(path.join(dir, file)) - ), - []); +const recursiveReadDirSync = dir => fs + .readdirSync(dir) + .reduce( + (files, file) => (fs.statSync(path.join(dir, file)).isDirectory() + ? files.concat(recursiveReadDirSync(path.join(dir, file))) + : files.concat(path.join(dir, file))), + [], + ); -const readDirSync = dir => - fs.readdirSync(dir) - .reduce((files, file) => ( - fs.statSync(path.join(dir, file)).isDirectory() ? - files : - files.concat(path.join(dir, file)) - ), - []); +const readDirSync = dir => fs + .readdirSync(dir) + .reduce( + (files, file) => (fs.statSync(path.join(dir, file)).isDirectory() + ? files + : files.concat(path.join(dir, file))), + [], + ); -const readGlobSync = (pattern, options) => - Glob.sync(pattern, options); +const readGlobSync = (pattern, options) => Glob.sync(pattern, options); const getSchemaFiles = (dir, recursive, globOptions) => { if (isGlob(dir)) { @@ -38,45 +37,43 @@ const getSchemaFiles = (dir, recursive, globOptions) => { const DEFAULT_EXTENSIONS = ['.ts', '.js', '.gql', '.graphql', '.graphqls']; -const fileLoader = (folderPath, - { - recursive = false, - extensions = DEFAULT_EXTENSIONS, - globOptions = {}, - } = {}) => { +const fileLoader = ( + folderPath, + { recursive = false, extensions = DEFAULT_EXTENSIONS, globOptions = {} } = {}, +) => { const dir = folderPath; const schemafiles = getSchemaFiles(dir, recursive, globOptions); const files = schemafiles - .map(f => ({ f, pathObj: path.parse(f) })) - .filter(({ pathObj }) => pathObj.name.toLowerCase() !== 'index') - .filter(({ pathObj }) => extensions.includes(pathObj.ext)) - .map(({ f, pathObj }) => { - let returnVal; + .map(f => ({ f, pathObj: path.parse(f) })) + .filter(({ pathObj }) => pathObj.name.toLowerCase() !== 'index') + .filter(({ pathObj }) => extensions.includes(pathObj.ext)) + .map(({ f, pathObj }) => { + let returnVal; - switch (pathObj.ext) { - case '.ts': - case '.js': { - const file = require(f); // eslint-disable-line - returnVal = file.default || file; - break; - } + switch (pathObj.ext) { + case '.ts': + case '.js': { + const file = require(f); // eslint-disable-line + returnVal = file.default || file; + break; + } - case '.graphqls': - case '.gql': - case '.graphql': { - const file = fs.readFileSync(f, 'utf8'); - returnVal = file; - break; - } + case '.graphqls': + case '.gql': + case '.graphql': { + const file = fs.readFileSync(f, 'utf8'); + returnVal = file; + break; + } - default: - // we don't know how to handle other extensions - } + default: + // we don't know how to handle other extensions + } - return returnVal; - }) - .filter(v => !!v); // filter files that we don't know how to handle + return returnVal; + }) + .filter(v => !!v); // filter files that we don't know how to handle return files; }; diff --git a/src/merge_types.js b/src/merge_types.js index 5769138..fa1e2e6 100644 --- a/src/merge_types.js +++ b/src/merge_types.js @@ -5,26 +5,27 @@ import { getDescription } from 'graphql/utilities/buildASTSchema'; // TODO: Refactor code and switch to using print from graphql directly. import print from './utilities/astPrinter'; import { makeSchema, mergeableTypes } from './utilities/makeSchema'; -import { isObjectTypeDefinition, isObjectSchemaDefinition, isEnumTypeDefinition } from './utilities/astHelpers'; +import { + isObjectTypeDefinition, + isObjectSchemaDefinition, + isEnumTypeDefinition, +} from './utilities/astHelpers'; -const _isMergeableTypeDefinition = (def, all) => - isObjectTypeDefinition(def) && (mergeableTypes.includes(def.name.value) || all); +const _isMergeableTypeDefinition = (def, all) => isObjectTypeDefinition(def) && (mergeableTypes.includes(def.name.value) || all); const _isNonMergeableTypeDefinition = (def, all) => !_isMergeableTypeDefinition(def, all); const _makeCommentNode = value => ({ kind: 'Comment', value }); const _addCommentsToAST = (nodes = [], flatten = true) => { - const astWithComments = nodes.map( - (node) => { - const description = getDescription(node, { commentDescriptions: true }); - if (description) { - return [_makeCommentNode(description), node]; - } + const astWithComments = nodes.map((node) => { + const description = getDescription(node, { commentDescriptions: true }); + if (description) { + return [_makeCommentNode(description), node]; + } - return [node]; - }, - ); + return [node]; + }); if (flatten) { return astWithComments.reduce((a, b) => a.concat(b), []); @@ -33,20 +34,19 @@ const _addCommentsToAST = (nodes = [], flatten = true) => { return astWithComments; }; -const _makeRestDefinitions = (defs, all = false) => - defs - .filter(def => _isNonMergeableTypeDefinition(def, all) && !isObjectSchemaDefinition(def)) - .map((def) => { - if (isObjectTypeDefinition(def) || isEnumTypeDefinition(def)) { - return { - ...def, - fields: def.fields ? _addCommentsToAST(def.fields) : undefined, - values: def.values ? _addCommentsToAST(def.values) : undefined, - }; - } +const _makeRestDefinitions = (defs, all = false) => defs + .filter(def => _isNonMergeableTypeDefinition(def, all) && !isObjectSchemaDefinition(def)) + .map((def) => { + if (isObjectTypeDefinition(def) || isEnumTypeDefinition(def)) { + return { + ...def, + fields: def.fields ? _addCommentsToAST(def.fields) : undefined, + values: def.values ? _addCommentsToAST(def.values) : undefined, + }; + } - return def; - }); + return def; + }); // Gracefully handle nested pathing of GraphQL types // skips any attributes that can't be found in path (i.e. type.type.type.value => type.type.value) @@ -54,58 +54,61 @@ const _makeRestDefinitions = (defs, all = false) => // (i.e. 'ListType.NamedType.SomeValue' !== 'NonNullType.ListType.NamedType.SomeValue') const _getGraphQLPath = (path, theObj, tracker) => { let result; - path - .split('.') - .reduce((o, x) => { - if (o && o[x]) { - const v = o[x].hasOwnProperty.call(tracker) ? - o[x][tracker] : o[x]; - if (!result) result = v; - else result += `.${v}`; - return o[x]; - } - return o; - }, theObj); + path.split('.').reduce((o, x) => { + if (o && o[x]) { + const v = o[x].hasOwnProperty.call(tracker) ? o[x][tracker] : o[x]; + if (!result) result = v; + else result += `.${v}`; + return o[x]; + } + return o; + }, theObj); return result; }; -const _makeMergedFieldDefinitions = (merged, candidate) => _addCommentsToAST(candidate.fields) - .reduce((fields, field) => { - const original = merged.fields.find(base => base.name && typeof base.name.value !== 'undefined' && - field.name && typeof field.name.value !== 'undefined' && - base.name.value === field.name.value); - if (!original) { - fields.push(field); - } else if (field.type.kind === 'NamedType') { - const fieldName = (field.type.name && field.type.name.value) || null; - const originalName = (original.type.name && original.type.name.value) || null; - if (!fieldName || !originalName || (fieldName !== originalName)) { - throw new Error(`Conflicting types for ${merged.name.value}.${fieldName}: ${fieldName || 'undefined'} != ${originalName}`); - } - } else if (field.type.kind === 'NonNullType' || field.type.kind === 'ListType') { - const path = _getGraphQLPath('type.type.type.value', field, 'kind'); - const originalPath = _getGraphQLPath('type.type.type.value', original, 'kind'); - - if (path !== originalPath) { - throw new Error( - `Conflicting types for ${merged.name.value}.${field.name.value}: ` + - `${path} != ${originalPath}`, - ); - } +const _makeMergedFieldDefinitions = (merged, candidate) => _addCommentsToAST(candidate.fields).reduce((fields, field) => { + const original = merged.fields.find( + base => base.name + && typeof base.name.value !== 'undefined' + && field.name + && typeof field.name.value !== 'undefined' + && base.name.value === field.name.value, + ); + if (!original) { + fields.push(field); + } else if (field.type.kind === 'NamedType') { + const fieldName = (field.type.name && field.type.name.value) || null; + const originalName = (original.type.name && original.type.name.value) || null; + if (!fieldName || !originalName || fieldName !== originalName) { + throw new Error( + `Conflicting types for ${merged.name.value}.${fieldName}: ${fieldName + || 'undefined'} != ${originalName}`, + ); } - - // retain directives of both fields. - if (original) { - original.directives = original.directives.concat(field.directives); - original.directives = original.directives.reduce((current, next) => { - if (current.findIndex(n => n.name.value === next.name.value) === -1) { - current.push(next); - } - return current; - }, []); + } else if (field.type.kind === 'NonNullType' || field.type.kind === 'ListType') { + const path = _getGraphQLPath('type.type.type.value', field, 'kind'); + const originalPath = _getGraphQLPath('type.type.type.value', original, 'kind'); + + if (path !== originalPath) { + throw new Error( + `Conflicting types for ${merged.name.value}.${field.name.value}: ` + + `${path} != ${originalPath}`, + ); } - return fields; - }, merged.fields); + } + + // retain directives of both fields. + if (original) { + original.directives = original.directives.concat(field.directives); + original.directives = original.directives.reduce((current, next) => { + if (current.findIndex(n => n.name.value === next.name.value) === -1) { + current.push(next); + } + return current; + }, []); + } + return fields; +}, merged.fields); const _makeMergedDefinitions = (defs, all = false) => { // TODO: This function can be cleaner! @@ -133,16 +136,18 @@ const _makeMergedDefinitions = (defs, all = false) => { fields: _makeMergedFieldDefinitions(mergableDefs[name], def), }, }; - }, { + }, + { Query: null, Mutation: null, Subscription: null, }, ); - return Object - .values(groupedMergableDefinitions) - .reduce((array, def) => (def ? [...array, def] : array), []); + return Object.values(groupedMergableDefinitions).reduce( + (array, def) => (def ? [...array, def] : array), + [], + ); }; const _makeDocumentWithDefinitions = definitions => ({ @@ -164,8 +169,9 @@ const mergeTypes = (types, options = { all: false }) => { .reduce((defs, newDef) => [...defs, ...newDef], []); const mergedDefs = _makeMergedDefinitions(allDefs, options.all); - const rest = _addCommentsToAST(_makeRestDefinitions(allDefs, options.all), false) - .map(printDefinitions); + const rest = _addCommentsToAST(_makeRestDefinitions(allDefs, options.all), false).map( + printDefinitions, + ); const schemaDefs = allDefs.filter(isObjectSchemaDefinition); const schema = printDefinitions([makeSchema(mergedDefs, schemaDefs), ...mergedDefs]); diff --git a/src/utilities/astHelpers.js b/src/utilities/astHelpers.js index 32dff3a..d61c170 100644 --- a/src/utilities/astHelpers.js +++ b/src/utilities/astHelpers.js @@ -1,19 +1,14 @@ import { Kind } from 'graphql'; -const hasDefinitionWithName = (nodes, name) => - nodes.findIndex(node => node.name.value === name) !== -1; +const hasDefinitionWithName = (nodes, name) => nodes.findIndex(node => node.name.value === name) !== -1; -const isObjectTypeDefinition = def => ( - def.kind === Kind.OBJECT_TYPE_DEFINITION || - def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION || - def.kind === Kind.SCALAR_TYPE_DEFINITION || - def.kind === Kind.ENUM_TYPE_DEFINITION || - def.kind === Kind.INTERFACE_TYPE_DEFINITION -); +const isObjectTypeDefinition = def => def.kind === Kind.OBJECT_TYPE_DEFINITION + || def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION + || def.kind === Kind.SCALAR_TYPE_DEFINITION + || def.kind === Kind.ENUM_TYPE_DEFINITION + || def.kind === Kind.INTERFACE_TYPE_DEFINITION; -const isEnumTypeDefinition = def => ( - def.kind === Kind.ENUM_TYPE_DEFINITION -); +const isEnumTypeDefinition = def => def.kind === Kind.ENUM_TYPE_DEFINITION; const isObjectSchemaDefinition = def => def.kind === Kind.SCHEMA_DEFINITION; diff --git a/src/utilities/astPrinter.js b/src/utilities/astPrinter.js index d4467af..9c9b4c8 100644 --- a/src/utilities/astPrinter.js +++ b/src/utilities/astPrinter.js @@ -6,9 +6,9 @@ */ /** -* NOTE: ==> This file has been modified just to add comments to the printed AST -* This is a temp measure, we will move to using the original non modified printer.js ASAP. -*/ + * NOTE: ==> This file has been modified just to add comments to the printed AST + * This is a temp measure, we will move to using the original non modified printer.js ASAP. + */ import { visit } from 'graphql/language/visitor'; @@ -56,25 +56,22 @@ function printBlockString(value, isDescription) { : `"""\n${isDescription ? escaped : indent(escaped)}\n"""`; } - const printDocASTReducer = { Name: node => node.value, Variable: node => `$${node.name}`, // Document - Document: node => - `${node.definitions - .map(defNode => `${defNode}\n${defNode[0] === '#' ? '' : '\n'}`) - .join('') - .trim()}\n`, + Document: node => `${node.definitions + .map(defNode => `${defNode}\n${defNode[0] === '#' ? '' : '\n'}`) + .join('') + .trim()}\n`, OperationDefinition(node) { const op = node.operation; - const name = node.name; + const { name, selectionSet } = node; const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')'); const directives = join(node.directives, ' '); - const selectionSet = node.selectionSet; // Anonymous queries with no directives or variable definitions can use // the query short form. return !name && !directives && !varDefs && op === 'query' @@ -82,20 +79,20 @@ const printDocASTReducer = { : join([op, join([name, varDefs]), directives, selectionSet], ' '); }, - VariableDefinition: ({ variable, type, defaultValue }) => - `${variable}: ${type}${wrap(' = ', defaultValue)}`, + VariableDefinition: ({ variable, type, defaultValue }) => `${variable}: ${type}${wrap(' = ', defaultValue)}`, SelectionSet: ({ selections }) => block(selections), - Field: ({ alias, name, arguments: args, directives, selectionSet }) => - join( - [ - wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), - join(directives, ' '), - selectionSet, - ], - ' ', - ), + Field: ({ + alias, name, arguments: args, directives, selectionSet, + }) => join( + [ + wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), + join(directives, ' '), + selectionSet, + ], + ' ', + ), Argument: ({ name, value }) => `${name}: ${value}`, @@ -103,21 +100,18 @@ const printDocASTReducer = { FragmentSpread: ({ name, directives }) => `...${name}${wrap(' ', join(directives, ' '))}`, - InlineFragment: ({ typeCondition, directives, selectionSet }) => - join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '), + InlineFragment: ({ typeCondition, directives, selectionSet }) => join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '), - FragmentDefinition: ({ name, typeCondition, variableDefinitions, directives, selectionSet }) => - // Note: fragment variable definitions are experimental and may be changed - // or removed in the future. - `${`fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` + - `on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}`}${selectionSet}`, + FragmentDefinition: ({ + name, typeCondition, variableDefinitions, directives, selectionSet, + }) => `${`fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` + + `on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}`}${selectionSet}`, // Value IntValue: ({ value }) => value, FloatValue: ({ value }) => value, - StringValue: ({ value, block: isBlockString }, key) => - (isBlockString ? printBlockString(value, key === 'description') : JSON.stringify(value)), + StringValue: ({ value, block: isBlockString }, key) => (isBlockString ? printBlockString(value, key === 'description') : JSON.stringify(value)), BooleanValue: ({ value }) => (value ? 'true' : 'false'), NullValue: () => 'null', EnumValue: ({ value }) => value, @@ -137,103 +131,86 @@ const printDocASTReducer = { // Type System Definitions - SchemaDefinition: ({ directives, operationTypes }) => - join(['schema', join(directives, ' '), block(operationTypes)], ' '), + SchemaDefinition: ({ directives, operationTypes }) => join(['schema', join(directives, ' '), block(operationTypes)], ' '), OperationTypeDefinition: ({ operation, type }) => `${operation}: ${type}`, - ScalarTypeDefinition: addDescription(({ name, directives }) => - join(['scalar', name, join(directives, ' ')], ' '), - ), - - ObjectTypeDefinition: addDescription(({ name, interfaces, directives, fields }) => - join( - [ - 'type', - name, - wrap('implements ', join(interfaces, ' & ')), - join(directives, ' '), - block(fields), - ], - ' ', - ), - ), + ScalarTypeDefinition: addDescription(({ name, directives }) => join(['scalar', name, join(directives, ' ')], ' ')), + + ObjectTypeDefinition: addDescription(({ + name, interfaces, directives, fields, + }) => join( + [ + 'type', + name, + wrap('implements ', join(interfaces, ' & ')), + join(directives, ' '), + block(fields), + ], + ' ', + )), FieldDefinition: addDescription( - ({ name, arguments: args, type, directives }) => - `${name + wrap('(', join(args, ', '), ')')}: ${type}${wrap(' ', join(directives, ' '))}`, + ({ + name, arguments: args, type, directives, + }) => `${name + wrap('(', join(args, ', '), ')')}: ${type}${wrap(' ', join(directives, ' '))}`, ), - InputValueDefinition: addDescription(({ name, type, defaultValue, directives }) => - join([`${name}: ${type}`, wrap('= ', defaultValue), join(directives, ' ')], ' '), + InputValueDefinition: addDescription(({ + name, type, defaultValue, directives, + }) => join([`${name}: ${type}`, wrap('= ', defaultValue), join(directives, ' ')], ' ')), + + InterfaceTypeDefinition: addDescription(({ name, directives, fields }) => join(['interface', name, join(directives, ' '), block(fields)], ' ')), + + UnionTypeDefinition: addDescription(({ name, directives, types }) => join( + [ + 'union', + name, + join(directives, ' '), + types && types.length !== 0 ? `= ${join(types, ' | ')}` : '', + ], + ' ', + )), + + EnumTypeDefinition: addDescription(({ name, directives, values }) => join(['enum', name, join(directives, ' '), block(values)], ' ')), + + EnumValueDefinition: addDescription(({ name, directives }) => join([name, join(directives, ' ')], ' ')), + + InputObjectTypeDefinition: addDescription(({ name, directives, fields }) => join(['input', name, join(directives, ' '), block(fields)], ' ')), + + ScalarTypeExtension: ({ name, directives }) => join(['extend scalar', name, join(directives, ' ')], ' '), + + ObjectTypeExtension: ({ + name, interfaces, directives, fields, + }) => join( + [ + 'extend type', + name, + wrap('implements ', join(interfaces, ' & ')), + join(directives, ' '), + block(fields), + ], + ' ', ), - InterfaceTypeDefinition: addDescription(({ name, directives, fields }) => - join(['interface', name, join(directives, ' '), block(fields)], ' '), - ), - - UnionTypeDefinition: addDescription(({ name, directives, types }) => - join( - [ - 'union', - name, - join(directives, ' '), - types && types.length !== 0 ? `= ${join(types, ' | ')}` : '', - ], - ' ', - ), - ), + InterfaceTypeExtension: ({ name, directives, fields }) => join(['extend interface', name, join(directives, ' '), block(fields)], ' '), - EnumTypeDefinition: addDescription(({ name, directives, values }) => - join(['enum', name, join(directives, ' '), block(values)], ' '), + UnionTypeExtension: ({ name, directives, types }) => join( + [ + 'extend union', + name, + join(directives, ' '), + types && types.length !== 0 ? `= ${join(types, ' | ')}` : '', + ], + ' ', ), - EnumValueDefinition: addDescription(({ name, directives }) => - join([name, join(directives, ' ')], ' '), - ), - - InputObjectTypeDefinition: addDescription(({ name, directives, fields }) => - join(['input', name, join(directives, ' '), block(fields)], ' '), - ), + EnumTypeExtension: ({ name, directives, values }) => join(['extend enum', name, join(directives, ' '), block(values)], ' '), - ScalarTypeExtension: ({ name, directives }) => - join(['extend scalar', name, join(directives, ' ')], ' '), - - ObjectTypeExtension: ({ name, interfaces, directives, fields }) => - join( - [ - 'extend type', - name, - wrap('implements ', join(interfaces, ' & ')), - join(directives, ' '), - block(fields), - ], - ' ', - ), - - InterfaceTypeExtension: ({ name, directives, fields }) => - join(['extend interface', name, join(directives, ' '), block(fields)], ' '), - - UnionTypeExtension: ({ name, directives, types }) => - join( - [ - 'extend union', - name, - join(directives, ' '), - types && types.length !== 0 ? `= ${join(types, ' | ')}` : '', - ], - ' ', - ), - - EnumTypeExtension: ({ name, directives, values }) => - join(['extend enum', name, join(directives, ' '), block(values)], ' '), - - InputObjectTypeExtension: ({ name, directives, fields }) => - join(['extend input', name, join(directives, ' '), block(fields)], ' '), + InputObjectTypeExtension: ({ name, directives, fields }) => join(['extend input', name, join(directives, ' '), block(fields)], ' '), DirectiveDefinition: addDescription( - ({ name, arguments: args, locations }) => - `directive @${name}${wrap('(', join(args, ', '), ')')} on ${join(locations, ' | ')}`, + ({ name, arguments: args, locations }) => `directive @${name}${wrap('(', join(args, ', '), ')')} on ${join(locations, ' | ')}`, ), Comment: ({ value }) => `# ${value.replace(/\n/g, '\n # ')}`, diff --git a/src/utilities/makeSchema.js b/src/utilities/makeSchema.js index 7471b3b..d31fcd7 100644 --- a/src/utilities/makeSchema.js +++ b/src/utilities/makeSchema.js @@ -9,19 +9,17 @@ const typesMap = { const _mergeableOperationTypes = Object.keys(typesMap); -const _makeOperationType = (operation, value) => ( - { - kind: Kind.OPERATION_TYPE_DEFINITION, - operation, - type: { - kind: Kind.NAMED_TYPE, - name: { - kind: Kind.NAME, - value, - }, +const _makeOperationType = (operation, value) => ({ + kind: Kind.OPERATION_TYPE_DEFINITION, + operation, + type: { + kind: Kind.NAMED_TYPE, + name: { + kind: Kind.NAME, + value, }, - } -); + }, +}); const mergeableTypes = Object.values(typesMap); @@ -32,26 +30,20 @@ const makeSchema = (definitions, schemaDefs) => { subscription: null, }; - mergeableTypes - .slice(1) - .forEach( - (type, key) => { - if (hasDefinitionWithName(definitions, type)) { - const operation = _mergeableOperationTypes[key + 1]; + mergeableTypes.slice(1).forEach((type, key) => { + if (hasDefinitionWithName(definitions, type)) { + const operation = _mergeableOperationTypes[key + 1]; - operationMap[operation] = _makeOperationType(operation, type); - } - }, - ); + operationMap[operation] = _makeOperationType(operation, type); + } + }); const operationTypes = Object.values(operationMap) .map((operation, i) => { if (!operation) { const type = Object.keys(operationMap)[i]; - if (schemaDefs.some(def => - def.operationTypes.some(op => op.operation === type), - )) { + if (schemaDefs.some(def => def.operationTypes.some(op => op.operation === type))) { return _makeOperationType(type, typesMap[type]); } } diff --git a/test/file_loader.test.js b/test/file_loader.test.js index eafa0f0..1633217 100644 --- a/test/file_loader.test.js +++ b/test/file_loader.test.js @@ -17,7 +17,12 @@ const raw2Type = fs.readFileSync(`${__dirname}/graphql/types/client/raw2_type.gq describe('fileLoader', () => { it('loads all files from specified folder', () => { const types = [ - clientType, personEntityType, personSearchType, productType, rawType, vendorType, + clientType, + personEntityType, + personSearchType, + productType, + rawType, + vendorType, ]; const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types')); @@ -34,7 +39,14 @@ describe('fileLoader', () => { it('loads all files recursively from specified folder', () => { const types = [ - contactType, raw2Type, clientType, personEntityType, personSearchType, productType, rawType, vendorType, + contactType, + raw2Type, + clientType, + personEntityType, + personSearchType, + productType, + rawType, + vendorType, ]; const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types'), { recursive: true }); @@ -43,9 +55,7 @@ describe('fileLoader', () => { }); it('loads all files from specified folder with ext .js', () => { - const types = [ - clientType, personEntityType, personSearchType, productType, vendorType, - ]; + const types = [clientType, personEntityType, personSearchType, productType, vendorType]; const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types'), { extensions: ['.js'] }); @@ -53,30 +63,27 @@ describe('fileLoader', () => { }); it('loads all files from specified folder with ext .graphqls', () => { - const types = [ - rawType, - ]; + const types = [rawType]; - const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types'), { extensions: ['.graphqls'] }); + const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types'), { + extensions: ['.graphqls'], + }); expect(loadedTypes).toEqual(types); }); it('unexpected extension should be ignored', () => { - const types = [ - rawType, - ]; + const types = [rawType]; - const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types'), { extensions: ['.graphqls', '.txt'] }); + const loadedTypes = fileLoader(path.join(__dirname, 'graphql/types'), { + extensions: ['.graphqls', '.txt'], + }); expect(loadedTypes).toEqual(types); }); it('loads all files from glob pattern of ext .graphqls or .gql', () => { - const types = [ - raw2Type, - rawType, - ]; + const types = [raw2Type, rawType]; const loadedTypes = fileLoader(path.join(__dirname, 'graphql/**/*.+(graphqls|gql)')); diff --git a/test/merge_types.test.js b/test/merge_types.test.js index 0e37708..0194a25 100644 --- a/test/merge_types.test.js +++ b/test/merge_types.test.js @@ -9,9 +9,9 @@ import personSearchType from './graphql/types/person_search_type'; import customType from './graphql/other/custom_type'; import disjointCustomTypes from './graphql/other/custom_type/disjoint'; import matchingCustomTypes from './graphql/other/custom_type/matching'; -import matchingNonNullListQueryTypes from "./graphql/other/query_type/matching_non_null_list_type"; -import conflictingNonNullListQueryTypes from "./graphql/other/query_type/conflicting_non_null_list_type"; -import inverseConflictingNonNullListQueryTypes from "./graphql/other/query_type/inverse_conflicting_non_null_list_type"; +import matchingNonNullListQueryTypes from './graphql/other/query_type/matching_non_null_list_type'; +import conflictingNonNullListQueryTypes from './graphql/other/query_type/conflicting_non_null_list_type'; +import inverseConflictingNonNullListQueryTypes from './graphql/other/query_type/inverse_conflicting_non_null_list_type'; import conflictingCustomTypes from './graphql/other/custom_type/conflicting'; import simpleQueryType from './graphql/other/simple_query_type'; @@ -258,7 +258,7 @@ describe('mergeTypes', () => { expect(separateTypes).toContain(expectedCustomType); }); - it("merges query types with matching NonNullType-ListType definitions", () => { + it('merges query types with matching NonNullType-ListType definitions', () => { const types = [matchingNonNullListQueryTypes]; const mergedTypes = mergeTypes(types); const expectedSchemaType = normalizeWhitespace(` @@ -270,28 +270,28 @@ describe('mergeTypes', () => { expect(separateTypes).toContain(expectedSchemaType); }); - it("throws on conflicting NonNullType-ListType definitions", () => { + it('throws on conflicting NonNullType-ListType definitions', () => { const types = [conflictingNonNullListQueryTypes]; expect(() => { mergeTypes(types); }).toThrow(expect.any(Error)); }); - it("throws on conflicting NonNullType-ListType definitions with merge attempt", () => { + it('throws on conflicting NonNullType-ListType definitions with merge attempt', () => { const types = [conflictingNonNullListQueryTypes]; expect(() => { mergeTypes(types, { all: true }); }).toThrow(expect.any(Error)); }); - it("throws on inverse conflicting NonNullType-ListType definitions", () => { + it('throws on inverse conflicting NonNullType-ListType definitions', () => { const types = [inverseConflictingNonNullListQueryTypes]; expect(() => { mergeTypes(types); }).toThrow(expect.any(Error)); }); - it("throws on inverse conflicting NonNullType-ListType definitions with merge attempt", () => { + it('throws on inverse conflicting NonNullType-ListType definitions with merge attempt', () => { const types = [inverseConflictingNonNullListQueryTypes]; expect(() => { mergeTypes(types, { all: true }); @@ -355,7 +355,6 @@ describe('mergeTypes', () => { const schema = normalizeWhitespace(mergedTypes); expect(schema).toContain(expectedType); - const count = schema.split('enum ClientStatus').length; expect(count).toBe(2);