graphql servlet not started with graphql-java-tools (schema files not found?)

2.3k Views Asked by At

So according to the graphql-java-kickstart/graphql-java-tools a 'graphql' endpoint should become available when the dependency 'com.graphql-java-kickstart:graphql-spring-boot-starter' is added to the project and .graphqls schema files are scanned automatically.

I have the following dependencies:

...
        <spring-boot.version>2.3.3.RELEASE</spring-boot.version>
        <graphql.version>7.0.1</graphql.version>
        <graphql-java-tools.version>6.2.0</graphql-java-tools.version>

...

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <!-- graphql -->
        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>${graphql.version}</version>
        </dependency>

        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>${graphql.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-java-tools</artifactId>
            <version>${graphql-java-tools.version}</version>
        </dependency>

A schema definition:

in query.graphqls:
type Query {
    user(username: String!)
    users: [User]
}

in user.graphqls:
type User {
    userId: Number!
    username: String!
}

And a GraphQLQueryResolver for that:

@Component
public class UserQueryResolver implements GraphQLQueryResolver {
    private final UserRepository userRepository;

    public UserQueryResolver(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public Iterable<User> users() {
        return this.userRepository.findAll();
    }

    public User user(String username) {
        return this.userRepository.findByUsername(username).orElseThrow();
    }
}

public interface UserRepository extends JpaRepository<User, Integer> {
    Optional<User> findByUsername(String username);
}

According to the documentation:

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.

The graphql-java-tools library should automatically create a schema, under these conditions:

All GraphQLResolver and GraphQLScalar beans, along with a bean of type SchemaParserDictionary (to provide all other classes), will be used to create a GraphQLSchema. Any files on the classpath named *.graphqls will be used to provide the schema definition. See the Readme for more info.

I think I have everything it needs, but navigating to localhost:8080/graphql gives a 404. N.B.: localhost:8080/graphiql works, but it cannot load the schema. It says:

{
  "timestamp": "2020-09-15T12:22:21.748+00:00",
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/graphql"
}

What am I missing?

1

There are 1 best solutions below

0
On BEST ANSWER

Apparently the application could not find any JpaRepositories, because the SpringBootApplication starter class was located in com.package.some.app while the repositories were in com.package.some.domain.repositories. The Component scanner was only scanning components with package com.package.som.app.*