1

I'm working on a GraphQL java API project and i'm having problems with the Query not finding the methods in the Query.java (QueryResolver)

I can't see an error on this code but aparently it has one ':D

PS: I use java open JDK 11

the error: (full stacktrace on https://pastebin.com/cpVRpdsj)

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'graphQLServlet' threw exception; nested exception is com.coxautodev.graphql.tools.FieldResolverError: No method or field found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:

  com.example.GraphQLRProject.core.Query.findAllPessoas()
  com.example.GraphQLRProject.core.Query.getFindAllPessoas()
  com.example.GraphQLRProject.core.Query.findAllPessoas
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    ... 59 common frames omitted

my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>GraphQLRProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>GraphQLRProject</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <!--<kotlin.version>1.3.10</kotlin.version> -->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>   
    
        <!-- 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>    
        -->

    
        <!-- 
        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>6.0.1</version>
        </dependency>
         -->
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-tools</artifactId>
            <version>5.2.4</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java</artifactId>
            <version>11.0</version>
        </dependency>

        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-servlet</artifactId>
            <version>6.1.3</version>
        </dependency>
        
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.6</version>
        </dependency>   
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>           
        <!-- GraphQL end -->    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

my Query.java

package com.example.GraphQLRProject.core;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.example.GraphQLRProject.model.Pessoa;
import com.example.GraphQLRProject.repository.PessoaRepository;

@Component
public class Query implements GraphQLQueryResolver {

    @Autowired
    PessoaRepository pessoaRepository;

    List<Pessoa> findAllPessoas() {
        return pessoaRepository.findAll();
    }
}

my main application

package com.example.GraphQLRProject;

import java.util.TimeZone;

import javax.annotation.PostConstruct;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.coxautodev.graphql.tools.SchemaParser;
import com.example.GraphQLRProject.core.Mutation;
import com.example.GraphQLRProject.core.Query;
import com.example.GraphQLRProject.util.DateScalar;

import graphql.schema.GraphQLSchema;
import graphql.servlet.SimpleGraphQLHttpServlet;

@ComponentScan(basePackageClasses = GraphQlrProjectApplication.class)
@SpringBootApplication(scanBasePackages = "com.example")
@EntityScan("com.example")
@EnableJpaRepositories("com.example")
@SuppressWarnings({"rawtypes", "unchecked"})
public class GraphQlrProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(GraphQlrProjectApplication.class, args);
    }

    @PostConstruct
    void started() {
        TimeZone.setDefault(TimeZone.getTimeZone("America/Recife"));
    }
    
    @Bean
    public ServletRegistrationBean graphQLServlet() {
        return new ServletRegistrationBean(SimpleGraphQLHttpServlet.newBuilder(buildSchema()).build(), "/graphql");
    }

    private static GraphQLSchema buildSchema() {
        return SchemaParser
                .newParser()
                .file("schema.graphqls")
                .resolvers(new Query(), new Mutation())
                .scalars(DateScalar.DATE)
                .build()
                .makeExecutableSchema();
    }
}

and my schema.graphqls

schema {
    query: Query
    mutation: Mutation
}

type Query {
    findAllPessoas: [Pessoa]
}

type Mutation {
    savePessoa(novo: PessoaInput): Pessoa
}

type Pessoa {
    id: ID!
    nome: String!
    cpf: String!
    email: String!
}

input PessoaInput{
    nome: String!
    cpf: String!
    email: String!
}

1 Answer 1

4

The problem was that the method was not public ...

big facepalm for me ':D

Sign up to request clarification or add additional context in comments.

1 Comment

..... saved my afternoon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.