graphql-spqr icon indicating copy to clipboard operation
graphql-spqr copied to clipboard

Input Argumnents are not recoginized

Open hantsy opened this issue 4 years ago • 0 comments

The input type:

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class CreatePostInput {
    @NotEmpty
    @Length(min = 5, max = 100)
    String title;

    @NotEmpty
    String content;
}

GraphQLMutation is like this:

  @GraphQLMutation(name = "createPost", description = "create a new post")
  public Post createPost(@GraphQLArgument(name = "createPostInput") @GraphQLNonNull CreatePostInput input) {
      return this.postService.createPost(input);
  }

I added a test to send a mutation query.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MutationTests {

    @LocalServerPort
    int port;

    WebTestClient webClient;

    @BeforeEach
    void setUp() {
        this.webClient = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build();
    }

    @SneakyThrows
    @Test
    public void testCreatePost() {
        var query = """
                mutation createPost($input: CreatePostInput){
                    createPost(createPostInput:$input){
                        id
                        title
                        content
                    }
                }
                """.trim();
        var variables = Map.of(
                "input", Map.of(
                        "title", "test title",
                        "content", "test content"
                )
        );
        Map<String, Object> body = Map.of("query", query, "variables", variables);
        webClient.post().uri("/graphql")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .bodyValue(body)
                .exchange()
                .expectStatus().isOk()
                .expectBody()
                .jsonPath("data.createPost.id").exists()
                .jsonPath("data.createPost.content").isEqualTo("test content")
                .jsonPath("data.createPost.title").isEqualTo("test title");

    }
}

When sending a mutation query, I got the following errors.

2021-10-09 13:39:53.396 DEBUG 17492 --- [o-auto-1-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [io.leangen.graphql.spqr.spring.web.dto.GraphQLRequest@514cc06a]
2021-10-09 13:39:53.548  WARN 17492 --- [o-auto-1-exec-1] notprivacysafe.graphql.GraphQL           : Query failed to validate : 'mutation createPost($input: CreatePostInput){
    createPost(createPostInput:$input){
        id
        title
        content
    }
}'
2021-10-09 13:39:53.564 DEBUG 17492 --- [o-auto-1-exec-1] o.s.w.c.request.async.WebAsyncManager    : Started async request
2021-10-09 13:39:53.565 DEBUG 17492 --- [o-auto-1-exec-1] o.s.w.c.request.async.WebAsyncManager    : Async result set, dispatch to /graphql
2021-10-09 13:39:53.568 DEBUG 17492 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting but response remains open for further handling
2021-10-09 13:39:53.574 DEBUG 17492 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : "ASYNC" dispatch for POST "/graphql", parameters={}
2021-10-09 13:39:53.576 DEBUG 17492 --- [o-auto-1-exec-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Resume with async result [{errors=[{message=Validation error of type VariableTypeMismatch: Variable type 'CreatePostInput' doe (truncated)...]
2021-10-09 13:39:53.581 DEBUG 17492 --- [o-auto-1-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [application/json] and supported [application/json]
2021-10-09 13:39:53.583 DEBUG 17492 --- [o-auto-1-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing [{errors=[{message=Validation error of type VariableTypeMismatch: Variable type 'CreatePostInput' doe (truncated)...]
2021-10-09 13:39:53.603 DEBUG 17492 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ASYNC" dispatch, status 200

The complete source codes can be found on my Github. I have tried to use multi simple arguments(title, content...) in the createPost method, it worked.

hantsy avatar Oct 09 '21 05:10 hantsy