|
| 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 | +} |
0 commit comments