Skip to content

Commit 4e19316

Browse files
author
Carlo Feliciano Aureus
committed
Issue-8: implement mapping of entity key ids to alias
1 parent b05cf6e commit 4e19316

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/main/java/com/intuit/graphql/orchestrator/batch/EntityFetcherBatchLoader.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.intuit.graphql.orchestrator.schema.ServiceMetadata;
1515
import graphql.GraphQLContext;
1616
import graphql.execution.DataFetcherResult;
17+
import graphql.execution.MergedField;
1718
import graphql.introspection.Introspection;
1819
import graphql.language.Field;
1920
import graphql.language.InlineFragment;
@@ -58,9 +59,7 @@ public CompletionStage<List<DataFetcherResult<Object>>> load(List<DataFetchingEn
5859
GraphQLContext graphQLContext = dfeTemplate.getContext();
5960

6061
List<Map<String, Object>> representations = dataFetchingEnvironments.stream()
61-
//.map(DataFetchingEnvironment::getSource)
62-
.map(dataFetchingEnvironment -> getSource(dataFetchingEnvironment))
63-
.map(source -> createRepresentation((Map<String, Object>) source))
62+
.map(dataFetchingEnvironment -> createRepresentation(dataFetchingEnvironment))
6463
.collect(Collectors.toList());
6564

6665
List<InlineFragment> inlineFragments = new ArrayList<>();
@@ -78,12 +77,6 @@ public CompletionStage<List<DataFetcherResult<Object>>> load(List<DataFetchingEn
7877
.thenApply(result -> batchResultTransformer.toBatchResult(result, dataFetchingEnvironments));
7978
}
8079

81-
private Map<String, Object> getSource(DataFetchingEnvironment dataFetchingEnvironment) {
82-
// TODO
83-
// return a POJO with source, and metadata about alias mapping.
84-
return dataFetchingEnvironment.getSource();
85-
}
86-
8780
private List<String> generateRepresentationTemplate(FederationMetadata.EntityExtensionMetadata metadata, String fieldName) {
8881
List<String> representationFields = new ArrayList<>();
8982

@@ -140,20 +133,43 @@ private InlineFragment createEntityRequestInlineFragment(DataFetchingEnvironment
140133
newField()
141134
.selectionSet(fieldSelectionSet)
142135
.name(originalField.getName())
136+
.alias(originalField.getAlias())
143137
.build())
144138
.build());
145139
return inlineFragmentBuilder.build();
146140
}
147141

148142
private Map<String, Object> createRepresentation(
149-
Map<String, Object> dataSource
143+
DataFetchingEnvironment dataFetchingEnvironment
150144
){
145+
Map<String, Object> dataSource = dataFetchingEnvironment.getSource();
146+
Map<String, String> fieldToAliasMap = buildFieldToAliasMap(dataFetchingEnvironment);
147+
151148
Map<String, Object> entityRepresentation = new HashMap<>();
152149
entityRepresentation.put(Introspection.TypeNameMetaFieldDef.getName(), this.entityTypeName);
153150

154151
this.representationFieldTemplate
155-
.forEach(fieldName -> entityRepresentation.put(fieldName, dataSource.get(fieldName)));
152+
.forEach(fieldName -> {
153+
String dataSourceKey = fieldToAliasMap.get(fieldName);
154+
entityRepresentation.put(fieldName, dataSource.get(dataSourceKey));
155+
});
156156

157157
return entityRepresentation;
158158
}
159+
160+
/**
161+
* builds mapping of fieldName-alias. If fieldName has no alias, it will be mapped to itself.
162+
*
163+
* @param dataFetchingEnvironment
164+
* @return
165+
*/
166+
private Map<String, String> buildFieldToAliasMap(DataFetchingEnvironment dataFetchingEnvironment) {
167+
MergedField parentField = dataFetchingEnvironment.getExecutionStepInfo().getParent().getField();
168+
return parentField.getSingleField().getSelectionSet().getSelections()
169+
.stream()
170+
.filter(selection -> selection instanceof Field)
171+
.map(selection -> (Field) selection)
172+
.filter(field -> this.representationFieldTemplate.contains(field.getName()))
173+
.collect(Collectors.toMap(field -> field.getName(), field -> field.getAlias() == null? field.getName() : field.getAlias()));
174+
}
159175
}

src/main/java/com/intuit/graphql/orchestrator/batch/EntityFetcherBatchResultTransformer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intuit.graphql.orchestrator.federation.EntityFetchingException;
44
import graphql.GraphQLError;
55
import graphql.execution.DataFetcherResult;
6+
import graphql.language.Field;
67
import graphql.schema.DataFetchingEnvironment;
78
import lombok.extern.slf4j.Slf4j;
89
import org.apache.commons.collections4.CollectionUtils;
@@ -54,8 +55,13 @@ public List<DataFetcherResult<Object>> toBatchResult(DataFetcherResult<Map<Strin
5455

5556
//using IntStream instead of regular for loop or atomic reference, so we can parallelize this if we want
5657
IntStream.range(0, _entities.size()).forEach( idx -> {
58+
59+
DataFetchingEnvironment dataFetchingEnvironment = dataFetchingEnvironments.get(idx);
60+
Field field = dataFetchingEnvironment.getField();
61+
String fieldNameOrAlias = field.getAlias() == null ? field.getName() : field.getAlias();
62+
5763
Map<String, Object> entityInfo = _entities.get(idx);
58-
Object fieldData = (entityInfo != null) ? entityInfo.get(this.extFieldName) : null;
64+
Object fieldData = (entityInfo != null) ? entityInfo.get(fieldNameOrAlias) : null;
5965
List<GraphQLError> graphQLErrors = (List<GraphQLError>) entityInfo.getOrDefault("errors", new ArrayList<>());
6066

6167
dataFetcherResults.add(DataFetcherResult.newResult()

src/test/groovy/com/intuit/graphql/orchestrator/integration/federation/FederatedQueryWithKeyAliasedSimpleSpec.groovy

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import helpers.BaseIntegrationTestSpecification
1616

1717
class FederatedQueryWithKeyAliasedSimpleSpec extends BaseIntegrationTestSpecification {
1818

19-
Parser parser = GraphQLUtil.parser
20-
2119
GraphQLOrchestrator specUnderTest
2220

2321
private def bookSchema = '''
@@ -62,7 +60,7 @@ class FederatedQueryWithKeyAliasedSimpleSpec extends BaseIntegrationTestSpecific
6260
name : "GraphQuilt: The future of Schema Stitching",
6361
pageCount: 100,
6462
author : [
65-
"author": "12345"
63+
"authorId": "12345"
6664
]
6765
]
6866

@@ -79,8 +77,7 @@ class FederatedQueryWithKeyAliasedSimpleSpec extends BaseIntegrationTestSpecific
7977
[
8078
__typename: "Author",
8179
id : "12345",
82-
firstName : "Charles",
83-
lastName : "Wayne"
80+
authorName : "Charles"
8481
]
8582
]
8683
]
@@ -113,7 +110,7 @@ class FederatedQueryWithKeyAliasedSimpleSpec extends BaseIntegrationTestSpecific
113110

114111
then:
115112

116-
def AUTHOR_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {firstName}}}"
113+
def AUTHOR_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {authorName:firstName}}}"
117114

118115
executionResult.get("errors") == null
119116
executionResult.get("data") != null

0 commit comments

Comments
 (0)