Search in sources :

Example 6 with WebOutput

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

the class GraphQlWebSocketHandler method handleWebOutput.

@SuppressWarnings("unchecked")
private Flux<TextMessage> handleWebOutput(WebSocketSession session, String id, WebOutput output) {
    if (logger.isDebugEnabled()) {
        logger.debug("Execution result ready" + (!CollectionUtils.isEmpty(output.getErrors()) ? " with errors: " + output.getErrors() : "") + ".");
    }
    Flux<ExecutionResult> outputFlux;
    if (output.getData() instanceof Publisher) {
        // Subscription
        outputFlux = Flux.from((Publisher<ExecutionResult>) output.getData()).doOnSubscribe((subscription) -> {
            Subscription prev = getSessionInfo(session).getSubscriptions().putIfAbsent(id, subscription);
            if (prev != null) {
                throw new SubscriptionExistsException();
            }
        });
    } else {
        // Single response (query or mutation) that may contain errors
        outputFlux = Flux.just(output);
    }
    return outputFlux.map(result -> encode(GraphQlMessage.next(id, result))).concatWith(Mono.fromCallable(() -> encode(GraphQlMessage.complete(id)))).onErrorResume((ex) -> {
        if (ex instanceof SubscriptionExistsException) {
            CloseStatus status = new CloseStatus(4409, "Subscriber for " + id + " already exists");
            GraphQlStatus.closeSession(session, status);
            return Flux.empty();
        }
        String message = ex.getMessage();
        GraphQLError error = GraphqlErrorBuilder.newError().message(message).build();
        return Mono.just(encode(GraphQlMessage.error(id, error)));
    });
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebOutput(org.springframework.graphql.web.WebOutput) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocketSession(org.springframework.web.socket.WebSocketSession) CloseStatus(org.springframework.web.socket.CloseStatus) ExecutionResult(graphql.ExecutionResult) TextMessage(org.springframework.web.socket.TextMessage) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphQLError(graphql.GraphQLError) Duration(java.time.Duration) Map(java.util.Map) Schedulers(reactor.core.scheduler.Schedulers) Nullable(org.springframework.lang.Nullable) URI(java.net.URI) OutputStream(java.io.OutputStream) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) HttpHeaders(org.springframework.http.HttpHeaders) ExceptionWebSocketHandlerDecorator(org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) GraphqlErrorBuilder(graphql.GraphqlErrorBuilder) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) WebInput(org.springframework.graphql.web.WebInput) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) CollectionUtils(org.springframework.util.CollectionUtils) Subscription(org.reactivestreams.Subscription) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) SubProtocolCapable(org.springframework.web.socket.SubProtocolCapable) InputStream(java.io.InputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) Assert(org.springframework.util.Assert) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult) Publisher(org.reactivestreams.Publisher) CloseStatus(org.springframework.web.socket.CloseStatus) Subscription(org.reactivestreams.Subscription)

Example 7 with WebOutput

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

the class QuerydslDataFetcherTests method shouldFetchMultipleItemsWithListInput.

@Test
void shouldFetchMultipleItemsWithListInput() {
    Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
    mockRepository.saveAll(Arrays.asList(book1, book2));
    Mono<WebOutput> output = graphQlSetup(mockRepository).toWebGraphQlHandler().handleRequest(input("{ booksById(id: [42,53]) {name}}"));
    List<String> names = GraphQlResponse.from(output).toList("booksById", Book.class).stream().map(Book::getName).collect(Collectors.toList());
    assertThat(names).containsExactlyInAnyOrder(book1.getName(), book2.getName());
}
Also used : WebOutput(org.springframework.graphql.web.WebOutput) Author(org.springframework.graphql.Author) Test(org.junit.jupiter.api.Test)

Example 8 with WebOutput

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

the class QuerydslDataFetcherTests method shouldFetchSingleItems.

@Test
void shouldFetchSingleItems() {
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    mockRepository.save(book);
    Consumer<GraphQlSetup> tester = setup -> {
        Mono<WebOutput> output = setup.toWebGraphQlHandler().handleRequest(input("{ bookById(id: 42) {name}}"));
        Book actualBook = GraphQlResponse.from(output).toEntity("bookById", Book.class);
        assertThat(actualBook.getName()).isEqualTo(book.getName());
    };
    // explicit wiring
    tester.accept(graphQlSetup("bookById", QuerydslDataFetcher.builder(mockRepository).single()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
Also used : Arrays(java.util.Arrays) GraphQlRepository(org.springframework.graphql.data.GraphQlRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) WebOutput(org.springframework.graphql.web.WebOutput) CrudRepository(org.springframework.data.repository.CrudRepository) ReactiveQuerydslPredicateExecutor(org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor) Value(org.springframework.beans.factory.annotation.Value) Author(org.springframework.graphql.Author) ArgumentCaptor(org.mockito.ArgumentCaptor) Repository(org.springframework.data.repository.Repository) KeyValueRepositoryFactory(org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory) DataFetcher(graphql.schema.DataFetcher) Nullable(org.springframework.lang.Nullable) URI(java.net.URI) GraphQlResponse(org.springframework.graphql.GraphQlResponse) QuerydslPredicateExecutor(org.springframework.data.querydsl.QuerydslPredicateExecutor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) RuntimeWiringConfigurer(org.springframework.graphql.execution.RuntimeWiringConfigurer) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) MapKeyValueAdapter(org.springframework.data.map.MapKeyValueAdapter) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) WebInput(org.springframework.graphql.web.WebInput) KeyValueTemplate(org.springframework.data.keyvalue.core.KeyValueTemplate) Optional(java.util.Optional) Predicate(com.querydsl.core.types.Predicate) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) Mockito.mock(org.mockito.Mockito.mock) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) Test(org.junit.jupiter.api.Test)

