use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class ContextValueMethodArgumentResolverTests method resolveMissing.
@Test
@SuppressWarnings({ "unchecked", "ConstantConditions" })
void resolveMissing() {
GraphQLContext context = GraphQLContext.newContext().build();
// Required
assertThatIllegalStateException().isThrownBy(() -> resolveValue(context, context, 0)).withMessage("Missing required context value for method 'handle' parameter 0");
// Not required
assertThat(resolveValue(context, context, 2)).isNull();
// Optional
Optional<Book> actual = (Optional<Book>) resolveValue(context, context, 3);
assertThat(actual.isPresent()).isFalse();
}
use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class ExecutionGraphQlService method registerDataLoaders.
private ExecutionInput registerDataLoaders(ExecutionInput executionInput) {
if (!this.dataLoaderRegistrars.isEmpty()) {
GraphQLContext graphQLContext = executionInput.getGraphQLContext();
DataLoaderRegistry previousRegistry = executionInput.getDataLoaderRegistry();
DataLoaderRegistry newRegistry = DataLoaderRegistry.newRegistry().registerAll(previousRegistry).build();
this.dataLoaderRegistrars.forEach(registrar -> registrar.registerDataLoaders(newRegistry, graphQLContext));
executionInput = executionInput.transform(builder -> builder.dataLoaderRegistry(newRegistry));
}
return executionInput;
}
use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class ContextValueMethodArgumentResolver method resolveContextValue.
@Nullable
static Object resolveContextValue(MethodParameter parameter, @Nullable Object localContext, GraphQLContext graphQlContext) {
ContextValue annotation = parameter.getParameterAnnotation(ContextValue.class);
Assert.state(annotation != null, "Expected @ContextValue annotation");
String name = getValueName(parameter, annotation);
Class<?> parameterType = parameter.getParameterType();
Object value = null;
if (localContext instanceof GraphQLContext) {
value = ((GraphQLContext) localContext).get(name);
}
if (value != null) {
return wrapAsOptionalIfNecessary(value, parameterType);
}
value = graphQlContext.get(name);
if (value == null && annotation.required() && !parameterType.equals(Optional.class)) {
throw new IllegalStateException("Missing required context value for " + parameter);
}
return wrapAsOptionalIfNecessary(value, parameterType);
}
use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class SchemaMappingInvocationTests method queryWithArgumentViaDataFetchingEnvironment.
@Test
void queryWithArgumentViaDataFetchingEnvironment() {
String document = "{ " + " authorById(id:\"101\") { " + " id" + " firstName" + " lastName" + " }" + "}";
AtomicReference<GraphQLContext> contextRef = new AtomicReference<>();
RequestInput requestInput = TestRequestInput.forDocument(document);
requestInput.configureExecutionInput((executionInput, builder) -> {
contextRef.set(executionInput.getGraphQLContext());
return executionInput;
});
Mono<RequestOutput> resultMono = graphQlService().execute(requestInput);
Author author = GraphQlResponse.from(resultMono).toEntity("authorById", Author.class);
assertThat(author.getId()).isEqualTo(101);
assertThat(author.getFirstName()).isEqualTo("George");
assertThat(author.getLastName()).isEqualTo("Orwell");
assertThat(contextRef.get().<String>get("key")).isEqualTo("value");
}
use of graphql.GraphQLContext in project spring-graphql by spring-projects.
the class DefaultBatchLoaderRegistryTests method batchLoader.
@Test
void batchLoader() throws Exception {
AtomicReference<String> valueRef = new AtomicReference<>();
this.batchLoaderRegistry.forTypePair(Long.class, Book.class).withOptions(// DataLoader invoked immediately
options -> options.setBatchingEnabled(false)).registerBatchLoader((ids, environment) -> Flux.deferContextual(contextView -> {
valueRef.set(contextView.get("key"));
return Flux.fromIterable(ids).map(BookSource::getBook);
}));
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");
}
Aggregations