简体   繁体   English

GraphQL 在突变函数上访问 HttpRequest

[英]GraphQL access to HttpRequest on a mutation function

Hi I'm new to GraphQL.嗨,我是 GraphQL 的新手。

I want to migrate an existing endpoint to a GraphQL endpoint (Java).我想将现有端点迁移到 GraphQL 端点 (Java)。 In oder to proceed with the application logic of the endpoint, I need the HttpServletRequest object.为了继续处理端点的应用程序逻辑,我需要HttpServletRequest对象。

How can I have access to HttpServletRequest object inside a mutation function?如何访问变异函数内的HttpServletRequest对象? I try to pass it as a parameter but I have to declare a HttpServetRequest Type.我尝试将它作为参数传递,但我必须声明一个HttpServetRequest类型。

Is there a way of having access to the above object inside a mutation?有没有办法在突变中访问上述对象? Is there a way of declaring Types of external libraries?有没有办法声明外部库的类型?

Thank you.谢谢你。

This is normally done using the shared context.这通常是使用共享上下文完成的。 When executing an operation, you can pass an arbitrary object that will be accessible to all the Datafetcher s (resolvers), via DataFetchingEnvironment#getContext .执行操作时,您可以通过DataFetchingEnvironment#getContext传递所有Datafetcher (解析器)都可以访问的任意对象。

Eg例如

graphQL.execute(ExecutionInput.newExecutionInput()
    .query(operation)
    .context(servletRequest) //this is where you pass any object you need
    .build());

For older graphql-java versions:对于较旧的 graphql-java 版本:

graphQL.execute(query, context);

and later in the DataFetcher of your query/mutation:稍后在您的查询/变异的DataFetcher中:

HttpServletRequest request = environment.getContext();

This is also the common way to perform authorization: you pass in the user object via the context.这也是执行授权的常用方法:通过上下文传入用户对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM