简体   繁体   English

Spring-boot 未创建 graphql 端点

[英]Spring-boot not creating graphql endpoint

I want to integrate Graphql in my project.我想在我的项目中集成 Graphql。 I started with a new Spring boot project following (several) tutorial but none of them are working: the graphql endpoint does not appear, it seems that the graphqls file is not detected?我在(几个)教程之后从一个新的 Spring 启动项目开始,但它们都没有工作:graphql 端点没有出现,似乎未检测到 graphqls 文件?

My project is a Maven project with following dependencies:我的项目是一个 Maven 项目,具有以下依赖项:

<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>5.10.0</version>
</dependency>
<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-java-tools</artifactId>
    <version>5.6.1</version>
</dependency>
<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphiql-spring-boot-starter</artifactId>
    <version>5.10.0</version>
    <scope>runtime</scope>
</dependency>

To keep the post brief, I tried to put only the relevant dependencies avoiding the others (Lombok, jpa, etc...)为了保持帖子简短,我尝试只放置相关依赖项,避免其他依赖项(Lombok、jpa 等......)

And my graphqls file (in resources folder):还有我的 graphqls 文件(在资源文件夹中):

type Vehicle {
    id: ID!,
    type: String,
    modelCode: String,
    brandName: String,
    launchDate: String
}
type Query {
    vehicles(count: Int):[Vehicle]
    vehicle(id: ID):Vehicle
}
type Mutation {
    createVehicle(type: String!, modelCode: String!, brandName: String, launchDate: String):Vehicle
}

According to the documentation, the graphql endpoint should be created since I have a graphql-spring-boot-starter dependency and a schema which will be created automatically with my vehicle.graphqls file (and the graphql-java-tools dependency)根据文档,应该创建 graphql 端点,因为我有一个 graphql-spring-boot-starter 依赖项和一个模式,它将使用我的 vehicle.graphqls 文件(以及 graphql-java-tools 依赖项)自动创建

The servlet becomes accessible at /graphql if graphql-spring-boot-starter added as a dependency to a boot application and a GraphQLSchema bean is present in the application [...] A GraphQL schema can also be automatically created when a supported graphql-java schema library is found on the classpath.如果将 graphql-spring-boot-starter 添加为启动应用程序的依赖项并且应用程序中存在 GraphQLSchema bean,则该 servlet 可以在 /graphql 访问 [...] 当支持的 graphql-在类路径中可以找到 java 模式库。

The only way I made the graphql endpoint appearing is by creating a schema programmatically (which I want to avoid):我使 graphql 端点出现的唯一方法是以编程方式创建模式(我想避免):

@Bean
    GraphQLSchema schema() {
    return GraphQLSchema.newSchema()
            .query(GraphQLObjectType.newObject()
                    .name("query")
                    .field(field -> field
                            .name("test")
                            .type(Scalars.GraphQLString)
                            .dataFetcher(environment -> "response")
                            )
                    .build())
            .build();
}

I guess that I missed something but after one day of attempts, I am now stuck, I don't see what can be my mistake.我想我错过了一些东西,但经过一天的尝试,我现在卡住了,我看不出我的错误是什么。

Are you exposing GraphQLQueryResolver as a @Bean so that the autoconfig knows to search for your schema files?您是否将GraphQLQueryResolver公开为@Bean以便自动配置知道搜索您的模式文件?

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GraphQLConfiguration {

    @Bean
    QueryResolver queryResolver() {
        return new QueryResolver();
    }

    @Bean
    MutationResolver mutationMutation() {
        return new MutationResolver();
    }

}

Documentation for author's quote 作者报价文件

The servlet becomes accessible at /graphql if graphql-spring-boot-starter added as a dependency to a boot application and a GraphQLSchema bean is present in the application.如果将 graphql-spring-boot-starter 作为依赖项添加到启动应用程序并且应用程序中存在 GraphQLSchema bean,则可以在 /graphql 访问 servlet。 Check out the simple example for the bare minimum required.查看简单示例,了解所需的最低要求。

A GraphQL schema can also be automatically created when a supported graphql-java schema library is found on the classpath.当在类路径上找到支持的 graphql-java 模式库时,也可以自动创建 GraphQL 模式。

Following THAT thread of documentation will eventually lead you the GraphQL Java Tools project documentation which explains how to generate the necessary GraphQLSchema object from your .graphqls definitions.遵循该文档线程最终将引导您阅读 GraphQL Java 工具项目文档,该文档解释了如何从您的.graphqls定义中生成必要的GraphQLSchema object。 Lastly, we have to map those definitions into Java somehow, and this is Spring Boot so @Bean your root resolver(s) so it has an implementation to work with and you should be good to go. Lastly, we have to map those definitions into Java somehow, and this is Spring Boot so @Bean your root resolver(s) so it has an implementation to work with and you should be good to go.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM