Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add graphql-java-validation module
- Loading branch information
Showing
10 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 2020 Netflix, 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
dependencies { | ||
api(project(":graphql-dgs")) | ||
api("com.graphql-java:graphql-java-extended-validation") | ||
implementation("org.springframework.boot:spring-boot-autoconfigure") | ||
|
||
testImplementation(project(":graphql-dgs-spring-boot-starter")) | ||
} |
23 changes: 23 additions & 0 deletions
23
...on/src/main/java/com/netflix/graphql/dgs/autoconfig/ValidationRulesBuilderCustomizer.java
This file contains 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 2021 Netflix, 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 | ||
* | ||
* http://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.netflix.graphql.dgs.autoconfig; | ||
|
||
import graphql.validation.rules.ValidationRules; | ||
|
||
@FunctionalInterface | ||
public interface ValidationRulesBuilderCustomizer { | ||
void customize(ValidationRules.Builder builder); | ||
} |
65 changes: 65 additions & 0 deletions
65
.../main/kotlin/com/netflix/graphql/dgs/autoconfig/DgsExtendedValidationAutoConfiguration.kt
This file contains 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,65 @@ | ||
/* | ||
* Copyright 2021 Netflix, 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 | ||
* | ||
* http://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.netflix.graphql.dgs.autoconfig | ||
|
||
import com.netflix.graphql.dgs.DgsComponent | ||
import com.netflix.graphql.dgs.DgsRuntimeWiring | ||
import graphql.schema.idl.RuntimeWiring | ||
import graphql.validation.rules.ValidationRules | ||
import graphql.validation.schemawiring.ValidationSchemaWiring | ||
import org.springframework.beans.factory.ObjectProvider | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@ConditionalOnClass(graphql.validation.rules.ValidationRules::class) | ||
@ConditionalOnProperty( | ||
prefix = "dgs.graphql.extensions.validation", | ||
name = ["enabled"], | ||
havingValue = "true", | ||
matchIfMissing = true | ||
) | ||
@Configuration(proxyBeanMethods = false) | ||
open class DgsExtendedValidationAutoConfiguration { | ||
|
||
@Bean | ||
open fun defaultExtendedValidationRegistrar(validationRulesCustomizerProvider: ObjectProvider<ValidationRulesBuilderCustomizer>): DefaultExtendedValidationRegistrar { | ||
return DefaultExtendedValidationRegistrar(validationRulesCustomizerProvider) | ||
} | ||
|
||
@DgsComponent | ||
@FunctionalInterface | ||
fun interface ExtendedValidationRegistrar { | ||
fun addValidationRules(builder: RuntimeWiring.Builder): RuntimeWiring.Builder | ||
} | ||
|
||
open class DefaultExtendedValidationRegistrar(private val validationRulesCustomizerProvider: ObjectProvider<ValidationRulesBuilderCustomizer>) : | ||
ExtendedValidationRegistrar { | ||
|
||
@DgsRuntimeWiring | ||
override fun addValidationRules(builder: RuntimeWiring.Builder): RuntimeWiring.Builder { | ||
val validationRulesBuilder = ValidationRules.newValidationRules() | ||
validationRulesCustomizerProvider.ifAvailable { it.customize(validationRulesBuilder) } | ||
|
||
val validationRules = validationRulesBuilder.build() | ||
val schemaWiring = ValidationSchemaWiring(validationRules) | ||
// we add this schema wiring to the graphql runtime | ||
return builder.directiveWiring(schemaWiring) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...nded-validation/src/main/resources/META-INF/additional-spring-configuration-metadata.json
This file contains 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,11 @@ | ||
{ | ||
"groups": [], | ||
"properties": [ | ||
{ | ||
"name": "dgs.graphql.extensions.validation.enabled", | ||
"type": "java.lang.Boolean", | ||
"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." | ||
} | ||
], | ||
"hints": [] | ||
} |
2 changes: 2 additions & 0 deletions
2
graphql-dgs-extended-validation/src/main/resources/META-INF/spring.factories
This file contains 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,2 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.netflix.graphql.dgs.autoconfig.DgsExtendedValidationAutoConfiguration |
86 changes: 86 additions & 0 deletions
86
...idation/src/test/kotlin/com/netflix/graphql/dgs/autoconfig/BeanValidationSizeSmokeTest.kt
This file contains 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,86 @@ | ||
/* | ||
* Copyright 2021 Netflix, 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 | ||
* | ||
* http://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.netflix.graphql.dgs.autoconfig | ||
|
||
import com.netflix.graphql.dgs.* | ||
import graphql.schema.idl.SchemaParser | ||
import graphql.schema.idl.TypeDefinitionRegistry | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.SpringBootConfiguration | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import java.util.* | ||
|
||
@SpringBootTest(classes = [BeanValidationSizeSmokeTest.LocalApp::class]) | ||
@EnableAutoConfiguration | ||
internal class BeanValidationSizeSmokeTest { | ||
|
||
@Autowired | ||
lateinit var queryExecutor: DgsQueryExecutor | ||
|
||
@Test | ||
fun createPostInputValidationFailed() { | ||
val query = "mutation newPost(\$input: CreatePostInput!){ createPost(createPostInput: \$input) }" | ||
var variables = mapOf( | ||
"input" to mapOf( | ||
"title" to "test", | ||
"content" to "test content" | ||
) | ||
) | ||
val executionResult = queryExecutor.execute(query, variables) | ||
assertThat(executionResult.errors).isNotEmpty() | ||
} | ||
|
||
@SpringBootConfiguration(proxyBeanMethods = false) | ||
@SuppressWarnings("unused") | ||
open class LocalApp { | ||
|
||
@DgsComponent | ||
class ExampleImplementation { | ||
|
||
@DgsTypeDefinitionRegistry | ||
fun typeDefinitionRegistry(): TypeDefinitionRegistry { | ||
val schemaParser = SchemaParser() | ||
|
||
val gqlSchema = """ | ||
| type Mutation { | ||
| createPost(createPostInput: CreatePostInput!): String! | ||
| } | ||
| | ||
| input CreatePostInput { | ||
| title: String! @Size(min:5, max:50) | ||
| content: String! | ||
| } | ||
| | ||
| directive @Size(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Size.message") | ||
| on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ||
""".trimMargin() | ||
return schemaParser.parse(gqlSchema) | ||
} | ||
|
||
@DgsMutation | ||
fun createPost(@InputArgument createPostInput: CreatePostInput): String { | ||
println(createPostInput) | ||
return UUID.randomUUID().toString() | ||
} | ||
} | ||
|
||
data class CreatePostInput(val title: String, val content: String) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../kotlin/com/netflix/graphql/dgs/autoconfig/DgsExtendedValidationAutoConfigurationTests.kt
This file contains 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,38 @@ | ||
/* | ||
* Copyright 2021 Netflix, 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 | ||
* | ||
* http://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.netflix.graphql.dgs.autoconfig | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.autoconfigure.AutoConfigurations | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner | ||
|
||
internal class DgsExtendedValidationAutoConfigurationTests { | ||
|
||
private val context = | ||
ApplicationContextRunner().withConfiguration( | ||
AutoConfigurations.of(DgsExtendedValidationAutoConfiguration::class.java) | ||
) | ||
|
||
@Test | ||
fun `BeanValidation Autoconfiguration elements are available by default`() { | ||
context.run { context -> | ||
assertThat(context) | ||
.hasSingleBean(DgsExtendedValidationAutoConfiguration::class.java) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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