Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 80,
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always"
}
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ node_js:
- 8

clean-cache:
- npm cache clean
- npm cache clean
install:
- npm install
script:
- npm run standard
- npm run prettier:check
- npm test
after_success:
- npm run coveralls
after_success:
- npm run coveralls
77 changes: 53 additions & 24 deletions lib/schemaParser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict'

const { parse, Kind, printSchema, buildClientSchema, GraphQLSchema, buildASTSchema } = require('graphql')
const {
parse,
Kind,
printSchema,
buildClientSchema,
GraphQLSchema,
buildASTSchema,
} = require('graphql')
const { mergeTypes } = require('merge-graphql-schemas')

function getOperationTypes (schema, nodeMap) {
function getOperationTypes(schema, nodeMap) {
const opTypes = {}
const s = parseSchema(nodeMap)
schema.operationTypes.forEach((operationType) => {
Expand All @@ -14,20 +21,26 @@ function getOperationTypes (schema, nodeMap) {
throw new Error('Must provide only one ' + operation + ' type in schema.')
}
if (!nodeMap[typeName]) {
throw new Error('Specified ' + operation + ' type "' + typeName + '" not found in document.')
throw new Error(
'Specified ' +
operation +
' type "' +
typeName +
'" not found in document.'
)
}

opTypes[operation] = {
type: operationType.kind,
operation,
field: typeName
field: typeName,
}
})

return Object.assign({ schemaDefinition: opTypes }, s)
}

function schemaBuilder (doc) {
function schemaBuilder(doc) {
let schemaDef = void 0

const nodeMap = Object.create(null)
Expand Down Expand Up @@ -56,7 +69,10 @@ function schemaBuilder (doc) {
break
case Kind.OBJECT_TYPE_EXTENSION:
if (nodeMap[typeName]) {
nodeMap[typeName].fields = [].concat(nodeMap[typeName].fields, d.fields)
nodeMap[typeName].fields = [].concat(
nodeMap[typeName].fields,
d.fields
)
break
}
nodeMap[typeName] = d
Expand All @@ -70,7 +86,9 @@ function schemaBuilder (doc) {
}
}

const operationTypes = schemaDef ? getOperationTypes(schemaDef, nodeMap) : parseSchema(nodeMap)
const operationTypes = schemaDef
? getOperationTypes(schemaDef, nodeMap)
: parseSchema(nodeMap)

return operationTypes
}
Expand All @@ -81,8 +99,12 @@ function schemaBuilder (doc) {
* @param typeInfo - The object with the recursive values
* @returns {{type: String, noNull: Boolean, isArray: Boolean}}
*/
function findType (node, typeInfo, nestedCall) {
typeInfo = typeInfo || { noNull: false, isArray: false, noNullArrayValues: false }
function findType(node, typeInfo, nestedCall) {
typeInfo = typeInfo || {
noNull: false,
isArray: false,
noNullArrayValues: false,
}
if (!node) {
return typeInfo
}
Expand Down Expand Up @@ -114,12 +136,12 @@ function findType (node, typeInfo, nestedCall) {
* @param node - The graph schema
* @returns {[{name: String, noNull: Boolean, isArray: Boolean, type: String}]}
*/
function findArguments (node) {
function findArguments(node) {
if (!node) {
return []
}

return node.map(arg => {
return node.map((arg) => {
const name = arg.name.value
const fieldType = findType(arg.type)
const isDeprecated = validateIfDeprecated(arg.directives)
Expand All @@ -133,15 +155,15 @@ function findArguments (node) {
* @param directives - Receive the directives array
* @returns {boolean}
*/
function validateIfDeprecated (directives) {
function validateIfDeprecated(directives) {
if (!directives.length) {
return false
}

return directives.some(directive => directive.name.value === 'deprecated')
return directives.some((directive) => directive.name.value === 'deprecated')
}

function parseSchema (types) {
function parseSchema(types) {
const parsedTypes = {}
// Loop all the types (Scalar, Type, Input, Query, Mutation)
for (const key of Object.keys(types)) {
Expand All @@ -152,12 +174,12 @@ function parseSchema (types) {
fields: [],
values: [],
types: [],
implementedTypes: []
implementedTypes: [],
}

if (type.fields) {
const fields = []
type.fields.forEach(field => {
type.fields.forEach((field) => {
// Set the name of the field used on the Schema
const name = field.name.value
// Get the type of the field, also check if is require and array
Expand All @@ -166,20 +188,23 @@ function parseSchema (types) {
const typeArguments = findArguments(field.arguments)

const isDeprecated = validateIfDeprecated(field.directives)
const newField = Object.assign({ name, arguments: typeArguments, isDeprecated }, fieldType)
const newField = Object.assign(
{ name, arguments: typeArguments, isDeprecated },
fieldType
)
fields.push(newField)
})
parsedType.fields = fields
} else if (type.values) {
const values = type.values.map(val => val.name.value)
const values = type.values.map((val) => val.name.value)
parsedType.values = values
} else if (type.types) {
const types = type.types.map(val => val.name.value)
const types = type.types.map((val) => val.name.value)
parsedType.types = types
}

if (type.interfaces) {
const types = type.interfaces.map(val => val.name.value)
const types = type.interfaces.map((val) => val.name.value)
parsedType.implementedTypes = types
}

Expand All @@ -191,8 +216,10 @@ function parseSchema (types) {
const implementedTypes = parsedTypes[key].implementedTypes
if (implementedTypes.length) {
// set to each interface the types that implement itself.
implementedTypes.forEach(implementedType => {
if (parsedTypes[implementedType].type === Kind.INTERFACE_TYPE_DEFINITION) {
implementedTypes.forEach((implementedType) => {
if (
parsedTypes[implementedType].type === Kind.INTERFACE_TYPE_DEFINITION
) {
parsedTypes[implementedType].implementedTypes.push(key)
}
})
Expand All @@ -202,7 +229,7 @@ function parseSchema (types) {
return parsedTypes
}

function schemaParser (source) {
function schemaParser(source) {
if (Array.isArray(source)) {
source = mergeTypes(source, { all: true })
}
Expand All @@ -218,7 +245,9 @@ function schemaParser (source) {
}
}

return schemaBuilder(parse(source, { allowLegacySDLImplementsInterfaces: true }))
return schemaBuilder(
parse(source, { allowLegacySDLImplementsInterfaces: true })
)
}

module.exports = schemaParser
Loading