如何使用graphql-java从请求标头设置上下文

时间:2019-06-11 01:20:01

标签: java graphql graphql-java

我希望能够从从请求中收到的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;
    }

}

2 个答案:

答案 0 :(得分:1)

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

public class GraphQLContext {
    private HttpServletRequest httpServletRequest;
} 

执行GraphQL查询时,您将创建此上下文对象并将其设置为ExecutionInput。大多数网络框架都应提供一些轻松访问当前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();

}

答案 1 :(得分:0)

您可以注入(或自动装配)自定义的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
    }
}