【问题标题】:How to set context from request header using graphql-java如何使用 graphql-java 从请求标头设置上下文
【发布时间】:2019-10-25 09:03:49
【问题描述】:

我希望能够从我从请求中收到的 http 请求标头中设置一个上下文变量。这将是一个 jwt 令牌,因此我可以在每个查询中识别我的用户。

package br.com.b2breservas.api;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URL;

import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;

@Component
public class GraphQLProvider   {


    @Autowired
    GraphQLDataFetchers graphQLDataFetchers;

    private GraphQL graphQL;

    @PostConstruct
    public void init() throws IOException {
        URL url = Resources.getResource("schema.graphqls");
        String sdl = Resources.toString(url, Charsets.UTF_8);
        GraphQLSchema graphQLSchema = buildSchema(sdl);
        this.graphQL = GraphQL.newGraphQL(graphQLSchema).build();
    }

    private GraphQLSchema buildSchema(String sdl) {
        TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
        RuntimeWiring runtimeWiring = buildWiring();
        SchemaGenerator schemaGenerator = new SchemaGenerator();
        return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    }

    private RuntimeWiring buildWiring() {
        return RuntimeWiring.newRuntimeWiring()
                .type(newTypeWiring("Query")
                        .dataFetcher("books", graphQLDataFetchers.getBooks()))
                .type(newTypeWiring("Query")
                        .dataFetcher("bookById", graphQLDataFetchers.getBookByIdDataFetcher()))
                .type(newTypeWiring("Book")
                        .dataFetcher("author", graphQLDataFetchers.getAuthorDataFetcher()))
                .build();
    }

    @Bean
    public GraphQL graphQL() {
        return graphQL;
    }

}

【问题讨论】:

  • 你好,你从哪里导入类(或接口?)GraphQLDataFetchers?我在互联网上找不到任何相关的课程,因为许多图书馆似乎都有它......
  • @maxxyme 是您编写代码的类。只需在任何地方创建并导入它。只需在其上使用 spring 组件装饰器,您就可以开始使用了。类似gist.github.com/cescoferraro/385e2bd40ddbd2c6afda9a7e79d858ad
  • 这很奇怪,因为使用 GraphQL Java 工具我不需要编写这样的类,也不需要编写这样的提供程序......

标签: java graphql graphql-java


【解决方案1】:

您可以创建一个内部包含 JWT 或只是 HttpServletRequest 的自定义对象:

public class GraphQLContext {
    private HttpServletRequest httpServletRequest;
} 

在执行GraphQL 查询时,您创建此上下文对象并将其设置为ExecutionInput。大多数 Web 框架应该提供一些方法来轻松访问当前的HttpServletRequest

GraphQLContext context = new GraphQLContext(httpServletRequest);
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
                .query(query)
                .context(context)
                .build();

ExecutionResult result = graphQL.execute(executionInput);

然后在数据获取器中,可以通过以下方式获取上下文:

@Override
public Object get(DataFetchingEnvironment env) throws Exception {

    GraphQLContext context = env.getContext();
    HttpServletRequest httpServletRequest = context.getHttpServletRequest();

}

【讨论】:

  • 我不明白。客户端是 Apollo 客户端,它将在 que 请求时发送 Auth 标头。我想以某种方式从 spring-boot 读取这个令牌并将其设置为上下文变量,以便我可以在 DataFetchers 上使用它。我应该把这段代码放在哪里
  • 那么现在如何接收 GraphQL http 请求呢?如果您使用的是 MVC 控制器,您可以简单地将 HttpServletRequest 参数放入您的控制器方法中..
  • 弹簧靴。 @ken chan
  • 是一样的,很可能springboot在后台使用mvc。但如果您使用 webflux ,只需将请求参数类型更改为 ServerHttpRequest
  • 我想我正在使用一个在幕后处理 http post 请求的依赖项。我可能不得不覆盖它,以便我可以将上下文注入它。是否有意义? github.com/b2breservas/java/blob/…
【解决方案2】:

您可以注入(或自动装配)您的自定义GraphQLInvocation 实例,该实例可以充当GraphQL 处理的所有请求的拦截器

import graphql.ExecutionInput
import graphql.ExecutionResult
import graphql.GraphQL
import graphql.spring.web.servlet.GraphQLInvocation
import graphql.spring.web.servlet.GraphQLInvocationData
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Component
import org.springframework.web.context.request.WebRequest
import java.util.concurrent.CompletableFuture

@Component
@Primary // <= Mark it as Primary to override the default one
class ErsanGraphQLInvocation(private val graphQL: GraphQL) : GraphQLInvocation {
    override fun invoke(invocationData: GraphQLInvocationData,
        webRequest: WebRequest): CompletableFuture<ExecutionResult> {

        val context = "Context" //Basically any class you want <=====

        val executionInput = ExecutionInput.newExecutionInput()
            .query(invocationData.query)
            .operationName(invocationData.operationName)
            .variables(invocationData.variables)
            .context(context)
            .build()
        return graphQL.executeAsync(executionInput)
    }
}

然后在您的DataFetcher 中,您可以从DataFetchingEnvironment 实例中读取上下文,例如。

fun appVersionFetcher(): DataFetcher<Boolean> {
    return DataFetcher { dataFetchingEnvironment ->
        val context = dataFetchingEnvironment.getContext<String>()
        println("Context $context")
        false
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2018-03-30
    • 2020-10-09
    • 2019-08-18
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多