SpringBoot graphQL getting Invalid Syntax for nested Object

SpringBoot graphQL getting Invalid Syntax for nested Object


0

I am using GraphQL in my SpringBoot app. While the CRUD operations are running for single objects, I am facing the issue of Invalid Syntax for nested objects. Please find the code below and suggest.

graphql

type Book {
    isn: Int
    title: String
    author: String
    publishedDate: String
    publisher: Publisher!
}


input PublisherInput {
    pId : Int
    publisherName: String
    address: String
}

type Mutation {
    addBook(isn: Int, title: String, author: String, publishedDate: String, publisherInput: PublisherInput) : Book
}

Service


    public DataFetcher<Book> addBook() {
        return dataFetchingEnvironment -> {
            PublisherInput publisherInput = objectMapper.convertValue(dataFetchingEnvironment.getArgument("publisherInput"), PublisherInput.class);
            Publisher publisher = modelMapper.map(publisherInput, Publisher.class);
            publisherRepository.save(publisher);
            Book book = new Book(dataFetchingEnvironment.getArgument("isn"), dataFetchingEnvironment.getArgument("title"), dataFetchingEnvironment.getArgument("author"), dataFetchingEnvironment.getArgument("publishedDate"), publisher);
            bookRepository.save(book);
            return book;
        };
    }

Request

mutation {
    addBook(
        isn: 100,
        title : "Book Test",
        author : "aabb",
        publishedDate : "12-11-2012",
        publisherInput(
            pId: 2002,
            publisherName: "test",
            address: "aassd"
        )
    )
    {
        isn
        title
    }
}

Getting the below error

Response


{
    "data": null,
    "errors": [
        {
            "message": "Invalid Syntax",
            "locations": [
                {
                    "line": 7,
                    "column": 16,
                    "sourceName": null
                }
            ]
}

Share
Improve this question

1 Answer
1

Reset to default


0

Think that you should use syntax someArgumentName : {} rather than someArgumentName() when specifying the mutation argument which the type is the input object.

So change to the following should solve the problem :

mutation {
    addBook(
        isn: 100,
        title : "Book Test",
        author : "aabb",
        publishedDate : "12-11-2012",
        publisherInput : { 
            pId: 2002 ,
            publisherName: "test",
            address: "aassd"
        }
    )
    {
        isn
        title
    }
}

Share
Improve this answer



Leave a Reply

Your email address will not be published. Required fields are marked *