Skip to content

Commit b05cf6e

Browse files
author
Carlo Feliciano Aureus
committed
Issue-8: Test using query with an aliased key
1 parent 7640ec2 commit b05cf6e

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public CompletionStage<List<DataFetcherResult<Object>>> load(List<DataFetchingEn
5858
GraphQLContext graphQLContext = dfeTemplate.getContext();
5959

6060
List<Map<String, Object>> representations = dataFetchingEnvironments.stream()
61-
.map(DataFetchingEnvironment::getSource)
61+
//.map(DataFetchingEnvironment::getSource)
62+
.map(dataFetchingEnvironment -> getSource(dataFetchingEnvironment))
6263
.map(source -> createRepresentation((Map<String, Object>) source))
6364
.collect(Collectors.toList());
6465

@@ -77,6 +78,12 @@ public CompletionStage<List<DataFetcherResult<Object>>> load(List<DataFetchingEn
7778
.thenApply(result -> batchResultTransformer.toBatchResult(result, dataFetchingEnvironments));
7879
}
7980

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+
8087
private List<String> generateRepresentationTemplate(FederationMetadata.EntityExtensionMetadata metadata, String fieldName) {
8188
List<String> representationFields = new ArrayList<>();
8289

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.intuit.graphql.orchestrator.integration.federation
2+
3+
import com.google.common.collect.ImmutableMap
4+
import com.intuit.graphql.orchestrator.GraphQLOrchestrator
5+
import com.intuit.graphql.orchestrator.ServiceProvider
6+
import com.intuit.graphql.orchestrator.testhelpers.MockServiceProvider
7+
import com.intuit.graphql.orchestrator.testhelpers.SimpleMockServiceProvider
8+
import com.intuit.graphql.orchestrator.utils.GraphQLUtil
9+
import com.intuit.graphql.orchestrator.utils.SelectionSetUtil
10+
import graphql.ExecutionInput
11+
import graphql.language.Document
12+
import graphql.language.Field
13+
import graphql.language.OperationDefinition
14+
import graphql.parser.Parser
15+
import helpers.BaseIntegrationTestSpecification
16+
17+
class FederatedQueryWithKeyAliasedSimpleSpec extends BaseIntegrationTestSpecification {
18+
19+
Parser parser = GraphQLUtil.parser
20+
21+
GraphQLOrchestrator specUnderTest
22+
23+
private def bookSchema = '''
24+
type Query {
25+
bookById(id: ID): Book
26+
}
27+
28+
type Book {
29+
id: ID
30+
name: String
31+
pageCount: Int
32+
author: Author
33+
}
34+
35+
type Author @key(fields: "id") {
36+
id: String
37+
}
38+
'''
39+
40+
private def authorSchema = '''
41+
type Query {
42+
authorByid(id: ID!): Author
43+
}
44+
45+
type Author @extends @key(fields: "id") {
46+
id: String @external
47+
firstName: String!
48+
lastName: String!
49+
}
50+
'''
51+
52+
def setup() {
53+
}
54+
55+
def "test Query"() {
56+
given:
57+
//def BOOK_QUERY = "query QUERY {bookById(id:\"12345\") {id name pageCount author {authorId:id}}}"
58+
def bookResponse = [
59+
data: [
60+
bookById: [
61+
id : "book-1",
62+
name : "GraphQuilt: The future of Schema Stitching",
63+
pageCount: 100,
64+
author : [
65+
"author": "12345"
66+
]
67+
]
68+
69+
]
70+
]
71+
72+
SimpleMockServiceProvider bookService = createSimpleMockService("bookService",
73+
ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, bookSchema, bookResponse)
74+
75+
76+
def authorResponse = [
77+
data: [
78+
_entities: [
79+
[
80+
__typename: "Author",
81+
id : "12345",
82+
firstName : "Charles",
83+
lastName : "Wayne"
84+
]
85+
]
86+
]
87+
]
88+
SimpleMockServiceProvider authorService = createSimpleMockService("authorService",
89+
ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, authorSchema, authorResponse)
90+
91+
92+
ServiceProvider[] services = [bookService, authorService]
93+
specUnderTest = createGraphQLOrchestrator(services)
94+
95+
when:
96+
ExecutionInput booksAndPetsEI = ExecutionInput.newExecutionInput().query('''
97+
query {
98+
bookById(id: "12345") {
99+
id
100+
name
101+
pageCount
102+
author {
103+
authorId: id
104+
authorName: firstName
105+
}
106+
}
107+
}
108+
''')
109+
.variables(ImmutableMap.of("includeType", Boolean.TRUE))
110+
.build()
111+
112+
Map<String, Object> executionResult = specUnderTest.execute(booksAndPetsEI).get().toSpecification()
113+
114+
then:
115+
116+
def AUTHOR_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {firstName}}}"
117+
118+
executionResult.get("errors") == null
119+
executionResult.get("data") != null
120+
121+
executionResult?.data?.bookById?.id == "book-1"
122+
executionResult?.data?.bookById?.name == "GraphQuilt: The future of Schema Stitching"
123+
executionResult?.data?.bookById?.pageCount == 100
124+
executionResult?.data?.bookById?.author.authorId == "12345"
125+
executionResult?.data?.bookById?.author.authorName == "Charles"
126+
127+
Map<String, Object> dataValue = (Map<String, Object>) executionResult.get("data")
128+
dataValue.keySet().containsAll("bookById")
129+
((Map<String, Objects>) dataValue.get("bookById")).keySet().size() == 4
130+
ExecutionInput authorServiceExecutionInput = getCapturedDownstreamExecutionInput(authorService)
131+
authorServiceExecutionInput.getQuery() == AUTHOR_QUERY
132+
Map<String, Object> authorsServiceVariables = authorServiceExecutionInput.getVariables()
133+
authorsServiceVariables?.REPRESENTATIONS[0]?.__typename == "Author"
134+
authorsServiceVariables?.REPRESENTATIONS[0]?.id == "12345"
135+
}
136+
137+
}

src/test/groovy/helpers/BaseIntegrationTestSpecification.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class BaseIntegrationTestSpecification extends Specification {
3636
.build()
3737
}
3838

39+
def createSimpleMockService(String namespace, ServiceProvider.ServiceType serviceType,
40+
String testSchema, Map<String, Object> mockServiceResponse) {
41+
return SimpleMockServiceProvider.builder()
42+
.sdlFiles(["schema.graphqls": testSchema])
43+
.namespace(namespace)
44+
.serviceType(serviceType)
45+
.mockResponse(mockServiceResponse)
46+
.build()
47+
}
48+
3949
def createQueryMatchingService(String namespace, ServiceProvider.ServiceType serviceType,
4050
String testSchema, Map<String, Object> mockServiceResponse) {
4151
return MockServiceProvider.builder()

0 commit comments

Comments
 (0)