SpringBoot 未公开 GraphQL

问题描述 投票:0回答:1

我有一个多模块项目,其中之一是 GraphQL 模块 可运行文件位于具有工作 RestController 的不同模块中 GraphQLController 未公开。 我尝试将 application.properties 移动到可运行模块不起作用,尝试移动 GraphQLController 也不起作用我什至将所有文件移动到可运行模块但没有任何反应。 我的依赖关系很好 我的 gradle 很好 我确信我的代码可以工作,但只能单独尝试,它确实有效,但我正在开发一个多模块项目,所以我不能单独运行它。 这里似乎有什么问题,我用谷歌搜索了这个问题并尝试了所有相关的解决方案
文件架构:

-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;
    }
}

作者.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;
    }
}

应用程序属性

spring.graphql.graphiql.enabled=true
spring.graphql.graphiql.path=/graphiql
java spring-boot graphql endpoint graphiql
1个回答
0
投票

我在让 Spring Graphql 在多模块项目中工作时也遇到问题。根据文档,架构位置配置必须像这样调整:

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

当我使用此配置启动应用程序时,我收到 UnsatisfiedDependencyException 并出现以下错误:

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

当我将架构文件移动到根项目时,子项目中的控制器不会被拾取。

© www.soinside.com 2019 - 2024. All rights reserved.