Skip to content

Commit

Permalink
Moved Mono/Flux conversion to reactive module by adding a DataFetcher…
Browse files Browse the repository at this point in the history
…ResultProcessor abstraction
  • Loading branch information
paulbakker committed Jun 8, 2021
1 parent 3840a13 commit 395c0a4
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 157 deletions.
2 changes: 0 additions & 2 deletions graphql-dgs-example-java-webflux/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client",
"com.netflix.graphql.dgs:graphql-dgs-example-shared"
],
Expand Down Expand Up @@ -833,7 +832,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client",
"com.netflix.graphql.dgs:graphql-dgs-example-shared"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.graphql.dgs.example.reactive.datafetchers;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsQuery;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@DgsComponent
public class ReactiveDataFetchers {
@DgsQuery
public Mono<String> mono() {
return Mono.just("hello mono");
}

@DgsQuery
public Flux<Integer> flux() {
return Flux.just(1, 2, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extend type Query {
mono: String
flux: [Int]
}
2 changes: 0 additions & 2 deletions graphql-dgs-example-java/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client",
"com.netflix.graphql.dgs:graphql-dgs-example-shared"
],
Expand Down Expand Up @@ -866,7 +865,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client",
"com.netflix.graphql.dgs:graphql-dgs-example-shared"
],
Expand Down
6 changes: 0 additions & 6 deletions graphql-dgs-example-shared/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,6 @@
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
Expand Down Expand Up @@ -611,9 +608,6 @@
"locked": "1.11.0"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
Expand Down
7 changes: 0 additions & 7 deletions graphql-dgs-extended-scalars/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,6 @@
],
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down Expand Up @@ -636,7 +630,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client"
],
"locked": "3.4.6"
Expand Down
12 changes: 0 additions & 12 deletions graphql-dgs-reactive/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,6 @@
],
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down Expand Up @@ -568,12 +562,6 @@
"io.mockk:mockk": {
"locked": "1.11.0"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"io.projectreactor:reactor-test": {
"locked": "3.4.6"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.graphql.dgs.reactive.internal

import com.netflix.graphql.dgs.internal.DataFetcherResultProcessor
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.lang.IllegalArgumentException

class MonoDataFetcherResultProcessor : DataFetcherResultProcessor {
override fun supportsType(originalResult: Any): Boolean {
return originalResult is Mono<*>
}

override fun process(originalResult: Any): Any {
if (originalResult is Mono<*>) {
return originalResult.toFuture()
} else {
throw IllegalArgumentException("Instance passed to ${this::class.qualifiedName} was not a Mono<*>. It was a ${originalResult::class.qualifiedName} instead")
}
}
}

class FluxDataFetcherResultProcessor : DataFetcherResultProcessor {
override fun supportsType(originalResult: Any): Boolean {
return originalResult is Flux<*>
}

override fun process(originalResult: Any): Any {
if (originalResult is Flux<*>) {
return originalResult.collectList().toFuture()
} else {
throw IllegalArgumentException("Instance passed to ${this::class.qualifiedName} was not a Flux<*>. It was a ${originalResult::class.qualifiedName} instead")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.netflix.graphql.dgs.internal.DgsDataLoaderProvider
import com.netflix.graphql.dgs.internal.DgsSchemaProvider
import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveGraphQLContextBuilder
import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveQueryExecutor
import com.netflix.graphql.dgs.reactive.internal.FluxDataFetcherResultProcessor
import com.netflix.graphql.dgs.reactive.internal.MonoDataFetcherResultProcessor
import graphql.execution.AsyncExecutionStrategy
import graphql.execution.AsyncSerialExecutionStrategy
import graphql.execution.instrumentation.ChainedInstrumentation
Expand Down Expand Up @@ -106,7 +108,9 @@ internal class ReactiveReturnTypesTest {
applicationContextMock,
federationResolver = Optional.empty(),
existingTypeDefinitionRegistry = Optional.empty(),
mockProviders = Optional.empty()
mockProviders = Optional.empty(),
listOf(DgsSchemaProvider.DEFAULT_SCHEMA_LOCATION),
listOf(MonoDataFetcherResultProcessor(), FluxDataFetcherResultProcessor()),
)

val schema = provider.schema(
Expand Down
7 changes: 0 additions & 7 deletions graphql-dgs-spring-boot-micrometer/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,6 @@
"io.micrometer:micrometer-core": {
"locked": "1.5.11"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"net.bytebuddy:byte-buddy": {
"locked": "1.10.20"
},
Expand Down Expand Up @@ -750,7 +744,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client"
],
"locked": "3.4.6"
Expand Down
12 changes: 0 additions & 12 deletions graphql-dgs-spring-boot-oss-autoconfigure/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,6 @@
],
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down Expand Up @@ -568,12 +562,6 @@
"io.mockk:mockk": {
"locked": "1.11.0"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,16 @@ open class DgsAutoConfiguration(
dataFetcherExceptionHandler: DataFetcherExceptionHandler,
existingTypeDefinitionFactory: Optional<TypeDefinitionRegistry>,
existingCodeRegistry: Optional<GraphQLCodeRegistry>,
mockProviders: Optional<Set<MockProvider>>
mockProviders: Optional<Set<MockProvider>>,
dataFetcherResultProcessors: List<DataFetcherResultProcessor>,
): DgsSchemaProvider {
return DgsSchemaProvider(
applicationContext,
federationResolver,
existingTypeDefinitionFactory,
mockProviders,
configProps.schemaLocations
configProps.schemaLocations,
dataFetcherResultProcessors,
)
}

Expand Down
2 changes: 0 additions & 2 deletions graphql-dgs-spring-boot-starter/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client"
],
"locked": "3.4.6"
Expand Down Expand Up @@ -719,7 +718,6 @@
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs",
"com.netflix.graphql.dgs:graphql-dgs-client"
],
"locked": "3.4.6"
Expand Down
12 changes: 0 additions & 12 deletions graphql-dgs-spring-webflux-autoconfigure/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,6 @@
],
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down Expand Up @@ -606,12 +600,6 @@
"io.mockk:mockk": {
"locked": "1.11.0"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"io.projectreactor:reactor-test": {
"locked": "3.4.6"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.netflix.graphql.dgs.reactive.DgsReactiveCustomContextBuilderWithReque
import com.netflix.graphql.dgs.reactive.DgsReactiveQueryExecutor
import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveGraphQLContextBuilder
import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveQueryExecutor
import com.netflix.graphql.dgs.reactive.internal.FluxDataFetcherResultProcessor
import com.netflix.graphql.dgs.reactive.internal.MonoDataFetcherResultProcessor
import com.netflix.graphql.dgs.webflux.handlers.DgsReactiveWebsocketHandler
import com.netflix.graphql.dgs.webflux.handlers.DgsWebfluxHttpHandler
import graphql.ExecutionInput
Expand Down Expand Up @@ -154,4 +156,14 @@ open class DgsWebFluxAutoConfiguration(private val configProps: DgsWebfluxConfig
open fun handlerAdapter(): WebSocketHandlerAdapter? {
return WebSocketHandlerAdapter()
}

@Bean
open fun monoReactiveDataFetcherResultProcessor(): MonoDataFetcherResultProcessor {
return MonoDataFetcherResultProcessor()
}

@Bean
open fun fluxReactiveDataFetcherResultProcessor(): FluxDataFetcherResultProcessor {
return FluxDataFetcherResultProcessor()
}
}
12 changes: 0 additions & 12 deletions graphql-dgs-spring-webmvc-autoconfigure/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,6 @@
],
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"jakarta.servlet:jakarta.servlet-api": {
"locked": "4.0.4"
},
Expand Down Expand Up @@ -597,12 +591,6 @@
"io.mockk:mockk": {
"locked": "1.11.0"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"jakarta.servlet:jakarta.servlet-api": {
"locked": "4.0.4"
},
Expand Down
12 changes: 0 additions & 12 deletions graphql-dgs-spring-webmvc/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,6 @@
],
"project": true
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down Expand Up @@ -527,12 +521,6 @@
"io.mockk:mockk": {
"locked": "1.11.0"
},
"io.projectreactor:reactor-core": {
"firstLevelTransitive": [
"com.netflix.graphql.dgs:graphql-dgs"
],
"locked": "3.4.6"
},
"org.jetbrains.kotlin:kotlin-bom": {
"locked": "1.4.32"
},
Expand Down
Loading

0 comments on commit 395c0a4

Please sign in to comment.