|
1 | 1 | import { composeServices } from "../compose"; |
2 | | -import { printSchema } from "@apollo/federation-internals"; |
| 2 | +import { buildSubgraph, printSchema } from "@apollo/federation-internals"; |
3 | 3 | import { parse } from "graphql/index"; |
4 | 4 |
|
5 | 5 | describe("connect spec and join__directive", () => { |
6 | | - it("composes", () => { |
7 | | - const subgraphs = [ |
8 | | - { |
9 | | - name: "with-connectors", |
10 | | - typeDefs: parse(` |
11 | | - extend schema |
12 | | - @link( |
13 | | - url: "https://specs.apollo.dev/federation/v2.10" |
14 | | - import: ["@key"] |
15 | | - ) |
16 | | - @link( |
17 | | - url: "https://specs.apollo.dev/connect/v0.1" |
18 | | - import: ["@connect", "@source"] |
19 | | - ) |
20 | | - @source(name: "v1", http: { baseURL: "http://v1" }) |
| 6 | + const subgraphSdl = ` |
| 7 | + extend schema |
| 8 | + @link( |
| 9 | + url: "https://specs.apollo.dev/federation/v2.10" |
| 10 | + import: ["@key"] |
| 11 | + ) |
| 12 | + @link( |
| 13 | + url: "https://specs.apollo.dev/connect/v0.1" |
| 14 | + import: ["@connect", "@source"] |
| 15 | + ) |
| 16 | + @source(name: "v1", http: { baseURL: "http://v1" }) |
| 17 | +
|
| 18 | + type Query { |
| 19 | + resources: [Resource!]! |
| 20 | + @connect(source: "v1", http: { GET: "/resources" }, selection: "") |
| 21 | + } |
21 | 22 |
|
22 | | - type Query { |
23 | | - resources: [Resource!]! |
24 | | - @connect(source: "v1", http: { GET: "/resources" }, selection: "") |
25 | | - } |
| 23 | + type Resource @key(fields: "id") { |
| 24 | + id: ID! |
| 25 | + name: String! |
| 26 | + } |
| 27 | + `; |
| 28 | + |
| 29 | + const expectedSupergraphSdl = ` |
| 30 | + "schema |
| 31 | + @link(url: \\"https://specs.apollo.dev/link/v1.0\\") |
| 32 | + @link(url: \\"https://specs.apollo.dev/join/v0.5\\", for: EXECUTION) |
| 33 | + @link(url: \\"https://specs.apollo.dev/connect/v0.2\\", for: EXECUTION) |
| 34 | + @join__directive(graphs: [WITH_CONNECTORS], name: \\"link\\", args: {url: \\"https://specs.apollo.dev/connect/v0.1\\", import: [\\"@connect\\", \\"@source\\"]}) |
| 35 | + @join__directive(graphs: [WITH_CONNECTORS], name: \\"source\\", args: {name: \\"v1\\", http: {baseURL: \\"http://v1\\"}}) |
| 36 | + { |
| 37 | + query: Query |
| 38 | + } |
26 | 39 |
|
27 | | - type Resource @key(fields: "id") { |
28 | | - id: ID! |
29 | | - name: String! |
30 | | - } |
31 | | - `), |
32 | | - }, |
33 | | - ]; |
| 40 | + directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA |
34 | 41 |
|
35 | | - const result = composeServices(subgraphs); |
36 | | - expect(result.errors ?? []).toEqual([]); |
37 | | - const printed = printSchema(result.schema!); |
38 | | - expect(printed).toMatchInlineSnapshot(` |
39 | | - "schema |
40 | | - @link(url: \\"https://specs.apollo.dev/link/v1.0\\") |
41 | | - @link(url: \\"https://specs.apollo.dev/join/v0.5\\", for: EXECUTION) |
42 | | - @link(url: \\"https://specs.apollo.dev/connect/v0.2\\", for: EXECUTION) |
43 | | - @join__directive(graphs: [WITH_CONNECTORS], name: \\"link\\", args: {url: \\"https://specs.apollo.dev/connect/v0.1\\", import: [\\"@connect\\", \\"@source\\"]}) |
44 | | - @join__directive(graphs: [WITH_CONNECTORS], name: \\"source\\", args: {name: \\"v1\\", http: {baseURL: \\"http://v1\\"}}) |
45 | | - { |
46 | | - query: Query |
47 | | - } |
| 42 | + directive @join__graph(name: String!, url: String!) on ENUM_VALUE |
48 | 43 |
|
49 | | - directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA |
| 44 | + 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 |
50 | 45 |
|
51 | | - directive @join__graph(name: String!, url: String!) on ENUM_VALUE |
| 46 | + directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean, overrideLabel: String, contextArguments: [join__ContextArgument!]) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION |
52 | 47 |
|
53 | | - 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 |
| 48 | + directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE |
54 | 49 |
|
55 | | - directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean, overrideLabel: String, contextArguments: [join__ContextArgument!]) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION |
| 50 | + directive @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION |
56 | 51 |
|
57 | | - directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE |
| 52 | + directive @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE |
58 | 53 |
|
59 | | - directive @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION |
| 54 | + directive @join__directive(graphs: [join__Graph!], name: String!, args: join__DirectiveArguments) repeatable on SCHEMA | OBJECT | INTERFACE | FIELD_DEFINITION |
60 | 55 |
|
61 | | - directive @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE |
| 56 | + enum link__Purpose { |
| 57 | + \\"\\"\\" |
| 58 | + \`SECURITY\` features provide metadata necessary to securely resolve fields. |
| 59 | + \\"\\"\\" |
| 60 | + SECURITY |
62 | 61 |
|
63 | | - directive @join__directive(graphs: [join__Graph!], name: String!, args: join__DirectiveArguments) repeatable on SCHEMA | OBJECT | INTERFACE | FIELD_DEFINITION |
| 62 | + \\"\\"\\" |
| 63 | + \`EXECUTION\` features provide metadata necessary for operation execution. |
| 64 | + \\"\\"\\" |
| 65 | + EXECUTION |
| 66 | + } |
64 | 67 |
|
65 | | - enum link__Purpose { |
66 | | - \\"\\"\\" |
67 | | - \`SECURITY\` features provide metadata necessary to securely resolve fields. |
68 | | - \\"\\"\\" |
69 | | - SECURITY |
| 68 | + scalar link__Import |
70 | 69 |
|
71 | | - \\"\\"\\" |
72 | | - \`EXECUTION\` features provide metadata necessary for operation execution. |
73 | | - \\"\\"\\" |
74 | | - EXECUTION |
75 | | - } |
| 70 | + enum join__Graph { |
| 71 | + WITH_CONNECTORS @join__graph(name: \\"with-connectors\\", url: \\"\\") |
| 72 | + } |
76 | 73 |
|
77 | | - scalar link__Import |
| 74 | + scalar join__FieldSet |
78 | 75 |
|
79 | | - enum join__Graph { |
80 | | - WITH_CONNECTORS @join__graph(name: \\"with-connectors\\", url: \\"\\") |
81 | | - } |
| 76 | + scalar join__DirectiveArguments |
82 | 77 |
|
83 | | - scalar join__FieldSet |
| 78 | + scalar join__FieldValue |
84 | 79 |
|
85 | | - scalar join__DirectiveArguments |
| 80 | + input join__ContextArgument { |
| 81 | + name: String! |
| 82 | + type: String! |
| 83 | + context: String! |
| 84 | + selection: join__FieldValue! |
| 85 | + } |
86 | 86 |
|
87 | | - scalar join__FieldValue |
| 87 | + type Query |
| 88 | + @join__type(graph: WITH_CONNECTORS) |
| 89 | + { |
| 90 | + resources: [Resource!]! @join__directive(graphs: [WITH_CONNECTORS], name: \\"connect\\", args: {source: \\"v1\\", http: {GET: \\"/resources\\"}, selection: \\"\\"}) |
| 91 | + } |
88 | 92 |
|
89 | | - input join__ContextArgument { |
90 | | - name: String! |
91 | | - type: String! |
92 | | - context: String! |
93 | | - selection: join__FieldValue! |
94 | | - } |
| 93 | + type Resource |
| 94 | + @join__type(graph: WITH_CONNECTORS, key: \\"id\\") |
| 95 | + { |
| 96 | + id: ID! |
| 97 | + name: String! |
| 98 | + }" |
| 99 | + `; |
| 100 | + |
| 101 | + const expectedApiSdl = ` |
| 102 | + "type Query { |
| 103 | + resources: [Resource!]! |
| 104 | + } |
95 | 105 |
|
96 | | - type Query |
97 | | - @join__type(graph: WITH_CONNECTORS) |
98 | | - { |
99 | | - resources: [Resource!]! @join__directive(graphs: [WITH_CONNECTORS], name: \\"connect\\", args: {source: \\"v1\\", http: {GET: \\"/resources\\"}, selection: \\"\\"}) |
100 | | - } |
| 106 | + type Resource { |
| 107 | + id: ID! |
| 108 | + name: String! |
| 109 | + }" |
| 110 | + `; |
101 | 111 |
|
102 | | - type Resource |
103 | | - @join__type(graph: WITH_CONNECTORS, key: \\"id\\") |
| 112 | + it("composes", () => { |
| 113 | + const subgraphs = [ |
104 | 114 | { |
105 | | - id: ID! |
106 | | - name: String! |
107 | | - }" |
108 | | - `); |
| 115 | + name: "with-connectors", |
| 116 | + typeDefs: parse(subgraphSdl), |
| 117 | + }, |
| 118 | + ]; |
| 119 | + |
| 120 | + const result = composeServices(subgraphs); |
| 121 | + expect(result.errors ?? []).toEqual([]); |
| 122 | + const printed = printSchema(result.schema!); |
| 123 | + expect(printed).toMatchInlineSnapshot(expectedSupergraphSdl); |
109 | 124 |
|
110 | 125 | if (result.schema) { |
111 | | - expect(printSchema(result.schema.toAPISchema())).toMatchInlineSnapshot(` |
112 | | - "type Query { |
113 | | - resources: [Resource!]! |
114 | | - } |
| 126 | + expect(printSchema(result.schema.toAPISchema())) |
| 127 | + .toMatchInlineSnapshot(expectedApiSdl); |
| 128 | + } |
| 129 | + }); |
115 | 130 |
|
116 | | - type Resource { |
117 | | - id: ID! |
118 | | - name: String! |
119 | | - }" |
120 | | - `); |
| 131 | + it("composes with completed definitions", () => { |
| 132 | + const completedSubgraphSdl = printSchema(buildSubgraph( |
| 133 | + "with-connectors", |
| 134 | + "", |
| 135 | + subgraphSdl, |
| 136 | + ).schema); |
| 137 | + |
| 138 | + const subgraphs = [ |
| 139 | + { |
| 140 | + name: "with-connectors", |
| 141 | + typeDefs: parse(completedSubgraphSdl), |
| 142 | + }, |
| 143 | + ]; |
| 144 | + |
| 145 | + const result = composeServices(subgraphs); |
| 146 | + expect(result.errors ?? []).toEqual([]); |
| 147 | + const printed = printSchema(result.schema!); |
| 148 | + expect(printed).toMatchInlineSnapshot(expectedSupergraphSdl); |
| 149 | + |
| 150 | + if (result.schema) { |
| 151 | + expect(printSchema(result.schema.toAPISchema())) |
| 152 | + .toMatchInlineSnapshot(expectedApiSdl); |
121 | 153 | } |
122 | 154 | }); |
123 | 155 |
|
|
0 commit comments