use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class DefaultBatchLoaderRegistryTests method mappedBatchLoader.
@Test
void mappedBatchLoader() throws Exception {
AtomicReference<String> valueRef = new AtomicReference<>();
this.batchLoaderRegistry.forTypePair(Long.class, Book.class).withOptions(// DataLoader invoked immediately
options -> options.setBatchingEnabled(false)).registerMappedBatchLoader((ids, environment) -> Mono.deferContextual(contextView -> {
valueRef.set(contextView.get("key"));
return Flux.fromIterable(ids).map(BookSource::getBook).collectMap(Book::getId, Function.identity());
}));
GraphQLContext graphQLContext = initGraphQLContext(Context.of("key", "value"));
this.batchLoaderRegistry.registerDataLoaders(this.dataLoaderRegistry, graphQLContext);
Map<String, DataLoader<?, ?>> map = this.dataLoaderRegistry.getDataLoadersMap();
assertThat(map).hasSize(1).containsKey(Book.class.getName());
// Invoke DataLoader to check the context
((DataLoader<Long, Book>) map.get(Book.class.getName())).load(1L).get();
assertThat(valueRef.get()).isEqualTo("value");
}
use of graphql.GraphQLContext in project micronaut-graphql by micronaut-projects.
the class LoginDataFetcher method get.
@Override
public LoginPayload get(DataFetchingEnvironment environment) throws Exception {
GraphQLContext graphQLContext = environment.getContext();
if (LOGIN_RATE_LIMIT_REMAINING <= 0) {
addRateLimitHeaders(graphQLContext);
resetRateLimit();
return LoginPayload.ofError("Rate Limit Exceeded");
}
HttpRequest httpRequest = graphQLContext.get("httpRequest");
MutableHttpResponse<String> httpResponse = graphQLContext.get("httpResponse");
String username = environment.getArgument("username");
String password = environment.getArgument("password");
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
LOGIN_RATE_LIMIT_REMAINING--;
Flux<AuthenticationResponse> authenticationResponseFlowable = Flux.from(authenticator.authenticate(httpRequest, usernamePasswordCredentials));
return authenticationResponseFlowable.map(authenticationResponse -> {
addRateLimitHeaders(graphQLContext);
if (authenticationResponse.isAuthenticated()) {
eventPublisher.publishEvent(new LoginSuccessfulEvent(authenticationResponse));
Optional<Cookie> jwtCookie = accessTokenCookie(Authentication.build(username), httpRequest);
jwtCookie.ifPresent(httpResponse::cookie);
User user = userRepository.findByUsername(username).orElse(null);
return LoginPayload.ofUser(user);
} else {
eventPublisher.publishEvent(new LoginFailedEvent(authenticationResponse));
return LoginPayload.ofError(authenticationResponse.getMessage().orElse(null));
}
}).blockFirst();
}
use of graphql.GraphQLContext in project micronaut-graphql by micronaut-projects.
the class RequestResponseCustomizer method customize.
@Override
public Publisher<ExecutionInput> customize(ExecutionInput executionInput, HttpRequest httpRequest, @Nullable MutableHttpResponse<String> httpResponse) {
GraphQLContext graphQLContext = (GraphQLContext) executionInput.getContext();
graphQLContext.put("httpRequest", httpRequest);
graphQLContext.put("httpResponse", httpResponse);
return Publishers.just(executionInput);
}
use of graphql.GraphQLContext in project graphql-java by graphql-java.
the class ConcernsExamples method contextHelper.
private void contextHelper() {
//
// this could be code that authorises the user in some way and sets up enough context
// that can be used later inside data fetchers allowing them
// to do their job
//
UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser());
ExecutionInput executionInput = ExecutionInput.newExecutionInput().graphQLContext(context -> context.put("userContext", contextForUser)).build();
ExecutionResult executionResult = graphQL.execute(executionInput);
// ...
//
// later you are able to use this context object when a data fetcher is invoked
//
DataFetcher dataFetcher = new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
UserContext userCtx = environment.getGraphQlContext().get("userContext");
Long businessObjId = environment.getArgument("businessObjId");
return invokeBusinessLayerMethod(userCtx, businessObjId);
}
};
}
use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class ContextValueMethodArgumentResolverTests method resolveOptional.
@Test
@SuppressWarnings({ "unchecked", "ConstantConditions", "OptionalGetWithoutIsPresent" })
void resolveOptional() {
GraphQLContext context = GraphQLContext.newContext().of("optionalBook", this.book).build();
Optional<Book> actual = (Optional<Book>) resolveValue(context, context, 3);
assertThat(actual.get()).isSameAs(this.book);
}
Aggregations