简体   繁体   中英

Spring-boot not creating graphql endpoint

I want to integrate Graphql in my project. 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?

My project is a Maven project with following dependencies:

<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...)

And my graphqls file (in resources folder):

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)

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.

The only way I made the graphql endpoint appearing is by creating a schema programmatically (which I want to avoid):

@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?

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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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