0

I'm using Wildfly and graphql-java in my project. I set up the GraphQL Endpoint like they in there tutorial did and it worked perfectly with dummy data. But i am not able to inject my Repository which is inside a Bean to connect to my database. The injected variable is always null.

Can someone help me and tell me what i am doing wrong?

GraphQLEndpoint

@WebServlet(urlPatterns = "/graphql")
public class GraphQLEndpoint extends SimpleGraphQLServlet {

    @EJB()
    private static CountRepositoryInterface countRepository;

    public GraphQLEndpoint() {
        super(buildSchema());
    }

    private static GraphQLSchema buildSchema() {
        LinkRepository linkRepository = new LinkRepository(); // Dummy Data from ArrayList
        TraceRepository traceRepository = new TraceRepository(); // Dummy Data from ArrayList

        return SchemaParser.newParser()
                .file("schema.graphqls")
                .resolvers(new Query(linkRepository, traceRepository, countRepository))
                .build()
                .makeExecutableSchema();
    }
}

CountRepositoryInterface

@Remote
public interface CountRepositoryInterface {

    void createCounter(Counter counter);

    Counter getCounterByUser(String userName);

    void updateCounter(String userName);

    void deleteAllCounter();

    List<Counter> GetAllFromCounter();
}

CountRepository

@Stateless
public class CountRepository implements CountRepositoryInterface {

    @PersistenceContext(unitName = "count")
    private EntityManager entityManager;

    public void createCounter(Counter counter) {
        entityManager.persist(counter);
    }

    public List<Counter> GetAllFromCounter() {
        Query query = entityManager.createQuery("SELECT c FROM Counter c");

        return  query.getResultList();
    }
kaaPe
  • 23
  • 4

1 Answers1

0
  • Remove static modifiers from "countRepository" and "buildSchema";
  • Don't call buildSchema in constructor, since injection could not yet take place, but override the init() method of the servlet;
  • If you need repository to be a singleton, declare it using @Singleton.
Ramiz
  • 429
  • 8
  • 10