|
| 1 | +package com.intuit.graphql.orchestrator.integration.authzpolicy |
| 2 | + |
| 3 | +import com.intuit.graphql.orchestrator.ServiceProvider |
| 4 | +import com.intuit.graphql.orchestrator.datafetcher.ServiceDataFetcher |
| 5 | +import com.intuit.graphql.orchestrator.schema.Operation |
| 6 | +import com.intuit.graphql.orchestrator.stitching.StitchingException |
| 7 | +import graphql.schema.FieldCoordinates |
| 8 | +import graphql.schema.GraphQLCodeRegistry |
| 9 | +import graphql.schema.GraphQLSchema |
| 10 | +import graphql.schema.StaticDataFetcher |
| 11 | +import helpers.BaseIntegrationTestSpecification |
| 12 | +import spock.lang.Subject |
| 13 | + |
| 14 | +import static com.intuit.graphql.orchestrator.TestHelper.getResourceAsString |
| 15 | +import static graphql.Scalars.GraphQLInt |
| 16 | + |
| 17 | +class AuthzPolicyWithNestedLevelStitchingSpec extends BaseIntegrationTestSpecification { |
| 18 | + |
| 19 | + def v4osService, turboService |
| 20 | + |
| 21 | + def mockServiceResponse = new HashMap() |
| 22 | + |
| 23 | + def conflictingSchema = """ |
| 24 | + type Query { |
| 25 | + root: RootType |
| 26 | + topLevelField: NestedType @authzPolicy(ruleInput: [{key:"foo",value:"a"}]) |
| 27 | + } |
| 28 | +
|
| 29 | + type RootType { |
| 30 | + nestedField: NestedType |
| 31 | + } |
| 32 | + |
| 33 | + type NestedType { |
| 34 | + fieldA: String @authzPolicy(ruleInput: [{key:"foo",value:"b"}]) |
| 35 | + fieldB: String @authzPolicy(ruleInput: [{key:"foo",value:"b"}]) |
| 36 | + } |
| 37 | + """ |
| 38 | + |
| 39 | + def conflictingSchema2 = """ |
| 40 | + type Query { |
| 41 | + root: RootType |
| 42 | + } |
| 43 | +
|
| 44 | + type RootType { |
| 45 | + nestedField: NestedType |
| 46 | + } |
| 47 | + |
| 48 | + type NestedType { |
| 49 | + fieldC: String @authzPolicy(ruleInput: [{key:"foo",value:"b"}]) |
| 50 | + fieldD: String @authzPolicy(ruleInput: [{key:"foo",value:"c"}]) |
| 51 | + } |
| 52 | + """ |
| 53 | + ServiceProvider topLevelDeprecatedService1 |
| 54 | + ServiceProvider topLevelDeprecatedService2 |
| 55 | + |
| 56 | + |
| 57 | + @Subject |
| 58 | + def specUnderTest |
| 59 | + |
| 60 | + def "Test Nested Stitching"() { |
| 61 | + given: |
| 62 | + v4osService = createSimpleMockService("V4OS", getResourceAsString("nested/v4os/schema.graphqls"), mockServiceResponse) |
| 63 | + turboService = createSimpleMockService("TURBO", getResourceAsString("nested/turbo/schema.graphqls"), mockServiceResponse) |
| 64 | + |
| 65 | + when: |
| 66 | + specUnderTest = createGraphQLOrchestrator([v4osService, turboService]) |
| 67 | + |
| 68 | + then: |
| 69 | + GraphQLSchema result = specUnderTest.schema |
| 70 | + |
| 71 | + def consumer = result?.getQueryType()?.getFieldDefinition("consumer") |
| 72 | + consumer != null |
| 73 | + |
| 74 | + def consumerType = result?.getQueryType()?.getFieldDefinition("consumer")?.type |
| 75 | + consumerType != null |
| 76 | + consumerType.name == "ConsumerType" |
| 77 | + consumerType.description == "[V4OS,TURBO]" |
| 78 | + |
| 79 | + def financialProfile = consumerType?.getFieldDefinition("financialProfile") |
| 80 | + financialProfile != null |
| 81 | + financialProfile.type?.name == "FinancialProfileType" |
| 82 | + |
| 83 | + def turboExperiences = consumerType?.getFieldDefinition("turboExperiences") |
| 84 | + turboExperiences != null |
| 85 | + turboExperiences.type?.name == "ExperienceType" |
| 86 | + |
| 87 | + def financeField = consumerType?.getFieldDefinition("finance") |
| 88 | + financeField != null |
| 89 | + financeField.type?.name == "FinanceType" |
| 90 | + financeField.type?.getFieldDefinition("fieldFinance")?.type == GraphQLInt |
| 91 | + financeField.type?.getFieldDefinition("fieldTurbo")?.type == GraphQLInt |
| 92 | + |
| 93 | + //DataFetchers |
| 94 | + final GraphQLCodeRegistry codeRegistry = specUnderTest.runtimeGraph.getCodeRegistry().build() |
| 95 | + codeRegistry?.getDataFetcher(FieldCoordinates.coordinates("Query", "consumer"), consumer) instanceof StaticDataFetcher |
| 96 | + codeRegistry?.getDataFetcher(FieldCoordinates.coordinates("ConsumerType", "finance"), financeField) instanceof StaticDataFetcher |
| 97 | + codeRegistry?.getDataFetcher(FieldCoordinates.coordinates("ConsumerType", "financialProfile"), financeField) instanceof ServiceDataFetcher |
| 98 | + codeRegistry?.getDataFetcher(FieldCoordinates.coordinates("ConsumerType", "turboExperiences"), turboExperiences) instanceof ServiceDataFetcher |
| 99 | + } |
| 100 | + |
| 101 | + def "Nested Type Description With Namespace And Empty Description"() { |
| 102 | + given: |
| 103 | + def bSchema = "schema { query: Query } type Query { a: A } \"\n" +\ |
| 104 | + " \"type A { b: B @adapter(service: 'foo') } type B {d: D}\"\n" +\ |
| 105 | + " \"type D { field: String}\"\n" +\ |
| 106 | + " \"directive @adapter(service:String!) on FIELD_DEFINITION" |
| 107 | + |
| 108 | + def bbSchema = "schema { query: Query } type Query { a: A } \"\n" +\ |
| 109 | + " \"type A { bbc: BB } type BB {cc: String}" |
| 110 | + |
| 111 | + def abcSchema = "schema { query: Query } type Query { a: A } \"\n" +\ |
| 112 | + " \"type A { bbcd: C } type C {cc: String}" |
| 113 | + |
| 114 | + def secondSchema = "schema { query: Query } type Query { a: A } " +\ |
| 115 | + "type A { bbbb: BAB } type BAB {fieldBB: String}" |
| 116 | + |
| 117 | + def ambcSchema = "schema { query: Query } type Query { a: A } \"\n" +\ |
| 118 | + " \"type A { bba: CDD } type CDD {ccdd: String}" |
| 119 | + |
| 120 | + def ttbbSchema = "schema { query: Query } type Query { a: A } \"\n" +\ |
| 121 | + " \"type A { bbab: BBD } type BBD {cc: String}" |
| 122 | + |
| 123 | + def bService = createSimpleMockService("SVC_b", bSchema, mockServiceResponse) |
| 124 | + def bbService = createSimpleMockService("SVC_bb", bbSchema, mockServiceResponse) |
| 125 | + def abcService = createSimpleMockService("SVC_abc", abcSchema, mockServiceResponse) |
| 126 | + def secondService = createSimpleMockService("SVC_Second", secondSchema, mockServiceResponse) |
| 127 | + def ambcService = createSimpleMockService("AMBC", ambcSchema, mockServiceResponse) |
| 128 | + def ttbbService = createSimpleMockService("TTBB", ttbbSchema, mockServiceResponse) |
| 129 | + |
| 130 | + when: |
| 131 | + specUnderTest = createGraphQLOrchestrator([bService, bbService ,abcService,secondService, ambcService, ttbbService]) |
| 132 | + |
| 133 | + |
| 134 | + then: |
| 135 | + def aType = specUnderTest.runtimeGraph.getOperation(Operation.QUERY)?.getFieldDefinition("a")?.type |
| 136 | + |
| 137 | + aType.description.contains("SVC_abc") |
| 138 | + aType.description.contains("SVC_bb") |
| 139 | + aType.description.contains("AMBC") |
| 140 | + aType.description.contains("TTBB") |
| 141 | + aType.description.contains("SVC_Second") |
| 142 | + aType.description.contains("SVC_b") |
| 143 | + } |
| 144 | + |
| 145 | + def "Nested Type Description With Namespace And Description"() { |
| 146 | + given: |
| 147 | + String schema1 = "schema { query: Query } type Query { a: A } " +\ |
| 148 | + "type A { b: B @adapter(service: 'foo') } type B {d: D}" +\ |
| 149 | + "type D { field: String}" +\ |
| 150 | + "directive @adapter(service:String!) on FIELD_DEFINITION" |
| 151 | + |
| 152 | + String schema2 = "schema { query: Query } type Query { a: A } " +\ |
| 153 | + "\"description for schema2\"type A { bbc: BB } type BB {cc: String}" |
| 154 | + |
| 155 | + String schema3 = "schema { query: Query } type Query { a: A } " +\ |
| 156 | + "\"description for schema3\"type A { bbcd: C } type C {cc: String}" |
| 157 | + |
| 158 | + String schema4 = "schema { query: Query } type Query { a: A } " +\ |
| 159 | + "type A { bbbb: BAB } type BAB {fieldBB: String}" |
| 160 | + |
| 161 | + def service1 = createSimpleMockService("SVC_b", schema1, mockServiceResponse) |
| 162 | + def service2 = createSimpleMockService("SVC_bb", schema2, mockServiceResponse) |
| 163 | + def service3 = createSimpleMockService("SVC_abc", schema3, mockServiceResponse) |
| 164 | + def service4 = createSimpleMockService("SVC_Second", schema4, mockServiceResponse) |
| 165 | + |
| 166 | + when: |
| 167 | + specUnderTest = createGraphQLOrchestrator([service1, service2, service3, service4]) |
| 168 | + |
| 169 | + then: |
| 170 | + def aType = specUnderTest?.runtimeGraph?.getOperation(Operation.QUERY)?.getFieldDefinition("a")?.type |
| 171 | + |
| 172 | + aType.description.contains("SVC_abc") |
| 173 | + aType.description.contains("SVC_bb") |
| 174 | + aType.description.contains("SVC_Second") |
| 175 | + aType.description.contains("SVC_b") |
| 176 | + aType.description.contains("description for schema3") |
| 177 | + aType.description.contains("description for schema2") |
| 178 | + } |
| 179 | + |
| 180 | + def "deprecated fields can not be referenced again"() { |
| 181 | + given: |
| 182 | + topLevelDeprecatedService1 = createSimpleMockService("test1", conflictingSchema, new HashMap<String, Object>()) |
| 183 | + topLevelDeprecatedService2 = createSimpleMockService("test2", conflictingSchema2, new HashMap<String, Object>()) |
| 184 | + |
| 185 | + when: |
| 186 | + specUnderTest = createGraphQLOrchestrator([topLevelDeprecatedService1, topLevelDeprecatedService2]) |
| 187 | + |
| 188 | + then: |
| 189 | + def exception = thrown(StitchingException) |
| 190 | + exception.message == "FORBIDDEN: Subgraphs [test2,test1] are reusing type NestedType with different field definitions." |
| 191 | + } |
| 192 | +} |
0 commit comments