SpringBoot不暴露GraphQL

6rqinv9w  于 2023-10-16  发布在  Spring
关注(0)|答案(2)|浏览(114)

我有一个多模块项目,其中一个是GraphQL模块,Runnable文件在另一个模块中,有一个工作的RestController,GraphQLController没有暴露。我尝试将www.example.com移动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
ubbxdtey

ubbxdtey1#

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

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

当我用这个配置启动应用程序时,我得到一个UnsatisfiedDependencyException,并出现以下错误:

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

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

kcwpcxri

kcwpcxri2#

这为我修复了它:

//application.yaml
spring:
  graphql:
    schema:
      locations: [classpath*:graphql/**, classpath*:subproject/graphql/**]

相关问题