Skip to content

Upgrade library versions and unit tests #378

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

Merged
merged 4 commits into from
Sep 25, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

package com.expediagroup.graphql.federation

import com.expediagroup.graphql.federation.directives.FieldSet
import com.expediagroup.graphql.federation.exception.InvalidFederatedSchema
import com.expediagroup.graphql.federation.extensions.isFederatedType
import com.expediagroup.graphql.federation.validation.validateDirective
import com.expediagroup.graphql.federation.validation.validateProvidesDirective
import com.expediagroup.graphql.federation.validation.validateRequiresDirective
import graphql.schema.GraphQLDirective
import graphql.schema.GraphQLDirectiveContainer
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLList
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLType
import graphql.schema.GraphQLTypeUtil
import graphql.schema.GraphQLUnionType

/**
* Validates generated federated objects.
Expand Down Expand Up @@ -87,122 +87,4 @@ class FederatedSchemaValidator {
throw InvalidFederatedSchema(errors)
}
}

// [OK] @requires references valid fields marked @external
// [ERROR] @requires specified on base type
// [ERROR] @requires specifies non-existent fields
private fun validateRequiresDirective(validatedType: String, validatedField: GraphQLFieldDefinition, fieldMap: Map<String, GraphQLFieldDefinition>, extendedType: Boolean): List<String> {
val errors = mutableListOf<String>()
if (extendedType) {
errors.addAll(validateDirective("$validatedType.${validatedField.name}", "requires", validatedField.directivesByName, fieldMap, extendedType))
} else {
errors.add("base $validatedType type has fields marked with @requires directive, validatedField=${validatedField.name}")
}
return errors
}

// [OK] @provides on base type references valid @external fields on @extend object
// [ERROR] @provides on base type references local object fields
// [ERROR] @provides on base type references local fields on @extends object
// [ERROR] @provides references interface type
// [OK] @provides references list of valid @extend objects
// [ERROR] @provides references @external list field
// [ERROR] @provides references @external interface field
private fun validateProvidesDirective(federatedType: String, field: GraphQLFieldDefinition): List<String> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved these functions to their own internal files so we can more easily scope the unit tests

val errors = mutableListOf<String>()
val returnType = GraphQLTypeUtil.unwrapAll(field.type)
if (returnType is GraphQLObjectType) {
if (!returnType.isExtendedType()) {
errors.add("@provides directive is specified on a $federatedType.${field.name} field references local object")
} else {
val returnTypeFields = returnType.fieldDefinitions.associateBy { it.name }
// @provides is applicable on both base and federated types and always references @external fields
errors.addAll(
validateDirective(
"$federatedType.${field.name}",
"provides",
field.directivesByName,
returnTypeFields,
true))
}
} else {
errors.add("@provides directive is specified on a $federatedType.${field.name} field but it does not return an object type")
}
return errors
}

private fun validateDirective(
validatedType: String,
targetDirective: String,
directives: Map<String, GraphQLDirective>,
fieldMap: Map<String, GraphQLFieldDefinition>,
extendedType: Boolean
): List<String> {
val validationErrors = mutableListOf<String>()
val directive = directives[targetDirective]

if (directive == null) {
validationErrors.add("@$targetDirective directive is missing on federated $validatedType type")
} else {
val fieldSetValue = (directive.getArgument("fields")?.value as? FieldSet)?.value
val fieldSet = fieldSetValue?.split(" ")?.filter { it.isNotEmpty() }.orEmpty()
if (fieldSet.isEmpty()) {
validationErrors.add("@$targetDirective directive on $validatedType is missing field information")
} else {
// validate key field set
val validatedDirectiveInfo = "@$targetDirective(fields = $fieldSetValue) directive on $validatedType"
validateFieldSelection(validatedDirectiveInfo, fieldSet.iterator(), fieldMap, extendedType, validationErrors)
}
}
return validationErrors
}

private fun validateFieldSelection(
validatedDirective: String,
iterator: Iterator<String>,
fields: Map<String, GraphQLFieldDefinition>,
extendedType: Boolean,
errors: MutableList<String>
) {
var previousField: String? = null
while (iterator.hasNext()) {
val currentField = iterator.next()
when (currentField) {
"{" -> {
val targetField = fields[previousField]?.type
when (val unwrappedType = GraphQLTypeUtil.unwrapAll(targetField)) {
is GraphQLInterfaceType -> validateFieldSelection(validatedDirective, iterator, unwrappedType.fieldDefinitions.associateBy { it.name }, extendedType, errors)
is GraphQLObjectType -> validateFieldSelection(validatedDirective, iterator, unwrappedType.fieldDefinitions.associateBy { it.name }, extendedType, errors)
else -> errors.add("$validatedDirective specifies invalid field set - field set defines nested selection set on unsupported type")
}
}
"}" -> return
else -> validateKeySetField(fields[currentField], extendedType, errors, validatedDirective)
}
previousField = currentField
}
}

private fun validateKeySetField(targetField: GraphQLFieldDefinition?, extendedType: Boolean, errors: MutableList<String>, validatedDirective: String) {
if (null != targetField) {
val externalField = targetField.getDirective("external") != null
if (extendedType && !externalField) {
errors.add("$validatedDirective specifies invalid field set - extended type incorrectly references local field=${targetField.name}")
} else if (!extendedType && externalField) {
errors.add("$validatedDirective specifies invalid field set - type incorrectly references external field=${targetField.name}")
}

when (GraphQLTypeUtil.unwrapNonNull(targetField.type)) {
is GraphQLList -> errors.add("$validatedDirective specifies invalid field set - field set references GraphQLList, field=${targetField.name}")
is GraphQLInterfaceType -> errors.add("$validatedDirective specifies invalid field set - field set references GraphQLInterfaceType, field=${targetField.name}")
is GraphQLUnionType -> errors.add("$validatedDirective specifies invalid field set - field set references GraphQLUnionType, field=${targetField.name}")
}
} else {
errors.add("$validatedDirective specifies invalid field set - field set specifies fields that do not exist")
}
}

private fun GraphQLDirectiveContainer.isFederatedType() = this.getDirective("key") != null || isExtendedType()

private fun GraphQLDirectiveContainer.isExtendedType() = this.getDirective("extends") != null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.federation.extensions

import graphql.schema.GraphQLDirectiveContainer

internal fun GraphQLDirectiveContainer.isFederatedType() = this.getDirective("key") != null || isExtendedType()

internal fun GraphQLDirectiveContainer.isExtendedType() = this.getDirective("extends") != null
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ private object AnyCoercing : Coercing<Any, Any> {
is ObjectValue -> input.objectFields
.stream()
.collect(Collectors.toMap(ObjectField::getName) { parseLiteral(it.value) })
else -> Assert.assertShouldNeverHappen<Any>()
else -> Assert.assertShouldNeverHappen()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.federation.validation

import com.expediagroup.graphql.federation.directives.FieldSet
import graphql.schema.GraphQLDirective
import graphql.schema.GraphQLFieldDefinition

internal fun validateDirective(
validatedType: String,
targetDirective: String,
directives: Map<String, GraphQLDirective>,
fieldMap: Map<String, GraphQLFieldDefinition>,
extendedType: Boolean
): List<String> {
val validationErrors = mutableListOf<String>()
val directive = directives[targetDirective]

if (directive == null) {
validationErrors.add("@$targetDirective directive is missing on federated $validatedType type")
} else {
val fieldSetValue = (directive.getArgument("fields")?.value as? FieldSet)?.value
val fieldSet = fieldSetValue?.split(" ")?.filter { it.isNotEmpty() }.orEmpty()
if (fieldSet.isEmpty()) {
validationErrors.add("@$targetDirective directive on $validatedType is missing field information")
} else {
// validate key field set
val validatedDirectiveInfo = "@$targetDirective(fields = $fieldSetValue) directive on $validatedType"
validateFieldSelection(validatedDirectiveInfo, fieldSet.iterator(), fieldMap, extendedType, validationErrors)
}
}
return validationErrors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.federation.validation

import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLTypeUtil

internal fun validateFieldSelection(
validatedDirective: String,
iterator: Iterator<String>,
fields: Map<String, GraphQLFieldDefinition>,
extendedType: Boolean,
errors: MutableList<String>
) {
var previousField: String? = null
while (iterator.hasNext()) {
val currentField = iterator.next()
when (currentField) {
"{" -> {
val targetField = fields[previousField]?.type
when (val unwrappedType = GraphQLTypeUtil.unwrapAll(targetField)) {
is GraphQLInterfaceType -> validateFieldSelection(validatedDirective, iterator, unwrappedType.fieldDefinitions.associateBy { it.name }, extendedType, errors)
is GraphQLObjectType -> validateFieldSelection(validatedDirective, iterator, unwrappedType.fieldDefinitions.associateBy { it.name }, extendedType, errors)
else -> errors.add("$validatedDirective specifies invalid field set - field set defines nested selection set on unsupported type")
}
}
"}" -> return
else -> validateKeySetField(fields[currentField], extendedType, errors, validatedDirective)
}
previousField = currentField
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.federation.validation

import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLList
import graphql.schema.GraphQLTypeUtil
import graphql.schema.GraphQLUnionType

internal fun validateKeySetField(targetField: GraphQLFieldDefinition?, extendedType: Boolean, errors: MutableList<String>, validatedDirective: String) {
if (null != targetField) {
val externalField = targetField.getDirective("external") != null
if (extendedType && !externalField) {
errors.add("$validatedDirective specifies invalid field set - extended type incorrectly references local field=${targetField.name}")
} else if (!extendedType && externalField) {
errors.add("$validatedDirective specifies invalid field set - type incorrectly references external field=${targetField.name}")
}

when (GraphQLTypeUtil.unwrapNonNull(targetField.type)) {
is GraphQLList -> errors.add("$validatedDirective specifies invalid field set - field set references GraphQLList, field=${targetField.name}")
is GraphQLInterfaceType -> errors.add("$validatedDirective specifies invalid field set - field set references GraphQLInterfaceType, field=${targetField.name}")
is GraphQLUnionType -> errors.add("$validatedDirective specifies invalid field set - field set references GraphQLUnionType, field=${targetField.name}")
}
} else {
errors.add("$validatedDirective specifies invalid field set - field set specifies fields that do not exist")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.federation.validation

import com.expediagroup.graphql.federation.extensions.isExtendedType
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLTypeUtil

// [OK] @provides on base type references valid @external fields on @extend object
// [ERROR] @provides on base type references local object fields
// [ERROR] @provides on base type references local fields on @extends object
// [ERROR] @provides references interface type
// [OK] @provides references list of valid @extend objects
// [ERROR] @provides references @external list field
// [ERROR] @provides references @external interface field
internal fun validateProvidesDirective(federatedType: String, field: GraphQLFieldDefinition): List<String> {
val errors = mutableListOf<String>()
val returnType = GraphQLTypeUtil.unwrapAll(field.type)
if (returnType is GraphQLObjectType) {
if (!returnType.isExtendedType()) {
errors.add("@provides directive is specified on a $federatedType.${field.name} field references local object")
} else {
val returnTypeFields = returnType.fieldDefinitions.associateBy { it.name }
// @provides is applicable on both base and federated types and always references @external fields
errors.addAll(
validateDirective(
"$federatedType.${field.name}",
"provides",
field.directivesByName,
returnTypeFields,
true))
}
} else {
errors.add("@provides directive is specified on a $federatedType.${field.name} field but it does not return an object type")
}
return errors
}
Loading