SpringBoot not exposing GraphQL

SpringBoot not exposing GraphQL


0

I have a multi-module project where one of them is the GraphQL module
the Runnable File is in a different module with a working RestController
the GraphQLController is not exposed.
I have tried to move the application.properties to the runnable module didn’t work, tried to move the GraphQLController also didn’t work I even moved all files to the runnable module and nothing happened.
my dependencies are fine my gradle is fine
I am sure my code works but only separately I tried it and it did work but I’m working on a multi-module project so I can’t just run it separately.
what seems the problem here, I googled the problem and tried every related solution

files architecture:

-root
   |_graphql-service
   |    |_main
   |       |_java
   |       |  |_com.example.graphql
   |       |      |_Author
   |       |      |_Book
   |       |      |_GraphQLController
   |       |_resources
   |              |_graphql
   |              | |_schema.graphqls
   |              |
   |              |_application.properties
   |        
   |_service-main
           |_main
              |_java
                 |_com.example.service.main
                       |_ServiceMain
                       |_RestController

schema.graphqls

type Query {
    bookById(id: ID): Book
}

type Book {
    id: ID
    name: String
    pageCount: Int
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}

ServiceMain.java

@SpringBootApplication
public class ServiceMain {
    public static void main(String[] args) {
        SpringApplication.run(ServiceMain.class, args);
    }
}

GraphQLController.java

@Controller
public class GraphQLController {

    private static final Logger LOG = LoggerFactory.getLogger(GraphQLController.class);

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

    @SchemaMapping
    public Author author(Book book){
        LOG.info("Invoking author");
        return Author.getById(book.getAuthorId());
    }
}

Book.java

public class Book {

    private String id;
    private String name;
    private int pageCount;
    private String authorId;
    private static final Logger LOG = LoggerFactory.getLogger(Book.class);

    public Book(String id, String name, int pageCount, String authorId){
        LOG.info("Initializing Book Object");
        this.id = id;
        this.name = name;
        this.pageCount = pageCount;
        this.authorId = authorId;
    }

    private static List<Book> books = Arrays.asList(
            new Book("0", "name-0", 345, "0"),
            new Book("1", "name-1", 543, "1"),
            new Book("2", "name-2", 123, "2"),
            new Book("3", "name-3", 678, "3")
    );

    public static Book getById(String id){
        LOG.info("Filtering Books By ID");
        return books.stream().filter(
                book -> book.getId().equals(id)
        ).findFirst().orElse(null);
    }

    public String getId(){
        LOG.info("Getting Book ID");
        return id;
    }
    public String getAuthorId(){
        LOG.info("Getting Author ID");
        return authorId;
    }
}

Author.java

public class Author {

    private String id;
    private String firstName;
    private String lastName;

    private static final Logger LOG = LoggerFactory.getLogger(Author.class);

    public Author(String id, String firstName, String lastName){
        LOG.info("Initializing Author Object");
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    private static List<Author> authors = Arrays.asList(
            new Author("0", "name-0", "last-name-0"),
            new Author("1", "name-1", "last-name-1"),
            new Author("2", "name-2", "last-name-2"),
            new Author("4", "name-3", "last-name-3")
    );

    public static Author getById(String id){
        LOG.info("Getting Author By ID");
        return authors.stream().filter(
                author -> author.getId().equals(id)
        ).findFirst().orElse(null);
    }
    public String getId(){
        LOG.info("Getting Author ID");
        return id;
    }
}

application.properties

spring.graphql.graphiql.enabled=true
spring.graphql.graphiql.path=/graphiql

Share
Improve this question

1 Answer
1

Reset to default


0

I’m also having problems to get Spring Graphql working in a multi-module project. According to the docs the schema location configuration must be adjusted like this:

//application.properties
spring.graphql.schema.location=classpath*:graphql/**/

When I start the application with this configuration I get an UnsatisfiedDependencyException with the following error:

errors=['XY' type [@1:1] tried to redefine existing 'XY' type [@6887:1]

When I move the schema file to the root project the controllers in my sub project are not picked up.

Share
Improve this answer

New contributor

studix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

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