ThreadLocal context propagation for DataFetcher's

The WebMvc starter now supports propagation of ThreadLocal values
extracted at the level of the HTTP handler.

Closes gh-53
This commit is contained in:
Rossen Stoyanchev
2021-05-28 16:26:03 +01:00
parent ca18d03244
commit 56917d8d83
26 changed files with 757 additions and 150 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}

View File

@@ -0,0 +1,24 @@
package io.spring.sample.graphql.greeting;
import graphql.schema.idl.RuntimeWiring;
import org.springframework.graphql.boot.RuntimeWiringCustomizer;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
@Component
public class GreetingDataWiring implements RuntimeWiringCustomizer {
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", typeWiring ->
typeWiring.dataFetcher("greeting", env -> {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
}));
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql.greeting;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.stereotype.Component;
/**
* Servlet Filter that adds a Servlet request attribute.
*/
@Component
public class RequestAttributeFilter implements Filter {
public static final String NAME_ATTRIBUTE = RequestAttributeFilter.class.getName() + ".name";
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setAttribute(NAME_ATTRIBUTE, "007");
chain.doFilter(request, response);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql.greeting;
import java.util.Collections;
import java.util.Map;
import org.springframework.graphql.execution.ThreadLocalAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
/**
* {@link ThreadLocalAccessor} to expose a thread-bound RequestAttributes object
* to data fetchers in Spring GraphQL.
*/
@Component
public class RequestAttributesAccessor implements ThreadLocalAccessor {
private static final String ATTRIBUTES_KEY =
RequestAttributesAccessor.class.getName() + ".requestAttributes";
@Override
public void extractValues(Map<String, Object> container) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes != null) {
container.put(ATTRIBUTES_KEY, attributes);
}
}
@Override
public void restoreValues(Map<String, Object> values) {
RequestAttributes attributes = (RequestAttributes) values.get(ATTRIBUTES_KEY);
if (attributes != null) {
RequestContextHolder.setRequestAttributes(attributes);
}
}
@Override
public void resetValues(Map<String, Object> values) {
if (values.get(ATTRIBUTES_KEY) != null) {
RequestContextHolder.resetRequestAttributes();
}
}
}

View File

@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package io.spring.sample.graphql.greeting;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package io.spring.sample.graphql;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package io.spring.sample.graphql.project;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package io.spring.sample.graphql.repository;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@@ -1,4 +1,5 @@
type Query {
greeting: String
artifactRepositories : [ArtifactRepository]
artifactRepository(id : ID!) : ArtifactRepository
project(slug: ID!): Project