leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 179 forks source link

Input Argumnents are not recoginized #412

Closed hantsy closed 1 year ago

hantsy commented 2 years ago

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.

baohouse commented 1 year ago

You need to add @GraphQLType annotation to the CreatePostInput class.

kaqqao commented 1 year ago

I presume you've figured this out in the meantime. Could you please share what was it?

My gut feeling is it's Lombok doing something bonkers. Because every time things are mysteriously missing from the schema - Lombok is involved. So I'd investigate in that direction. Otherwise, I don't see anything suspicious.

Since this is an old issue, I'll close it, but feel free to reopen it if you still need help.