Skip to content

Commit b7ef5e4

Browse files
dariuszkucsmyrick
andauthored
[plugin] improve client generation exception handling (#915)
* [plugin] improve client generation exception handling Changes * use typed exceptions instead of generic `RuntimeException` * validate whether fragments are valid Related: #903 * fix ktlint * Apply suggestions from code review Co-authored-by: Shane Myrick <mail@shanemyrick.com> * fix code to use updated exception Co-authored-by: Shane Myrick <mail@shanemyrick.com>
1 parent 65e42a1 commit b7ef5e4

20 files changed

+280
-36
lines changed

plugins/graphql-kotlin-plugin-core/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ tasks {
2323
limit {
2424
counter = "INSTRUCTION"
2525
value = "COVEREDRATIO"
26-
minimum = "0.85".toBigDecimal()
26+
minimum = "0.90".toBigDecimal()
2727
}
2828
limit {
2929
counter = "BRANCH"
3030
value = "COVEREDRATIO"
31-
minimum = "0.65".toBigDecimal()
31+
minimum = "0.75".toBigDecimal()
3232
}
3333
}
3434
}

plugins/graphql-kotlin-plugin-core/src/main/kotlin/com/expediagroup/graphql/plugin/generator/GraphQLClientGenerator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.expediagroup.graphql.plugin.generator
1818

19+
import com.expediagroup.graphql.plugin.generator.exceptions.MultipleOperationsInFileException
1920
import com.expediagroup.graphql.plugin.generator.types.generateGraphQLObjectTypeSpec
2021
import com.expediagroup.graphql.plugin.generator.types.generateVariableTypeSpec
2122
import com.squareup.kotlinpoet.ClassName
@@ -81,7 +82,7 @@ class GraphQLClientGenerator(
8182

8283
val operationDefinitions = queryDocument.definitions.filterIsInstance(OperationDefinition::class.java)
8384
if (operationDefinitions.size > 1) {
84-
throw RuntimeException("GraphQL client does not support query files with multiple operations")
85+
throw MultipleOperationsInFileException
8586
}
8687

8788
val fileSpec = FileSpec.builder(packageName = config.packageName, fileName = queryFile.nameWithoutExtension.capitalize())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
/**
20+
* Exception thrown when query specifies deprecated fields but client configuration does not allow it.
21+
*/
22+
internal class DeprecatedFieldsSelectedException(field: String, type: String) :
23+
RuntimeException("Operation specifies deprecated field - $field in $type. Update your operation or update your configuration to allow usage of deprecated fields")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
/**
20+
* Exception thrown when query file specifies invalid fragment.
21+
*/
22+
internal class InvalidFragmentException(fragmentName: String, targetType: String) :
23+
RuntimeException("Invalid fragment, $fragmentName fragment not found or does not reference valid $targetType type")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
/**
20+
* Exception thrown when query contains invalid polymorphic selection sets.
21+
*/
22+
internal class InvalidPolymorphicQueryException(message: String) : RuntimeException(message)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
/**
20+
* Exception thrown when specified query file contains invalid selection set.
21+
*/
22+
internal class InvalidSelectionSetException(typeDefinitionName: String, typeName: String) :
23+
RuntimeException("Invalid selection set for $typeName - cannot select empty $typeDefinitionName")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
/**
20+
* Exception thrown when attempting to generate a client from a query file containing multiple operations.
21+
*/
22+
internal object MultipleOperationsInFileException : RuntimeException("GraphQL client does not support query files with multiple operations")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
/**
20+
* Exception thrown when specified query file references unknown fields.
21+
*/
22+
internal class UnknownFieldSelectedException(fieldName: String, typeDefinitionName: String) :
23+
RuntimeException("Invalid selection set for $typeDefinitionName - unknown $fieldName field selected")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.exceptions
18+
19+
import graphql.language.Node
20+
21+
/**
22+
* This exception should never be thrown. It is used to enforce type safety of type name generation operations. Exception
23+
* is used to mark unreachable code execution branches that ensures non null response from all valid branches.
24+
*/
25+
internal class UnknownGraphQLTypeException(graphQLType: Node<*>) :
26+
RuntimeException("Client generation failure - attempting to generate code for unsupported GraphQL type $graphQLType")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.plugin.generator.extensions
18+
19+
import com.expediagroup.graphql.plugin.generator.exceptions.InvalidFragmentException
20+
import graphql.language.Document
21+
import graphql.language.FragmentDefinition
22+
23+
internal fun Document.findFragmentDefinition(targetFragment: String, targetType: String): FragmentDefinition =
24+
this.getDefinitionsOfType(FragmentDefinition::class.java)
25+
.find { it.name == targetFragment && it.typeCondition.name == targetType } ?: throw InvalidFragmentException(targetFragment, targetType)

0 commit comments

Comments
 (0)