|
| 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 FederationQueryWithFragmentSpec extends BaseIntegrationTestSpecification { |
| 10 | + |
| 11 | + GraphQLOrchestrator specUnderTest |
| 12 | + |
| 13 | + private def bookSchema = ''' |
| 14 | + type Query { |
| 15 | + bookById(id: ID): Book |
| 16 | + } |
| 17 | +
|
| 18 | + type Book { |
| 19 | + id: ID |
| 20 | + name: String |
| 21 | + pageCount: Int |
| 22 | + author: Author |
| 23 | + } |
| 24 | +
|
| 25 | + type Author @key(fields: "id") { |
| 26 | + id: String |
| 27 | + } |
| 28 | + ''' |
| 29 | + |
| 30 | + private def authorSchema = ''' |
| 31 | + type Query { |
| 32 | + authorByid(id: ID!): Author |
| 33 | + } |
| 34 | +
|
| 35 | + type Author @extends @key(fields: "id") { |
| 36 | + id: String @external |
| 37 | + firstName: String! |
| 38 | + lastName: String! |
| 39 | + } |
| 40 | + ''' |
| 41 | + |
| 42 | + def setup() { |
| 43 | + } |
| 44 | + |
| 45 | + def "Query multiple field of entity extension with fragment and inline fragment"() { |
| 46 | + given: |
| 47 | + |
| 48 | + def testQuery = ''' |
| 49 | + query { |
| 50 | + bookById(id: "12345") { |
| 51 | + id |
| 52 | + name |
| 53 | + pageCount |
| 54 | + author { |
| 55 | + ... authorBaseQuery |
| 56 | + ... on Author { |
| 57 | + lastName |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + fragment authorBaseQuery on Author { |
| 64 | + idAlias:id |
| 65 | + fname:firstName |
| 66 | + } |
| 67 | + ''' |
| 68 | + |
| 69 | + def bookResponse = [ |
| 70 | + data: [ |
| 71 | + bookById: [ |
| 72 | + id : "book-1", |
| 73 | + name : "GraphQuilt: The future of Schema Stitching", |
| 74 | + pageCount: 100, |
| 75 | + author : ["idAlias": "12345"] |
| 76 | + ] |
| 77 | + |
| 78 | + ] |
| 79 | + ] |
| 80 | + |
| 81 | + def FNAME_ENTITY_FETCH_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {fname:firstName}}}" |
| 82 | + def LNAME_ENTITY_FETCH_QUERY = "query (\$REPRESENTATIONS:[_Any!]!) {_entities(representations:\$REPRESENTATIONS) {... on Author {lastName}}}" |
| 83 | + def authorResponse = [ |
| 84 | + (FNAME_ENTITY_FETCH_QUERY): [data: [ |
| 85 | + _entities: [ |
| 86 | + [ |
| 87 | + fname : "Charles" |
| 88 | + ] |
| 89 | + ] |
| 90 | + ]], |
| 91 | + (LNAME_ENTITY_FETCH_QUERY): [data: [ |
| 92 | + _entities: [ |
| 93 | + [ |
| 94 | + lastName : "Charles-LastName" |
| 95 | + ] |
| 96 | + ] |
| 97 | + ]] |
| 98 | + |
| 99 | + ] |
| 100 | + |
| 101 | + ServiceProvider bookService = createSimpleMockService("bookService", |
| 102 | + ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, bookSchema, bookResponse) |
| 103 | + |
| 104 | + |
| 105 | + ServiceProvider authorService = createQueryMatchingService("authorService", |
| 106 | + ServiceProvider.ServiceType.FEDERATION_SUBGRAPH, authorSchema, authorResponse) |
| 107 | + |
| 108 | + |
| 109 | + ServiceProvider[] services = [bookService, authorService] |
| 110 | + specUnderTest = createGraphQLOrchestrator(services) |
| 111 | + |
| 112 | + when: |
| 113 | + ExecutionInput booksAndPetsEI = ExecutionInput.newExecutionInput().query(testQuery) |
| 114 | + .variables(ImmutableMap.of("includeType", Boolean.TRUE)) |
| 115 | + .build() |
| 116 | + |
| 117 | + Map<String, Object> executionResult = specUnderTest.execute(booksAndPetsEI).get().toSpecification() |
| 118 | + |
| 119 | + then: |
| 120 | + executionResult.get("errors") == null |
| 121 | + executionResult.get("data") != null |
| 122 | + |
| 123 | + executionResult?.data?.bookById?.id == "book-1" |
| 124 | + executionResult?.data?.bookById?.name == "GraphQuilt: The future of Schema Stitching" |
| 125 | + executionResult?.data?.bookById?.pageCount == 100 |
| 126 | + |
| 127 | + executionResult?.data?.bookById?.author.idAlias == "12345" |
| 128 | + executionResult?.data?.bookById?.author.fname == "Charles" |
| 129 | + executionResult?.data?.bookById?.author.lastName == "Charles-LastName" |
| 130 | + |
| 131 | + Map<String, Object> dataValue = (Map<String, Object>) executionResult.get("data") |
| 132 | + dataValue.keySet().containsAll("bookById") |
| 133 | + ((Map<String, Objects>) dataValue.get("bookById")).keySet().size() == 4 |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments