-
Notifications
You must be signed in to change notification settings - Fork 364
feat: CompletableFuture in FederatedTypeResolver, standardize entities resolver with other subgraph implementations #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samuelAndalon
merged 19 commits into
ExpediaGroup:master
from
samuelAndalon:feat/federated-type-resolver-promise
Sep 13, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ce0b217
feat: use additional directives for schema directives
07292a1
feat: add test for contact directive
6fa4fdb
feat: move test to federated schema v2
5aeff29
feat: remove unused import
93c88ab
feat: initial commit
e6965e1
feat: support completable future in entities data fetcher
b3813a8
feat: solve merge conflucts
cf2db3a
feat: fix tests
4891dfc
feat: simplify signature to match other subgraph implementations
samuelAndalon 7e6a47f
feat: update example
samuelAndalon f42bf4c
feat: update example
samuelAndalon 0ffa986
feat: update examples
samuelAndalon f2702bc
feat: update examples
samuelAndalon f70ebfa
feat: update examples
samuelAndalon e88a2d3
feat: update docs
samuelAndalon a60478f
feat: federatedSchemaGeneratorHooks to autowire FederatedTypeResolver…
samuelAndalon 7ea0cb7
feat: resolve conflicts
samuelAndalon 80f451f
Merge branch 'master' into feat/federated-type-resolver-promise
samuelAndalon 70c41eb
feat: fix test
samuelAndalon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...ain/kotlin/com/expediagroup/graphql/generator/federation/execution/EntitiesDataFetcher.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2022 Expedia, 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 | ||
* | ||
* https://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.expediagroup.graphql.generator.federation.execution | ||
|
||
import com.expediagroup.graphql.generator.federation.exception.InvalidFederatedRequest | ||
import com.expediagroup.graphql.generator.federation.execution.resolverexecutor.FederatedTypePromiseResolverExecutor | ||
import com.expediagroup.graphql.generator.federation.execution.resolverexecutor.FederatedTypeSuspendResolverExecutor | ||
import com.expediagroup.graphql.generator.federation.execution.resolverexecutor.ResolvableEntity | ||
import com.expediagroup.graphql.generator.federation.extensions.collectAll | ||
import com.expediagroup.graphql.generator.federation.extensions.toDataFetcherResult | ||
import graphql.execution.DataFetcherResult | ||
import graphql.schema.DataFetcher | ||
import graphql.schema.DataFetchingEnvironment | ||
import java.util.concurrent.CompletableFuture | ||
|
||
private const val TYPENAME_FIELD = "__typename" | ||
private const val REPRESENTATIONS = "representations" | ||
|
||
/** | ||
* Federated _entities field data fetcher. | ||
*/ | ||
open class EntitiesDataFetcher( | ||
resolvers: List<FederatedTypeResolver> | ||
) : DataFetcher<CompletableFuture<DataFetcherResult<List<Any?>>>> { | ||
|
||
constructor(vararg resolvers: FederatedTypeResolver) : this(resolvers.toList()) | ||
/** | ||
* Pre-compute resolvers by typename so, we don't have to search on every request | ||
*/ | ||
private val resolversByType: Map<String, FederatedTypeResolver> = resolvers.associateBy(FederatedTypeResolver::typeName) | ||
|
||
/** | ||
* Resolves entities based on the passed in representations argument. Entities are resolved in the same order | ||
* they are specified in the list of representations. If target representation cannot be resolved, NULL will | ||
* be returned instead. | ||
* | ||
* Representations are grouped by the underlying typename and each batch is resolved asynchronously before merging | ||
* the results back into a single list that preserves the original order. | ||
* | ||
* @return list of resolved nullable entities | ||
*/ | ||
override fun get(env: DataFetchingEnvironment): CompletableFuture<DataFetcherResult<List<Any?>>> { | ||
val representations: List<Map<String, Any>> = env.getArgument(REPRESENTATIONS) | ||
|
||
val representationsWithoutResolver = mutableListOf<IndexedValue<Map<String, Any>>>() | ||
val entitiesWithPromiseResolver = mutableListOf<ResolvableEntity<FederatedTypePromiseResolver<*>>>() | ||
val entitiesWithSuspendResolver = mutableListOf<ResolvableEntity<FederatedTypeSuspendResolver<*>>>() | ||
|
||
representations.withIndex() | ||
.groupBy { (_, representation) -> representation[TYPENAME_FIELD].toString() } | ||
.forEach { (typeName, indexedRequests) -> | ||
when (val resolver = resolversByType[typeName]) { | ||
is FederatedTypePromiseResolver<*> -> { | ||
entitiesWithPromiseResolver += ResolvableEntity(typeName, indexedRequests, resolver) | ||
} | ||
is FederatedTypeSuspendResolver<*> -> { | ||
entitiesWithSuspendResolver += ResolvableEntity(typeName, indexedRequests, resolver) | ||
} | ||
null -> { | ||
representationsWithoutResolver += indexedRequests | ||
} | ||
} | ||
} | ||
|
||
val noResolverErrors: CompletableFuture<List<Map<Int, Any?>>> = CompletableFuture.completedFuture( | ||
listOf( | ||
representationsWithoutResolver.associateBy(IndexedValue<Map<String, Any>>::index) { (_, representation) -> | ||
InvalidFederatedRequest("Unable to resolve federated type, representation=$representation") | ||
} | ||
) | ||
) | ||
|
||
val promises: List<CompletableFuture<List<Map<Int, Any?>>>> = listOf( | ||
FederatedTypePromiseResolverExecutor.execute(entitiesWithPromiseResolver, env), | ||
FederatedTypeSuspendResolverExecutor.execute(entitiesWithSuspendResolver, env), | ||
noResolverErrors | ||
) | ||
|
||
return promises | ||
.collectAll() | ||
.thenApply { results -> | ||
results.asSequence() | ||
.flatten() | ||
.map(Map<Int, Any?>::toList) | ||
.flatten() | ||
.sortedBy(Pair<Int, Any?>::first) | ||
.map(Pair<Int, Any?>::second) | ||
.toDataFetcherResult() | ||
} | ||
} | ||
} |
92 changes: 0 additions & 92 deletions
92
...src/main/kotlin/com/expediagroup/graphql/generator/federation/execution/EntityResolver.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.