Search in sources :

Example 6 with RequestOutput

use of org.springframework.graphql.RequestOutput in project spring-graphql by spring-projects.

the class SchemaMappingPrincipalMethodArgumentResolverTests method executeAsync.

private Mono<RequestOutput> executeAsync(String schema, String document, Function<Context, Context> contextWriter) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.registerBean(GreetingController.class, () -> greetingController);
    context.refresh();
    ExecutionGraphQlService graphQlService = GraphQlSetup.schemaContent(schema).runtimeWiringForAnnotatedControllers(context).toGraphQlService();
    return Mono.delay(Duration.ofMillis(10)).flatMap(aLong -> graphQlService.execute(TestRequestInput.forDocument(document))).contextWrite(contextWriter);
}
Also used : StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionGraphQlService(org.springframework.graphql.execution.ExecutionGraphQlService) GraphQlSetup(org.springframework.graphql.GraphQlSetup) SecurityContextThreadLocalAccessor(org.springframework.graphql.execution.SecurityContextThreadLocalAccessor) Controller(org.springframework.stereotype.Controller) Function(java.util.function.Function) Nested(org.junit.jupiter.api.Nested) ReactorContextManager(org.springframework.graphql.execution.ReactorContextManager) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) Duration(java.time.Duration) MethodParameter(org.springframework.core.MethodParameter) SubscriptionMapping(org.springframework.graphql.data.method.annotation.SubscriptionMapping) Nullable(org.springframework.lang.Nullable) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Method(java.lang.reflect.Method) TestRequestInput(org.springframework.graphql.TestRequestInput) ValueSource(org.junit.jupiter.params.provider.ValueSource) GraphQlResponse(org.springframework.graphql.GraphQlResponse) ClassUtils(org.springframework.util.ClassUtils) QueryMapping(org.springframework.graphql.data.method.annotation.QueryMapping) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Context(reactor.util.context.Context) Mono(reactor.core.publisher.Mono) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Principal(java.security.Principal) Authentication(org.springframework.security.core.Authentication) RequestOutput(org.springframework.graphql.RequestOutput) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ExecutionGraphQlService(org.springframework.graphql.execution.ExecutionGraphQlService)

Example 7 with RequestOutput

use of org.springframework.graphql.RequestOutput in project spring-graphql by spring-projects.

the class BatchLoadingTests method batchLoader.

@Test
void batchLoader() {
    String document = "{ " + "  booksByCriteria(criteria: {author:\"Orwell\"}) { " + "    author {" + "      firstName, " + "      lastName " + "    }" + "  }" + "}";
    this.registry.forTypePair(Long.class, Author.class).registerBatchLoader((ids, env) -> Flux.fromIterable(ids).map(BookSource::getAuthor));
    GraphQlService service = GraphQlSetup.schemaResource(BookSource.schema).queryFetcher("booksByCriteria", env -> {
        Map<String, Object> criteria = env.getArgument("criteria");
        String authorName = (String) criteria.get("author");
        return BookSource.findBooksByAuthor(authorName).stream().map(book -> new Book(book.getId(), book.getName(), book.getAuthorId())).collect(Collectors.toList());
    }).dataFetcher("Book", "author", env -> {
        Book book = env.getSource();
        DataLoader<Long, Author> dataLoader = env.getDataLoader(Author.class.getName());
        return dataLoader.load(book.getAuthorId());
    }).dataLoaders(this.registry).toGraphQlService();
    Mono<RequestOutput> resultMono = service.execute(TestRequestInput.forDocument(document));
    List<Book> books = GraphQlResponse.from(resultMono).toList("booksByCriteria", Book.class);
    assertThat(books).hasSize(2);
    Author author = books.get(0).getAuthor();
    assertThat(author).isNotNull();
    assertThat(author.getFirstName()).isEqualTo("George");
    assertThat(author.getLastName()).isEqualTo("Orwell");
}
Also used : GraphQlResponse(org.springframework.graphql.GraphQlResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) DataLoader(org.dataloader.DataLoader) Collectors(java.util.stream.Collectors) Book(org.springframework.graphql.Book) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Author(org.springframework.graphql.Author) List(java.util.List) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) RequestOutput(org.springframework.graphql.RequestOutput) TestRequestInput(org.springframework.graphql.TestRequestInput) RequestOutput(org.springframework.graphql.RequestOutput) Book(org.springframework.graphql.Book) Author(org.springframework.graphql.Author) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) Test(org.junit.jupiter.api.Test)

