-
Notifications
You must be signed in to change notification settings - Fork 362
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...lin/com/expediagroup/graphql/federation/extensions/GraphQLDirectiveContainerExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ation/src/main/kotlin/com/expediagroup/graphql/federation/validation/validateDirective.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
48 changes: 48 additions & 0 deletions
48
.../src/main/kotlin/com/expediagroup/graphql/federation/validation/validateFieldSelection.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ion/src/main/kotlin/com/expediagroup/graphql/federation/validation/validateKeySetField.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...c/main/kotlin/com/expediagroup/graphql/federation/validation/validateProvidesDirective.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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