Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cd974f7

Browse files
committedSep 25, 2021
feat: add graphql-java-validation module
1 parent df419f9 commit cd974f7

File tree

10 files changed

+253
-0
lines changed

10 files changed

+253
-0
lines changed
 

Diff for: ‎buildSrc/src/main/kotlin/Versions.kt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object Versions {
2222
const val SPRING_CLOUD_VERSION = "Hoxton.SR10"
2323
const val GRAPHQL_JAVA = "17.3"
2424
const val GRAPHQL_JAVA_EXTENDED_SCALARS = "17.0"
25+
const val GRAPHQL_JAVA_EXTENDED_VALIDATION = "17.0"
2526
const val GRAPHQL_JAVA_FEDERATION = "0.7.0"
2627
const val JACKSON_BOM = "2.12.3"
2728
}

Diff for: ‎graphql-dgs-extended-validation/build.gradle.kts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020 Netflix, 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+
* http://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+
dependencies {
18+
api(project(":graphql-dgs"))
19+
api("com.graphql-java:graphql-java-extended-validation")
20+
implementation("org.springframework.boot:spring-boot-autoconfigure")
21+
22+
testImplementation(project(":graphql-dgs-spring-boot-starter"))
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2021 Netflix, 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+
* http://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+
package com.netflix.graphql.dgs.autoconfig;
17+
18+
import graphql.validation.rules.ValidationRules;
19+
20+
@FunctionalInterface
21+
public interface ValidationRulesBuilderCustomizer {
22+
void customize(ValidationRules.Builder builder);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 Netflix, 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+
* http://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.netflix.graphql.dgs.autoconfig
18+
19+
import com.netflix.graphql.dgs.DgsComponent
20+
import com.netflix.graphql.dgs.DgsRuntimeWiring
21+
import graphql.schema.idl.RuntimeWiring
22+
import graphql.validation.rules.ValidationRules
23+
import graphql.validation.schemawiring.ValidationSchemaWiring
24+
import org.springframework.beans.factory.ObjectProvider
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
27+
import org.springframework.context.annotation.Bean
28+
import org.springframework.context.annotation.Configuration
29+
30+
@ConditionalOnClass(graphql.validation.rules.ValidationRules::class)
31+
@ConditionalOnProperty(
32+
prefix = "dgs.graphql.extensions.validation",
33+
name = ["enabled"],
34+
havingValue = "true",
35+
matchIfMissing = true
36+
)
37+
@Configuration(proxyBeanMethods = false)
38+
open class DgsExtendedValidationAutoConfiguration {
39+
40+
@Bean
41+
open fun defaultExtendedValidationRegistrar(validationRulesCustomizerProvider: ObjectProvider<ValidationRulesBuilderCustomizer>): DefaultExtendedValidationRegistrar {
42+
return DefaultExtendedValidationRegistrar(validationRulesCustomizerProvider)
43+
}
44+
45+
@DgsComponent
46+
@FunctionalInterface
47+
fun interface ExtendedValidationRegistrar {
48+
fun addValidationRules(builder: RuntimeWiring.Builder): RuntimeWiring.Builder
49+
}
50+
51+
open class DefaultExtendedValidationRegistrar(private val validationRulesCustomizerProvider: ObjectProvider<ValidationRulesBuilderCustomizer>) :
52+
ExtendedValidationRegistrar {
53+
54+
@DgsRuntimeWiring
55+
override fun addValidationRules(builder: RuntimeWiring.Builder): RuntimeWiring.Builder {
56+
val validationRulesBuilder = ValidationRules.newValidationRules()
57+
validationRulesCustomizerProvider.ifAvailable { it.customize(validationRulesBuilder) }
58+
59+
val validationRules = validationRulesBuilder.build()
60+
val schemaWiring = ValidationSchemaWiring(validationRules)
61+
// we add this schema wiring to the graphql runtime
62+
return builder.directiveWiring(schemaWiring)
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"groups": [],
3+
"properties": [
4+
{
5+
"name": "dgs.graphql.extensions.validation.enabled",
6+
"type": "java.lang.Boolean",
7+
"description": "If enabled, will provide an auto-configuration that will by default registered the Bean Validation Extensions available in graphql-java-extended-validation for the DGS Framework."
8+
}
9+
],
10+
"hints": []
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
com.netflix.graphql.dgs.autoconfig.DgsExtendedValidationAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2021 Netflix, 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+
* http://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.netflix.graphql.dgs.autoconfig
18+
19+
import com.netflix.graphql.dgs.*
20+
import graphql.schema.idl.SchemaParser
21+
import graphql.schema.idl.TypeDefinitionRegistry
22+
import org.assertj.core.api.Assertions.assertThat
23+
import org.junit.jupiter.api.Test
24+
import org.springframework.beans.factory.annotation.Autowired
25+
import org.springframework.boot.SpringBootConfiguration
26+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
27+
import org.springframework.boot.test.context.SpringBootTest
28+
import java.util.*
29+
30+
@SpringBootTest(classes = [BeanValidationSizeSmokeTest.LocalApp::class])
31+
@EnableAutoConfiguration
32+
internal class BeanValidationSizeSmokeTest {
33+
34+
@Autowired
35+
lateinit var queryExecutor: DgsQueryExecutor
36+
37+
@Test
38+
fun createPostInputValidationFailed() {
39+
val query = "mutation newPost(\$input: CreatePostInput!){ createPost(createPostInput: \$input) }"
40+
var variables = mapOf(
41+
"input" to mapOf(
42+
"title" to "test",
43+
"content" to "test content"
44+
)
45+
)
46+
val executionResult = queryExecutor.execute(query, variables)
47+
assertThat(executionResult.errors).isNotEmpty()
48+
}
49+
50+
@SpringBootConfiguration(proxyBeanMethods = false)
51+
@SuppressWarnings("unused")
52+
open class LocalApp {
53+
54+
@DgsComponent
55+
class ExampleImplementation {
56+
57+
@DgsTypeDefinitionRegistry
58+
fun typeDefinitionRegistry(): TypeDefinitionRegistry {
59+
val schemaParser = SchemaParser()
60+
61+
val gqlSchema = """
62+
| type Mutation {
63+
| createPost(createPostInput: CreatePostInput!): String!
64+
| }
65+
|
66+
| input CreatePostInput {
67+
| title: String! @Size(min:5, max:50)
68+
| content: String!
69+
| }
70+
|
71+
| directive @Size(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Size.message")
72+
| on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
73+
""".trimMargin()
74+
return schemaParser.parse(gqlSchema)
75+
}
76+
77+
@DgsMutation
78+
fun createPost(@InputArgument createPostInput: CreatePostInput): String {
79+
println(createPostInput)
80+
return UUID.randomUUID().toString()
81+
}
82+
}
83+
84+
data class CreatePostInput(val title: String, val content: String)
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2021 Netflix, 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+
* http://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.netflix.graphql.dgs.autoconfig
18+
19+
import org.assertj.core.api.Assertions.assertThat
20+
import org.junit.jupiter.api.Test
21+
import org.springframework.boot.autoconfigure.AutoConfigurations
22+
import org.springframework.boot.test.context.runner.ApplicationContextRunner
23+
24+
internal class DgsExtendedValidationAutoConfigurationTests {
25+
26+
private val context =
27+
ApplicationContextRunner().withConfiguration(
28+
AutoConfigurations.of(DgsExtendedValidationAutoConfiguration::class.java)
29+
)
30+
31+
@Test
32+
fun `BeanValidation Autoconfiguration elements are available by default`() {
33+
context.run { context ->
34+
assertThat(context)
35+
.hasSingleBean(DgsExtendedValidationAutoConfiguration::class.java)
36+
}
37+
}
38+
}

Diff for: ‎graphql-dgs-platform/build.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ dependencies {
5151
api("com.graphql-java:graphql-java-extended-scalars") {
5252
version { require(Versions.GRAPHQL_JAVA_EXTENDED_SCALARS) }
5353
}
54+
api("com.graphql-java:graphql-java-extended-validation") {
55+
version { require(Versions.GRAPHQL_JAVA_EXTENDED_VALIDATION) }
56+
}
5457
api("com.apollographql.federation:federation-graphql-java-support") {
5558
version { require(Versions.GRAPHQL_JAVA_FEDERATION) }
5659
}

Diff for: ‎settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ include("graphql-dgs-spring-boot-micrometer")
3333
include("graphql-dgs-platform")
3434
include("graphql-dgs-platform-dependencies")
3535
include("graphql-dgs-extended-scalars")
36+
include("graphql-dgs-extended-validation")
3637
include("graphql-dgs-spring-webflux-autoconfigure")
3738
include("graphql-dgs-reactive")
3839
include("graphql-dgs-webflux-starter")

0 commit comments

Comments
 (0)
Please sign in to comment.