【问题标题】:graphql-java-servlet's removal of SimpleGraphQLServletgraphql-java-servlet 对 SimpleGraphQLServlet 的移除
【发布时间】:2019-03-09 04:32:33
【问题描述】:

在旧版本的 graphql-java-servlet 中,我曾经扩展 SimpleGraphQLServlet,然后覆盖 GraphQLContext createContext( Optional request, Optional response ) 以将 cookie 添加到响应中。我还会覆盖 GraphQLErrorHandler getGraphQLErrorHandler() 来进行一些自定义错误处理。

我现在正在尝试将版本大幅提升到 graphql-java-servlet 6.x。 从 graphql-java-servlet 6.x 开始,SimpleGraphQLServlet 消失了。现在有一个SimpleGraphQLHttpServlet,我不能直接使用。

但不幸的是,github 文档已经过时了,即使它早已不复存在,仍然建议使用 SimpleGraphQLServlet。有一些构建器,我可以在 github 文档之外找到一些非常简单的参考资料,但它们都没有涵盖我的用例。

我不想做任何花哨的事情,但我需要能够将 cookie 添加到响应中并进行一些自定义错误处理。

如何在 graphql-java-servlet 6.x 中做到这一点?我似乎无法找到任何明确的答案。

【问题讨论】:

    标签: java graphql graphql-java


    【解决方案1】:

    GraphQLServletListenerproject docs中也有描述,虽然有点不对(代码中没有OperationCallback)。

    无论如何,这是一段工作代码(使用com.graphql-java-kickstart:graphql-java-servlet:6.1.4):

        GraphQLSchema schema = getSchema();
        List<GraphQLServletListener> listeners = new ArrayList<GraphQLServletListener>();
        GraphQLServletListener lstnr = new GraphQLServletListener(){
    
            public RequestCallback onRequest(HttpServletRequest request, HttpServletResponse response) {
                System.out.println("onRequest:" + request.getRequestURI());
                //TODO cookies here
                response.addCookie(new Cookie("sample","test"));
    
                return new RequestCallback() {
    
                    @Override
                    public void onSuccess(HttpServletRequest request, HttpServletResponse response) {
                        System.out.println("onSuccess:" + request.getRequestURI());
                    }
    
                    @Override
                    public void onError(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
                        //TODO add some error handling here
                        System.out.println("onError:" + request.getRequestURI());
                    }
    
                    @Override
                    public void onFinally(HttpServletRequest request, HttpServletResponse response) {
                        System.out.println("onFinally:" + request.getRequestURI());
                    }
    
                };
            }
        };
        listeners.add(lstnr);
    
        SimpleGraphQLHttpServlet servlet = SimpleGraphQLHttpServlet.newBuilder(schema)
            .withListeners(listeners)
            .build();
    

    【讨论】:

    • 感谢@Eugene。这让我在那儿的路的一部分,但有一个问题。我需要能够在我的解析器中创建 cookie。所以当我提到我重写了 createContext 时,我的意思是: 1. 我重写了 createContext 以返回一个包含 addCookie 方法的包装器。 2. 在我的解析器中,我从数据获取环境中获取了上下文并添加了 cookie。它们似乎也改变了 GraphQLContext 类的工作方式。所以我不确定这是否仍然可行?有什么想法吗?
    • 看起来也许我可以通过自定义 GraphQLContextBuilder 来进行覆盖,但是我如何将它绑定到这个 servlet?
    • @MarkNenadov 你在说什么解析器?此外,我认为你在 graphql-servlet 上的挣扎比它对你的帮助更大。也许您应该为 graphql 实现自己的 servlet 并摆脱 graphql-servlet ?
    • 我说的解析器是一个自定义解析器,用于处理graphql查询。我不确定您在这里提出的建议有什么帮助——为 graphql 实现我自己的 servlet 仍然会让我没有我需要的功能在我的解析器中。不过,我可能会遗漏一些东西。也就是说,我与一个 graphql-java-servlet 开发人员取得了联系,似乎在他们的重构中,他们无意中从上下文构建器中删除了响应对象。所以我的答案是等待下一个版本,它将包含它并且应该允许我做我需要做的事情。
    • @MarkNenadov 您能否指导如何使用 GraphQLErrorHandler 在 SimpleGraphqlHttpServlet 中包装错误。目前,我在响应中只收到 InternalServer 错误,并且我坚持了几天。
    猜你喜欢
    • 2019-10-22
    • 2019-07-14
    • 1970-01-01
    • 2018-06-11
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2011-06-29
    相关资源
    最近更新 更多