|
| 1 | +/* |
| 2 | + * Copyright 2020 Expedia, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.expediagroup.graphql.execution |
| 18 | + |
| 19 | +import graphql.AssertException |
| 20 | +import graphql.ExecutionResult |
| 21 | +import graphql.ExecutionResultImpl |
| 22 | +import graphql.execution.DataFetcherExceptionHandler |
| 23 | +import graphql.execution.ExecutionContext |
| 24 | +import graphql.execution.ExecutionStrategy |
| 25 | +import graphql.execution.ExecutionStrategyParameters |
| 26 | +import graphql.execution.FetchedValue |
| 27 | +import graphql.execution.SimpleDataFetcherExceptionHandler |
| 28 | +import graphql.execution.SubscriptionExecutionStrategy |
| 29 | +import kotlinx.coroutines.flow.Flow |
| 30 | +import kotlinx.coroutines.flow.map |
| 31 | +import kotlinx.coroutines.future.await |
| 32 | +import kotlinx.coroutines.reactive.asFlow |
| 33 | +import org.reactivestreams.Publisher |
| 34 | +import java.util.Collections |
| 35 | +import java.util.concurrent.CompletableFuture |
| 36 | + |
| 37 | +/** |
| 38 | + * [SubscriptionExecutionStrategy] replacement that returns an [ExecutionResult] |
| 39 | + * that is a [Flow] instead of a [Publisher], and allows schema subscription functions |
| 40 | + * to return either a [Flow] or a [Publisher]. |
| 41 | + */ |
| 42 | +class FlowSubscriptionExecutionStrategy(dfe: DataFetcherExceptionHandler) : ExecutionStrategy(dfe) { |
| 43 | + constructor() : this(SimpleDataFetcherExceptionHandler()) |
| 44 | + |
| 45 | + override fun execute( |
| 46 | + executionContext: ExecutionContext, |
| 47 | + parameters: ExecutionStrategyParameters |
| 48 | + ): CompletableFuture<ExecutionResult> { |
| 49 | + |
| 50 | + val sourceEventStream = createSourceEventStream(executionContext, parameters) |
| 51 | + |
| 52 | + // |
| 53 | + // when the upstream source event stream completes, subscribe to it and wire in our adapter |
| 54 | + return sourceEventStream.thenApply { sourceFlow -> |
| 55 | + if (sourceFlow == null) { |
| 56 | + ExecutionResultImpl(null, executionContext.errors) |
| 57 | + } else { |
| 58 | + val returnFlow = sourceFlow.map { |
| 59 | + executeSubscriptionEvent(executionContext, parameters, it).await() |
| 60 | + } |
| 61 | + ExecutionResultImpl(returnFlow, executionContext.errors) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + https://github.com/facebook/graphql/blob/master/spec/Section%206%20--%20Execution.md |
| 68 | +
|
| 69 | + CreateSourceEventStream(subscription, schema, variableValues, initialValue): |
| 70 | +
|
| 71 | + Let {subscriptionType} be the root Subscription type in {schema}. |
| 72 | + Assert: {subscriptionType} is an Object type. |
| 73 | + Let {selectionSet} be the top level Selection Set in {subscription}. |
| 74 | + Let {rootField} be the first top level field in {selectionSet}. |
| 75 | + Let {argumentValues} be the result of {CoerceArgumentValues(subscriptionType, rootField, variableValues)}. |
| 76 | + Let {fieldStream} be the result of running {ResolveFieldEventStream(subscriptionType, initialValue, rootField, argumentValues)}. |
| 77 | + Return {fieldStream}. |
| 78 | + */ |
| 79 | + private fun createSourceEventStream( |
| 80 | + executionContext: ExecutionContext, |
| 81 | + parameters: ExecutionStrategyParameters |
| 82 | + ): CompletableFuture<Flow<*>> { |
| 83 | + val newParameters = firstFieldOfSubscriptionSelection(parameters) |
| 84 | + |
| 85 | + val fieldFetched = fetchField(executionContext, newParameters) |
| 86 | + return fieldFetched.thenApply { fetchedValue -> |
| 87 | + val flow = when (val publisherOrFlow = fetchedValue.fetchedValue) { |
| 88 | + null -> null |
| 89 | + is Publisher<*> -> publisherOrFlow.asFlow() |
| 90 | + is Flow<*> -> publisherOrFlow |
| 91 | + else -> throw AssertException( |
| 92 | + "You data fetcher must return a Flow of events when using graphql subscriptions" |
| 93 | + ) |
| 94 | + } |
| 95 | + flow |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + ExecuteSubscriptionEvent(subscription, schema, variableValues, initialValue): |
| 101 | +
|
| 102 | + Let {subscriptionType} be the root Subscription type in {schema}. |
| 103 | + Assert: {subscriptionType} is an Object type. |
| 104 | + Let {selectionSet} be the top level Selection Set in {subscription}. |
| 105 | + Let {data} be the result of running {ExecuteSelectionSet(selectionSet, subscriptionType, initialValue, variableValues)} normally (allowing parallelization). |
| 106 | + Let {errors} be any field errors produced while executing the selection set. |
| 107 | + Return an unordered map containing {data} and {errors}. |
| 108 | +
|
| 109 | + Note: The {ExecuteSubscriptionEvent()} algorithm is intentionally similar to {ExecuteQuery()} since this is how each event result is produced. |
| 110 | + */ |
| 111 | + |
| 112 | + private fun executeSubscriptionEvent( |
| 113 | + executionContext: ExecutionContext, |
| 114 | + parameters: ExecutionStrategyParameters, |
| 115 | + eventPayload: Any? |
| 116 | + ): CompletableFuture<ExecutionResult> { |
| 117 | + val newExecutionContext = executionContext.transform { builder -> builder.root(eventPayload) } |
| 118 | + |
| 119 | + val newParameters = firstFieldOfSubscriptionSelection(parameters) |
| 120 | + val fetchedValue = FetchedValue.newFetchedValue().fetchedValue(eventPayload) |
| 121 | + .rawFetchedValue(eventPayload) |
| 122 | + .localContext(parameters.localContext) |
| 123 | + .build() |
| 124 | + return completeField(newExecutionContext, newParameters, fetchedValue).fieldValue |
| 125 | + .thenApply { executionResult -> wrapWithRootFieldName(newParameters, executionResult) } |
| 126 | + } |
| 127 | + |
| 128 | + private fun wrapWithRootFieldName( |
| 129 | + parameters: ExecutionStrategyParameters, |
| 130 | + executionResult: ExecutionResult |
| 131 | + ): ExecutionResult { |
| 132 | + val rootFieldName = getRootFieldName(parameters) |
| 133 | + return ExecutionResultImpl( |
| 134 | + Collections.singletonMap<String, Any>(rootFieldName, executionResult.getData<Any>()), |
| 135 | + executionResult.errors |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + private fun getRootFieldName(parameters: ExecutionStrategyParameters): String { |
| 140 | + val rootField = parameters.field.singleField |
| 141 | + return if (rootField.alias != null) rootField.alias else rootField.name |
| 142 | + } |
| 143 | + |
| 144 | + private fun firstFieldOfSubscriptionSelection( |
| 145 | + parameters: ExecutionStrategyParameters |
| 146 | + ): ExecutionStrategyParameters { |
| 147 | + val fields = parameters.fields |
| 148 | + val firstField = fields.getSubField(fields.keys[0]) |
| 149 | + |
| 150 | + val fieldPath = parameters.path.segment(ExecutionStrategy.mkNameForPath(firstField.singleField)) |
| 151 | + return parameters.transform { builder -> builder.field(firstField).path(fieldPath) } |
| 152 | + } |
| 153 | +} |
0 commit comments