【问题标题】:GraphQL Java: Buildschema with injected beanGraphQL Java:使用注入的 bean 构建模式
【发布时间】:2018-05-21 09:45:33
【问题描述】:

我在我的项目中使用 Wildfly 和 graphql-java。我在教程中设置了像they 这样的GraphQL Endpoint,它与虚拟数据完美配合。但是我无法注入位于 Bean 内的存储库以连接到我的数据库。注入的变量始终为空。

有人可以帮助我并告诉我我做错了什么吗?

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();
    }

【问题讨论】:

    标签: java javabeans graphql wildfly-8


    【解决方案1】:
    • 从“countRepository”和“buildSchema”中移除静态修饰符;
    • 不要在构造函数中调用 buildSchema,因为还不能进行注入,但要覆盖 servlet 的 init() 方法;
    • 如果您需要将存储库设为单例,请使用 @Singleton 声明它。

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      相关资源
      最近更新 更多