Spring boot graphql guide Illegal Argument Exception

225 Views Asked by At

I am trying to run Springboot Graphql starter project from https://spring.io/guides/gs/graphql-server/. My IDE Gradle compiler shows as Java 16.

I am using Java 17 and Spring Tool Suite IDE.

since it is the sample project from spring website, I dont see any issue in the code. I am not sure if java version 17 has some issues with spring 3.2.1

When I run the server, and post a query.

I am getting an exception like:

ExceptionResolversExceptionHandler[0;39m [2m:[0;39m Unresolved IllegalArgumentException for executionId 558526c2-e382-1790-634f-ce88818aad13

java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not found in class file either.
    at org.springframework.graphql.data.method.annotation.support.ArgumentMethodArgumentResolver.getArgumentName(ArgumentMethodArgumentResolver.java:92) ~[spring-graphql-1.2.4.jar:1.2.4]
    at org.springframework.graphql.data.method.annotation.support.ArgumentMethodArgumentResolver.resolveArgument(ArgumentMethodArgumentResolver.java:69) ~[spring-graphql-1.2.4.jar:1.2.4]
    at org.springframework.graphql.data.method.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:81) ~[spring-graphql-1.2.4.jar:1.2.4]
    at org.springframework.graphql.data.method.annotation.support.DataFetcherHandlerMethod.getMethodArgumentValues(DataFetcherHandlerMethod.java:177) ~[spring-graphql-1.2.4.jar:1.2.4]
    at org.springframework.graphql.data.method.annotation.support.DataFetcherHandlerMethod.invoke(DataFetcherHandlerMethod.java:119) ~[spring-graphql-1.2.4.jar:1.2.4]

This is my controller class

@Controller
public class BookController {
    @QueryMapping
    public Book bookById(@Argument String id) {
        return Book.getById(id);
    }

    @SchemaMapping
    public Author author(Book book) {
        return Author.getById(book.authorId());
    }

and my request is like this.

query bookDetails {
  bookById(id: "book-1") {
    id
    name
    pageCount
    author {
      id
      firstName
      lastName
    }
  }
}

I am using Java 17 and Spring Tool Suite IDE.

My build file looks like this:

    id 'java'
    id 'org.springframework.boot' version '3.2.1'
    id 'io.spring.dependency-management' version '1.1.4'
 

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
    targetCompatibility = '17'
}



repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-graphql'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework:spring-webflux'
    testImplementation 'org.springframework.graphql:spring-graphql-test'
}

tasks.named('test') {
    useJUnitPlatform()
} ```


Please help me on this one. I have no clue as to what is missing.
1

There are 1 best solutions below

0
On

I ran into this guide to get started in GraphQL + Spring (for my first time) and I got the same problem.

After many workarounds I tried this one, and just changing the @Argument decorator syntax in the BookController it works fine:

Then:

@QueryMapping
public Book bookById(@Argument String id) {
    return Book.getById(id);
}

Now:

@QueryMapping
public Book bookById(@Argument("id") String id) {
    return Book.getById(id);
}

It seems a new feature in the most recent Spring version (3.2.1)