Example 9 with WebOutput

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

the class QuerydslDataFetcherTests method shouldFetchMultipleItems.

@Test
void shouldFetchMultipleItems() {
    Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
    mockRepository.saveAll(Arrays.asList(book1, book2));
    Consumer<GraphQlSetup> tester = graphQlSetup -> {
        Mono<WebOutput> output = graphQlSetup.toWebGraphQlHandler().handleRequest(input("{ books {name}}"));
        List<String> names = GraphQlResponse.from(output).toList("books", Book.class).stream().map(Book::getName).collect(Collectors.toList());
        assertThat(names).containsExactlyInAnyOrder(book1.getName(), book2.getName());
    };
    // explicit wiring
    tester.accept(graphQlSetup("books", QuerydslDataFetcher.builder(mockRepository).many()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
Also used : Arrays(java.util.Arrays) GraphQlRepository(org.springframework.graphql.data.GraphQlRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) WebOutput(org.springframework.graphql.web.WebOutput) CrudRepository(org.springframework.data.repository.CrudRepository) ReactiveQuerydslPredicateExecutor(org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor) Value(org.springframework.beans.factory.annotation.Value) Author(org.springframework.graphql.Author) ArgumentCaptor(org.mockito.ArgumentCaptor) Repository(org.springframework.data.repository.Repository) KeyValueRepositoryFactory(org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory) DataFetcher(graphql.schema.DataFetcher) Nullable(org.springframework.lang.Nullable) URI(java.net.URI) GraphQlResponse(org.springframework.graphql.GraphQlResponse) QuerydslPredicateExecutor(org.springframework.data.querydsl.QuerydslPredicateExecutor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) RuntimeWiringConfigurer(org.springframework.graphql.execution.RuntimeWiringConfigurer) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) MapKeyValueAdapter(org.springframework.data.map.MapKeyValueAdapter) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) WebInput(org.springframework.graphql.web.WebInput) KeyValueTemplate(org.springframework.data.keyvalue.core.KeyValueTemplate) Optional(java.util.Optional) Predicate(com.querydsl.core.types.Predicate) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) Mockito.mock(org.mockito.Mockito.mock) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 10 with WebOutput

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

the class QuerydslDataFetcherTests method shouldApplyCustomizerInRepository.

@Test
void shouldApplyCustomizerInRepository() {
    MockWithCustomizerRepository repository = repositoryFactory.getRepository(MockWithCustomizerRepository.class);
    Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
    repository.saveAll(Arrays.asList(book1, book2));
    Consumer<GraphQlSetup> tester = graphQlSetup -> {
        Mono<WebOutput> output = graphQlSetup.toWebGraphQlHandler().handleRequest(input("{ books {name}}"));
        List<String> names = GraphQlResponse.from(output).toList("books", Book.class).stream().map(Book::getName).collect(Collectors.toList());
        assertThat(names).containsExactlyInAnyOrder(book1.getName(), book2.getName());
    };
    // explicit wiring
    tester.accept(graphQlSetup("books", QuerydslDataFetcher.builder(mockRepository).many()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
Also used : Arrays(java.util.Arrays) GraphQlRepository(org.springframework.graphql.data.GraphQlRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) WebOutput(org.springframework.graphql.web.WebOutput) CrudRepository(org.springframework.data.repository.CrudRepository) ReactiveQuerydslPredicateExecutor(org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor) Value(org.springframework.beans.factory.annotation.Value) Author(org.springframework.graphql.Author) ArgumentCaptor(org.mockito.ArgumentCaptor) Repository(org.springframework.data.repository.Repository) KeyValueRepositoryFactory(org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory) DataFetcher(graphql.schema.DataFetcher) Nullable(org.springframework.lang.Nullable) URI(java.net.URI) GraphQlResponse(org.springframework.graphql.GraphQlResponse) QuerydslPredicateExecutor(org.springframework.data.querydsl.QuerydslPredicateExecutor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) RuntimeWiringConfigurer(org.springframework.graphql.execution.RuntimeWiringConfigurer) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) MapKeyValueAdapter(org.springframework.data.map.MapKeyValueAdapter) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) WebInput(org.springframework.graphql.web.WebInput) KeyValueTemplate(org.springframework.data.keyvalue.core.KeyValueTemplate) Optional(java.util.Optional) Predicate(com.querydsl.core.types.Predicate) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) Mockito.mock(org.mockito.Mockito.mock) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

WebOutput (org.springframework.graphql.web.WebOutput)18 Test (org.junit.jupiter.api.Test)17 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)17 Author (org.springframework.graphql.Author)9 URI (java.net.URI)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 WebInput (org.springframework.graphql.web.WebInput)6 HttpHeaders (org.springframework.http.HttpHeaders)6 Nullable (org.springframework.lang.Nullable)6 Flux (reactor.core.publisher.Flux)6 Mono (reactor.core.publisher.Mono)6 Predicate (com.querydsl.core.types.Predicate)5 DataFetcher (graphql.schema.DataFetcher)5 Optional (java.util.Optional)5 Consumer (java.util.function.Consumer)5 Collectors (java.util.stream.Collectors)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 ArgumentCaptor (org.mockito.ArgumentCaptor)5