Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw GraphQL Schema error when federated schemas are not extend each other properly #830

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fp = require('fastify-plugin')
let LRU = require("tiny-lru");
let LRU = require('tiny-lru')
const routes = require('./lib/routes')
const { compileQuery, isCompiledQuery } = require('graphql-jit')
const { Factory } = require('single-user-cache')
Expand Down
100 changes: 98 additions & 2 deletions lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const {
getNamedType,
isObjectType,
isScalarType,
Kind
Kind,
GraphQLError
} = require('graphql')
const { Factory } = require('single-user-cache')
const buildFederatedSchema = require('./federation')
Expand All @@ -16,7 +17,7 @@ const {
createEntityReferenceResolverOperation,
kEntityResolvers
} = require('./gateway/make-resolver')
const { MER_ERR_GQL_GATEWAY_REFRESH, MER_ERR_GQL_GATEWAY_INIT, MER_ERR_SERVICE_RETRY_FAILED } = require('./errors')
const { MER_ERR_GQL_GATEWAY_REFRESH, MER_ERR_GQL_GATEWAY_INIT, MER_ERR_SERVICE_RETRY_FAILED, MER_ERR_GQL_INVALID_SCHEMA } = require('./errors')
const findValueTypes = require('./gateway/find-value-types')
const getQueryResult = require('./gateway/get-query-result')

Expand Down Expand Up @@ -285,6 +286,93 @@ function defineResolvers (schema, typeToServiceMap, serviceMap, typeFieldsToServ
}
}

function checkForConflictingSchemas (serviceMap, valueTypes) {
const existingTypes = new Map()
const errors = []

Object.entries(serviceMap).forEach(([_, value]) => {
const types = value.schema.getTypeMap()
for (const type of Object.values(types)) {
if (valueTypes.includes(type.name)) {
continue
}

const check = isObjectType(type) && !isDefaultType(type.name) && type.name !== '_Service'
if (!check) {
continue
}

const isExtension = type.extensionASTNodes.length && type.extensionASTNodes[0].name.value === type.name

const doesNotExistIsExtension = !existingTypes.has(type.name) && isExtension
const doesNotExistIsNotExtension = !existingTypes.has(type.name) && !isExtension
const existsAndIsNotExtension = existingTypes.has(type.name) && !isExtension
const existsAndHasExtension = existingTypes.has(type.name) && isExtension

if (doesNotExistIsExtension || existsAndHasExtension) {
continue
} else if (existsAndIsNotExtension) {
const firstType = existingTypes.get(type.name, type)

const firstTypeFields = firstType.getFields()
const firstTypeFieldNames = Object.keys(firstTypeFields)

const fields = type.getFields()
const fieldNames = Object.keys(fields)

// Here we want to ensure that the types
// are the same
let identical = true

if (fieldNames.length !== firstTypeFieldNames.length) {
identical = false
} else {
for (let i = 0; i < fieldNames.length; i++) {
const fieldName = fieldNames[i]
const currentField = fields[fieldName]
const firstTypeField = firstTypeFields[fieldName]

const currentFieldType = currentField.astNode.type.type.name.value
const firstTypeFieldType = firstTypeField.astNode.type.type.name.value
// Ensure they are the same type
if (currentFieldType !== firstTypeFieldType) {
identical = false
break
}

// Ensure they are both same nullability
if (currentField.astNode.type.kind !== firstTypeField.astNode.type.kind) {
identical = false
break
}
}
}

if (!identical) {
const error = new Error(`Type ${type.name} may only be defined once in the schema`)
const gqlError = new GraphQLError(
error.message,
error.nodes,
error.source,
error.positions,
error.path,
error.originalError,
error.extensions
)
gqlError.locations = error.locations
gqlError.name = error.name

errors.push(error)
}
} else if (doesNotExistIsNotExtension) {
existingTypes.set(type.name, type)
}
}
})

return errors
}

function defaultErrorHandler (error, service) {
if (service.mandatory) {
throw error
Expand Down Expand Up @@ -409,6 +497,14 @@ async function buildGateway (gatewayOpts, app) {
typeToServiceMap[typeName] = null
}

const conflictErrors = checkForConflictingSchemas(serviceMap, valueTypes)

if (conflictErrors.length > 0) {
const err = new MER_ERR_GQL_INVALID_SCHEMA()
err.errors = conflictErrors
throw err
}

defineResolvers(schema, typeToServiceMap, serviceMap, typeFieldsToService)

return {
Expand Down
4 changes: 2 additions & 2 deletions lib/persistedQueryDefaults.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const crypto = require('crypto')
let LRU = require("tiny-lru")
let LRU = require('tiny-lru')

// Required for module bundlers
LRU = typeof LRU === "function" ? LRU : LRU.default
LRU = typeof LRU === 'function' ? LRU : LRU.default

const persistedQueryDefaults = {
prepared: (persistedQueries) => ({
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"lint:standard": "standard | snazzy",
"lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin test/types/*.ts",
"typescript": "tsd",
"test": "npm run lint && npm run unit && npm run typescript"
"test": "npm run lint && npm run unit && npm run typescript",
"test:one": "tap test/gateway/value-types.js"
},
"repository": {
"type": "git",
Expand Down
Loading