|
| 1 | +import { |
| 2 | + Field, |
| 3 | + FieldDefinition, |
| 4 | + Schema, |
| 5 | + assert, |
| 6 | + buildSupergraphSchema, |
| 7 | +} from "@apollo/federation-internals"; |
| 8 | +import { |
| 9 | + GraphPath, |
| 10 | + OpGraphPath, |
| 11 | + SimultaneousPathsWithLazyIndirectPaths, |
| 12 | + advanceSimultaneousPathsWithOperation, |
| 13 | + createInitialOptions |
| 14 | +} from "../graphPath"; |
| 15 | +import { QueryGraph, Vertex, buildFederatedQueryGraph } from "../querygraph"; |
| 16 | +import { emptyContext } from "../pathContext"; |
| 17 | +import { simpleValidationConditionResolver } from "../conditionsValidation"; |
| 18 | + |
| 19 | +function parseSupergraph(subgraphs: number, schema: string): { supergraph: Schema, api: Schema, queryGraph: QueryGraph } { |
| 20 | + assert(subgraphs >= 1, 'Should have at least 1 subgraph'); |
| 21 | + const header = ` |
| 22 | + schema |
| 23 | + @link(url: "https://specs.apollo.dev/link/v1.0") |
| 24 | + @link(url: "https://specs.apollo.dev/join/v0.3", for: EXECUTION) |
| 25 | + { |
| 26 | + query: Query |
| 27 | + } |
| 28 | +
|
| 29 | + directive @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE |
| 30 | + directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION |
| 31 | + directive @join__graph(name: String!, url: String!) on ENUM_VALUE |
| 32 | + directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE |
| 33 | + directive @join__type(graph: join__Graph!, key: join__FieldSet, extension: Boolean! = false, resolvable: Boolean! = true, isInterfaceObject: Boolean! = false) repeatable on OBJECT | INTERFACE | UNION | ENUM | INPUT_OBJECT | SCALAR |
| 34 | + directive @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION |
| 35 | + directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA |
| 36 | +
|
| 37 | + scalar join__FieldSet |
| 38 | +
|
| 39 | + scalar link__Import |
| 40 | +
|
| 41 | + enum link__Purpose { |
| 42 | + SECURITY |
| 43 | + EXECUTION |
| 44 | + } |
| 45 | +
|
| 46 | + enum join__Graph { |
| 47 | + ${[...Array(subgraphs).keys()].map((n) => `S${n+1} @join__graph(name: "S${n+1}", url: "https://S${n+1}")`).join('\n')} |
| 48 | + } |
| 49 | +
|
| 50 | + `; |
| 51 | + |
| 52 | + try { |
| 53 | + const supergraph = buildSupergraphSchema(header + schema)[0]; |
| 54 | + return { |
| 55 | + supergraph, |
| 56 | + api: supergraph.toAPISchema(), |
| 57 | + queryGraph: buildFederatedQueryGraph(supergraph, true), |
| 58 | + }; |
| 59 | + } catch (e) { |
| 60 | + throw new Error('Error parsing supergraph schema:\n' + e.toString()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function createOptions(supergraph: Schema, queryGraph: QueryGraph): SimultaneousPathsWithLazyIndirectPaths<Vertex>[] { |
| 65 | + // We know we only use `Query` in the supergraph, so there is only that as root. |
| 66 | + const root = queryGraph.roots()[0]; |
| 67 | + const initialPath: OpGraphPath<Vertex> = GraphPath.create(queryGraph, root); |
| 68 | + return createInitialOptions( |
| 69 | + initialPath, |
| 70 | + emptyContext, |
| 71 | + simpleValidationConditionResolver({ supergraph, queryGraph }), |
| 72 | + [], |
| 73 | + [], |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +function field(schema: Schema, coordinate: string): Field { |
| 78 | + const def = schema.elementByCoordinate(coordinate) as FieldDefinition<any>; |
| 79 | + return new Field(def); |
| 80 | +} |
| 81 | + |
| 82 | +describe("advanceSimultaneousPathsWithOperation", () => { |
| 83 | + test("do not use key `x` to fetch `x`", () => { |
| 84 | + const { supergraph, api, queryGraph } = parseSupergraph(3, ` |
| 85 | + type Query |
| 86 | + @join__type(graph: S1) |
| 87 | + { |
| 88 | + t: T @join__field(graph: S1) |
| 89 | + } |
| 90 | +
|
| 91 | + type T |
| 92 | + @join__type(graph: S1) |
| 93 | + @join__type(graph: S2, key: "otherId") |
| 94 | + @join__type(graph: S2, key: "id") |
| 95 | + @join__type(graph: S3, key: "id") |
| 96 | + { |
| 97 | + otherId: ID! @join__field(graph: S1) @join__field(graph: S2) |
| 98 | + id: ID! @join__field(graph: S2) @join__field(graph: S3) |
| 99 | + } |
| 100 | + `); |
| 101 | + |
| 102 | + // Picking the first initial, the one going to S1 |
| 103 | + const initial = createOptions(supergraph, queryGraph)[0]; |
| 104 | + |
| 105 | + // Then picking `t`, which should be just the one option of picking it in S1 at this point. |
| 106 | + const allAfterT = advanceSimultaneousPathsWithOperation(supergraph, initial, field(api, "Query.t")); |
| 107 | + assert(allAfterT, 'Should have advanced correctly'); |
| 108 | + expect(allAfterT).toHaveLength(1); |
| 109 | + const afterT = allAfterT[0]; |
| 110 | + expect(afterT.toString()).toBe(`Query(S1) --[t]--> T(S1) (types: [T])`); |
| 111 | + |
| 112 | + // Checking that, at this point, we technically have 2 options: |
| 113 | + // 1. we can go to S2 using `otherId`. |
| 114 | + // 2. we can go to S3 using `id`, assuming we first get `id` from S2 (using `otherId`). |
| 115 | + const indirect = afterT.indirectOptions(afterT.context, 0); |
| 116 | + expect(indirect.paths).toHaveLength(2); |
| 117 | + expect(indirect.paths[0].toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ otherId } ⊢ key()]--> T(S2) (types: [T])`); |
| 118 | + expect(indirect.paths[1].toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ id } ⊢ key()]--> T(S3) (types: [T])`); |
| 119 | + |
| 120 | + const allForId = advanceSimultaneousPathsWithOperation(supergraph, afterT, field(api, "T.id")); |
| 121 | + assert(allForId, 'Should have advanced correctly'); |
| 122 | + |
| 123 | + // Here, `id` is a direct path from both of our indirect paths. However, it makes no sense to use the 2nd |
| 124 | + // indirect path above, since the condition to get to `S3` was `id`, and this means another indirect path |
| 125 | + // is able to get to `id` more directly (the first one in this case). |
| 126 | + // So ultimately, we should only keep the 1st option. |
| 127 | + expect(allForId).toHaveLength(1); |
| 128 | + const forId = allForId[0]; |
| 129 | + expect(forId.toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ otherId } ⊢ key()]--> T(S2) --[id]--> ID(S2)`); |
| 130 | + }); |
| 131 | + |
| 132 | + test("do not use key containing `x` to fetch `x`", () => { |
| 133 | + // Similar to the previous test, but the key used is not exactly the fetch field, it only contains |
| 134 | + // it (but the optimisation should still work). |
| 135 | + const { supergraph, api, queryGraph } = parseSupergraph(3, ` |
| 136 | + type Query |
| 137 | + @join__type(graph: S1) |
| 138 | + { |
| 139 | + t: T @join__field(graph: S1) |
| 140 | + } |
| 141 | +
|
| 142 | + type T |
| 143 | + @join__type(graph: S1) |
| 144 | + @join__type(graph: S2, key: "otherId") |
| 145 | + @join__type(graph: S2, key: "id1 id2") |
| 146 | + @join__type(graph: S3, key: "id1 id2") |
| 147 | + { |
| 148 | + otherId: ID! @join__field(graph: S1) @join__field(graph: S2) |
| 149 | + id1: ID! @join__field(graph: S2) @join__field(graph: S3) |
| 150 | + id2: ID! @join__field(graph: S2) @join__field(graph: S3) |
| 151 | + } |
| 152 | + `); |
| 153 | + |
| 154 | + // Picking the first initial, the one going to S1 |
| 155 | + const initial = createOptions(supergraph, queryGraph)[0]; |
| 156 | + |
| 157 | + // Then picking `t`, which should be just the one option of picking it in S1 at this point. |
| 158 | + const allAfterT = advanceSimultaneousPathsWithOperation(supergraph, initial, field(api, "Query.t")); |
| 159 | + assert(allAfterT, 'Should have advanced correctly'); |
| 160 | + expect(allAfterT).toHaveLength(1); |
| 161 | + const afterT = allAfterT[0]; |
| 162 | + expect(afterT.toString()).toBe(`Query(S1) --[t]--> T(S1) (types: [T])`); |
| 163 | + |
| 164 | + // Checking that, at this point, we technically have 2 options: |
| 165 | + // 1. we can go to S2 using `otherId`. |
| 166 | + // 2. we can go to S3 using `id1 id2`, assuming we first get `id1 id2` from S2 (using `otherId`). |
| 167 | + const indirect = afterT.indirectOptions(afterT.context, 0); |
| 168 | + expect(indirect.paths).toHaveLength(2); |
| 169 | + expect(indirect.paths[0].toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ otherId } ⊢ key()]--> T(S2) (types: [T])`); |
| 170 | + expect(indirect.paths[1].toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ id1 id2 } ⊢ key()]--> T(S3) (types: [T])`); |
| 171 | + |
| 172 | + const allForId = advanceSimultaneousPathsWithOperation(supergraph, afterT, field(api, "T.id1")); |
| 173 | + assert(allForId, 'Should have advanced correctly'); |
| 174 | + |
| 175 | + // Here, `id1` is a direct path from both of our indirect paths. However, it makes no sense to use the 2nd |
| 176 | + // indirect path above, since the condition to get to `S3` was `id1 id2`, which includes `id1`. |
| 177 | + expect(allForId).toHaveLength(1); |
| 178 | + const forId = allForId[0]; |
| 179 | + expect(forId.toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ otherId } ⊢ key()]--> T(S2) --[id1]--> ID(S2)`); |
| 180 | + }); |
| 181 | + |
| 182 | + test("avoids indirect path that needs a key to the same subgraph to validate its condition", () => { |
| 183 | + const { supergraph, api, queryGraph } = parseSupergraph(2, ` |
| 184 | + type Query |
| 185 | + @join__type(graph: S1) |
| 186 | + { |
| 187 | + t: T @join__field(graph: S1) |
| 188 | + } |
| 189 | +
|
| 190 | + type T |
| 191 | + @join__type(graph: S1) |
| 192 | + @join__type(graph: S2, key: "id1") |
| 193 | + @join__type(graph: S2, key: "id2") |
| 194 | + { |
| 195 | + id1: ID! @join__field(graph: S2) |
| 196 | + id2: ID! @join__field(graph: S1) @join__field(graph: S2) |
| 197 | + } |
| 198 | + `); |
| 199 | + |
| 200 | + // Picking the first initial, the one going to S1 |
| 201 | + const initial = createOptions(supergraph, queryGraph)[0]; |
| 202 | + |
| 203 | + // Then picking `t`, which should be just the one option of picking it in S1 at this point. |
| 204 | + const allAfterT = advanceSimultaneousPathsWithOperation(supergraph, initial, field(api, "Query.t")); |
| 205 | + assert(allAfterT, 'Should have advanced correctly'); |
| 206 | + expect(allAfterT).toHaveLength(1); |
| 207 | + const afterT = allAfterT[0]; |
| 208 | + expect(afterT.toString()).toBe(`Query(S1) --[t]--> T(S1) (types: [T])`); |
| 209 | + |
| 210 | + // Technically, the `id1` key could be used to go to S2 by first getting `id1` from S2 using `id2`, but |
| 211 | + // that's obviously unecessary to consider since we can just use `id2` to go to S2 in the first place. |
| 212 | + const indirect = afterT.indirectOptions(afterT.context, 0); |
| 213 | + expect(indirect.paths).toHaveLength(1); |
| 214 | + expect(indirect.paths[0].toString()).toBe(`Query(S1) --[t]--> T(S1) --[{ id2 } ⊢ key()]--> T(S2) (types: [T])`); |
| 215 | + }); |
| 216 | +}); |
0 commit comments