Example 8 with RequestOutput

use of org.springframework.graphql.RequestOutput in project spring-graphql by spring-projects.

the class GraphQlClientTestSupport method setMockResponse.

private void setMockResponse(Consumer<ExecutionResultImpl.Builder> consumer) {
    ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder();
    consumer.accept(builder);
    ExecutionInput executionInput = ExecutionInput.newExecutionInput("{}").build();
    ExecutionResult result = builder.build();
    when(this.transport.execute(this.requestCaptor.capture())).thenReturn(Mono.just(new RequestOutput(executionInput, result)));
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) ExecutionResultImpl(graphql.ExecutionResultImpl) ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput)

Example 9 with RequestOutput

use of org.springframework.graphql.RequestOutput in project spring-graphql by spring-projects.

the class ExecutionGraphQlService method execute.

@Override
public final Mono<RequestOutput> execute(RequestInput requestInput) {
    return Mono.deferContextual((contextView) -> {
        if (!this.isDefaultExecutionIdProvider && requestInput.getExecutionId() == null) {
            requestInput.configureExecutionInput(RESET_EXECUTION_ID_CONFIGURER);
        }
        ExecutionInput executionInput = requestInput.toExecutionInput();
        ReactorContextManager.setReactorContext(contextView, executionInput);
        ExecutionInput updatedExecutionInput = registerDataLoaders(executionInput);
        return Mono.fromFuture(this.graphQlSource.graphQl().executeAsync(updatedExecutionInput)).map(result -> new RequestOutput(updatedExecutionInput, result));
    });
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) ExecutionInput(graphql.ExecutionInput)

Example 10 with RequestOutput

use of org.springframework.graphql.RequestOutput in project spring-graphql by spring-projects.

the class GraphQlTesterTestSupport method setMockResponse.

private void setMockResponse(Consumer<ExecutionResultImpl.Builder> consumer) {
    ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder();
    consumer.accept(builder);
    ExecutionInput executionInput = ExecutionInput.newExecutionInput("{}").build();
    ExecutionResult result = builder.build();
    given(this.graphQlService.execute(this.inputCaptor.capture())).willReturn(Mono.just(new RequestOutput(executionInput, result)));
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) ExecutionResultImpl(graphql.ExecutionResultImpl) ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput)

Aggregations

RequestOutput (org.springframework.graphql.RequestOutput)12 Test (org.junit.jupiter.api.Test)9 Mono (reactor.core.publisher.Mono)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Author (org.springframework.graphql.Author)4 GraphQlResponse (org.springframework.graphql.GraphQlResponse)4 TestRequestInput (org.springframework.graphql.TestRequestInput)4 ExecutionInput (graphql.ExecutionInput)3 Map (java.util.Map)3 GraphQlSetup (org.springframework.graphql.GraphQlSetup)3 Flux (reactor.core.publisher.Flux)3 ExecutionResult (graphql.ExecutionResult)2 ExecutionResultImpl (graphql.ExecutionResultImpl)2 Duration (java.time.Duration)2 List (java.util.List)2 Locale (java.util.Locale)2 Book (org.springframework.graphql.Book)2 GraphQlService (org.springframework.graphql.GraphQlService)2 QueryMapping (org.springframework.graphql.data.method.annotation.QueryMapping)2 SubscriptionMapping (org.springframework.graphql.data.method.annotation.SubscriptionMapping)2