|
| 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 graphql.ExecutionInput |
| 7 | +import helpers.BaseIntegrationTestSpecification |
| 8 | + |
| 9 | +class FederationQueryWithFieldSelectionsOnlySpec extends BaseIntegrationTestSpecification { |
| 10 | + |
| 11 | + GraphQLOrchestrator specUnderTest |
| 12 | + |
| 13 | + static testQueryBase = ''' |
| 14 | + query { |
| 15 | + bookById(id: "12345") { |
| 16 | + id |
| 17 | + name |
| 18 | + pageCount |
| 19 | + author { |
| 20 | + id |
| 21 | + firstName |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + ''' |
| 26 | + |
| 27 | + static testQueryKeyAliased = ''' |
| 28 | + query { |
| 29 | + bookById(id: "12345") { |
| 30 | + id |
| 31 | + name |
| 32 | + pageCount |
| 33 | + author { |
| 34 | + idAlias: id |
| 35 | + fnameAlias: firstName |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + ''' |
| 40 | + |
| 41 | + static testQueryKeyNotAliased = ''' |
| 42 | + query { |
| 43 | + bookById(id: "12345") { |
| 44 | + id |
| 45 | + name |
| 46 | + pageCount |
| 47 | + author { |
| 48 | + id |
| 49 | + fnameAlias: firstName |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + ''' |
| 54 | + |
| 55 | + static testQueryKeyNotQueried = ''' |
| 56 | + query { |
| 57 | + bookById(id: "12345") { |
| 58 | + id |
| 59 | + name |
| 60 | + pageCount |
| 61 | + author { |
| 62 | + fnameAlias: firstName |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + ''' |
| 67 | + |
| 68 | + private def bookSchema = ''' |
| 69 | + type Query { |
| 70 | + bookById(id: ID): Book |
| 71 | + } |
| 72 | + |
| 73 | + type Book { |
| 74 | + id: ID |
| 75 | + name: String |
| 76 | + pageCount: Int |
| 77 | + author: Author |
| 78 | + } |
| 79 | + |
| 80 | + type Author @key(fields: "id") { |
| 81 | + id: String |
| 82 | + } |
| 83 | + ''' |
| 84 | + |
| 85 | + private def authorSchema = ''' |
| 86 | + type Query { |
| 87 | + authorByid(id: ID!): Author |
| 88 | + } |
| 89 | + |
| 90 | + type Author @extends @key(fields: "id") { |
| 91 | + id: String @external |
| 92 | + firstName: String! |
| 93 | + lastName: String! |
| 94 | + } |
| 95 | + ''' |
| 96 | + |
| 97 | + def setup() { |
| 98 | + } |
| 99 | + |
| 100 | + def "Query single field of entity extension"(testQuery, bookSvcAuthorData, author_id, author_idAlias, author_firstName, author_fnameAlias) { |
| 101 | + given: |
| 102 | + |
| 103 | + def bookResponse = [ |
| 104 | + data: [ |
| 105 | + bookById: [ |
| 106 | + id : "book-1", |
| 107 | + name : "GraphQuilt: The future of Schema Stitching", |
| 108 | + pageCount: 100, |
| 109 | + author : bookSvcAuthorData |
| 110 | + ] |
| 111 | + |
| 112 | + ] |
| 113 | + ] |
| 114 | + |
| 115 | + def fnameField = author_firstName != null ? "firstName" : "fnameAlias" |
| 116 | + def authorResponse = [ |
| 117 | + data: [ |
| 118 | + _entities: [ |
| 119 | + [ |
| 120 | + __typename : "Author", |
| 121 | + id : "12345", |
| 122 | + (fnameField): "Charles" |
| 123 | + ] |
| 124 | + ] |
| 125 | + ] |
| 126 | + ] |
| 127 | + |
| 128 | + ServiceProvider bookService = createSimpleMockService("bookService", |
| 129 | + ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, bookSchema, bookResponse) |
| 130 | + |
| 131 | + |
| 132 | + ServiceProvider authorService = createSimpleMockService("authorService", |
| 133 | + ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, authorSchema, authorResponse) |
| 134 | + |
| 135 | + |
| 136 | + ServiceProvider[] services = [bookService, authorService] |
| 137 | + specUnderTest = createGraphQLOrchestrator(services) |
| 138 | + |
| 139 | + when: |
| 140 | + ExecutionInput booksAndPetsEI = ExecutionInput.newExecutionInput().query(testQuery) |
| 141 | + .variables(ImmutableMap.of("includeType", Boolean.TRUE)) |
| 142 | + .build() |
| 143 | + |
| 144 | + Map<String, Object> executionResult = specUnderTest.execute(booksAndPetsEI).get().toSpecification() |
| 145 | + |
| 146 | + then: |
| 147 | + executionResult.get("errors") == null |
| 148 | + executionResult.get("data") != null |
| 149 | + |
| 150 | + executionResult?.data?.bookById?.id == "book-1" |
| 151 | + executionResult?.data?.bookById?.name == "GraphQuilt: The future of Schema Stitching" |
| 152 | + executionResult?.data?.bookById?.pageCount == 100 |
| 153 | + |
| 154 | + executionResult?.data?.bookById?.author.id == author_id |
| 155 | + executionResult?.data?.bookById?.author.idAlias == author_idAlias |
| 156 | + executionResult?.data?.bookById?.author.firstName == author_firstName |
| 157 | + executionResult?.data?.bookById?.author.fnameAlias == author_fnameAlias |
| 158 | + |
| 159 | + Map<String, Object> dataValue = (Map<String, Object>) executionResult.get("data") |
| 160 | + dataValue.keySet().containsAll("bookById") |
| 161 | + ((Map<String, Objects>) dataValue.get("bookById")).keySet().size() == 4 |
| 162 | + ExecutionInput authorServiceExecutionInput = getCapturedDownstreamExecutionInput(authorService) |
| 163 | + |
| 164 | + def firstNameSelection = author_firstName != null ? "firstName" : "fnameAlias:firstName" |
| 165 | + authorServiceExecutionInput.getQuery() == "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {${firstNameSelection}}}}" |
| 166 | + Map<String, Object> authorsServiceVariables = authorServiceExecutionInput.getVariables() |
| 167 | + authorsServiceVariables?.REPRESENTATIONS[0]?.__typename == "Author" |
| 168 | + authorsServiceVariables?.REPRESENTATIONS[0]?.id == "12345" |
| 169 | + |
| 170 | + where: |
| 171 | + testQuery | bookSvcAuthorData | author_id | author_idAlias | author_firstName | author_fnameAlias |
| 172 | + testQueryBase | ["id": "12345"] | "12345" | null | "Charles" | null |
| 173 | + testQueryKeyAliased | ["idAlias": "12345"] | null | "12345" | null | "Charles" |
| 174 | + testQueryKeyNotAliased | ["id": "12345"] | "12345" | null | null | "Charles" |
| 175 | + testQueryKeyNotQueried | ["id": "12345"] | null | null | null | "Charles" |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + def "Query multiple field of entity extension"() { |
| 180 | + given: |
| 181 | + |
| 182 | + def testQuery = ''' |
| 183 | + query { |
| 184 | + bookById(id: "12345") { |
| 185 | + id |
| 186 | + name |
| 187 | + pageCount |
| 188 | + author { |
| 189 | + idAlias:id |
| 190 | + firstName |
| 191 | + lastName |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + ''' |
| 196 | + |
| 197 | + def bookResponse = [ |
| 198 | + data: [ |
| 199 | + bookById: [ |
| 200 | + id : "book-1", |
| 201 | + name : "GraphQuilt: The future of Schema Stitching", |
| 202 | + pageCount: 100, |
| 203 | + author : ["idAlias": "12345"] |
| 204 | + ] |
| 205 | + |
| 206 | + ] |
| 207 | + ] |
| 208 | + |
| 209 | + def FNAME_ENTITY_FETCH_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {firstName}}}" |
| 210 | + def LNAME_ENTITY_FETCH_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {lastName}}}" |
| 211 | + def authorResponse = [ |
| 212 | + (FNAME_ENTITY_FETCH_QUERY): [data: [ |
| 213 | + _entities: [ |
| 214 | + [ |
| 215 | + __typename: "Author", |
| 216 | + id : "12345", |
| 217 | + firstName : "Charles" |
| 218 | + ] |
| 219 | + ] |
| 220 | + ]], |
| 221 | + (LNAME_ENTITY_FETCH_QUERY): [data: [ |
| 222 | + _entities: [ |
| 223 | + [ |
| 224 | + __typename: "Author", |
| 225 | + id : "12345", |
| 226 | + lastName : "Charles-LastName" |
| 227 | + ] |
| 228 | + ] |
| 229 | + ]] |
| 230 | + |
| 231 | + ] |
| 232 | + |
| 233 | + ServiceProvider bookService = createSimpleMockService("bookService", |
| 234 | + ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, bookSchema, bookResponse) |
| 235 | + |
| 236 | + |
| 237 | + ServiceProvider authorService = createQueryMatchingService("authorService", |
| 238 | + ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, authorSchema, authorResponse) |
| 239 | + |
| 240 | + |
| 241 | + ServiceProvider[] services = [bookService, authorService] |
| 242 | + specUnderTest = createGraphQLOrchestrator(services) |
| 243 | + |
| 244 | + when: |
| 245 | + ExecutionInput booksAndPetsEI = ExecutionInput.newExecutionInput().query(testQuery) |
| 246 | + .variables(ImmutableMap.of("includeType", Boolean.TRUE)) |
| 247 | + .build() |
| 248 | + |
| 249 | + Map<String, Object> executionResult = specUnderTest.execute(booksAndPetsEI).get().toSpecification() |
| 250 | + |
| 251 | + then: |
| 252 | + executionResult.get("errors") == null |
| 253 | + executionResult.get("data") != null |
| 254 | + |
| 255 | + executionResult?.data?.bookById?.id == "book-1" |
| 256 | + executionResult?.data?.bookById?.name == "GraphQuilt: The future of Schema Stitching" |
| 257 | + executionResult?.data?.bookById?.pageCount == 100 |
| 258 | + |
| 259 | + executionResult?.data?.bookById?.author.idAlias == "12345" |
| 260 | + executionResult?.data?.bookById?.author.firstName == "Charles" |
| 261 | + executionResult?.data?.bookById?.author.lastName == "Charles-LastName" |
| 262 | + |
| 263 | + Map<String, Object> dataValue = (Map<String, Object>) executionResult.get("data") |
| 264 | + dataValue.keySet().containsAll("bookById") |
| 265 | + ((Map<String, Objects>) dataValue.get("bookById")).keySet().size() == 4 |
| 266 | + } |
| 267 | + |
| 268 | +} |
0 commit comments