Search in sources :

Example 6 with GraphQLContext

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();
}
Also used : Optional(java.util.Optional) Book(org.springframework.graphql.Book) GraphQLContext(graphql.GraphQLContext) Test(org.junit.jupiter.api.Test)

Example 7 with GraphQLContext

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;
}
Also used : ExecutionInput(graphql.ExecutionInput) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) List(java.util.List) GraphQL(graphql.GraphQL) GraphQLContext(graphql.GraphQLContext) BiFunction(java.util.function.BiFunction) GraphQlService(org.springframework.graphql.GraphQlService) Mono(reactor.core.publisher.Mono) RequestInput(org.springframework.graphql.RequestInput) ExecutionIdProvider(graphql.execution.ExecutionIdProvider) RequestOutput(org.springframework.graphql.RequestOutput) ArrayList(java.util.ArrayList) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) GraphQLContext(graphql.GraphQLContext)

Example 8 with GraphQLContext

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);
}
Also used : ContextValue(org.springframework.graphql.data.method.annotation.ContextValue) GraphQLContext(graphql.GraphQLContext) Nullable(org.springframework.lang.Nullable)

Example 9 with GraphQLContext

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");
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) GraphQLContext(graphql.GraphQLContext) Author(org.springframework.graphql.Author) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestInput(org.springframework.graphql.RequestInput) TestRequestInput(org.springframework.graphql.TestRequestInput) Test(org.junit.jupiter.api.Test)

Example 10 with GraphQLContext

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");
}
Also used : DataLoaderRegistry(org.dataloader.DataLoaderRegistry) ContextView(reactor.util.context.ContextView) StatisticsCollector(org.dataloader.stats.StatisticsCollector) Context(reactor.util.context.Context) BookSource(org.springframework.graphql.BookSource) NoOpStatisticsCollector(org.dataloader.stats.NoOpStatisticsCollector) Mono(reactor.core.publisher.Mono) DataLoader(org.dataloader.DataLoader) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Book(org.springframework.graphql.Book) ExecutionInput(graphql.ExecutionInput) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) GraphQLContext(graphql.GraphQLContext) Map(java.util.Map) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) DataLoader(org.dataloader.DataLoader) Book(org.springframework.graphql.Book) GraphQLContext(graphql.GraphQLContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

GraphQLContext (graphql.GraphQLContext)11 Test (org.junit.jupiter.api.Test)6 ExecutionInput (graphql.ExecutionInput)4 Book (org.springframework.graphql.Book)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)3 Optional (java.util.Optional)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 GraphQL (graphql.GraphQL)2 DataFetcher (graphql.schema.DataFetcher)2 Map (java.util.Map)2 Function (java.util.function.Function)2 AssertionsForInterfaceTypes.assertThat (org.assertj.core.api.AssertionsForInterfaceTypes.assertThat)2 DataLoader (org.dataloader.DataLoader)2 NoOpStatisticsCollector (org.dataloader.stats.NoOpStatisticsCollector)2 StatisticsCollector (org.dataloader.stats.StatisticsCollector)2 BookSource (org.springframework.graphql.BookSource)2 RequestInput (org.springframework.graphql.RequestInput)2