@apollo/federation-internals 2.4.5 → 2.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,212 +0,0 @@
1
- import { DocumentNode, GraphQLError } from "graphql";
2
- import gql from "graphql-tag";
3
- import { buildSubgraph } from "../federation";
4
- import { assert } from "../utils";
5
- import { buildSchemaFromAST } from "../buildSchema";
6
- import { removeAllCoreFeatures } from "../coreSpec";
7
- import { errorCauses } from "../error";
8
-
9
- function expectErrors(
10
- subgraphDefs: DocumentNode,
11
- expectedErrorMessages: string[],
12
- ) {
13
- let thrownError: Error | undefined = undefined;
14
- expect(() => {
15
- try {
16
- // Note: we use buildSubgraph because currently it's the only one that does auto-magic import of
17
- // directive definition, and we don't want to bother with adding the @link definition to every
18
- // example.
19
- buildSubgraph('S', '', subgraphDefs)
20
- } catch (e) {
21
- // Kind-of ugly, but if Jest has a better option, I haven't found it.
22
- thrownError = e;
23
- throw e;
24
- }
25
- }).toThrow(GraphQLError);
26
-
27
- assert(thrownError, 'Should have thrown');
28
- const causes = errorCauses(thrownError);
29
- assert(causes, 'Should have some causes');
30
- // Note: all the received message with start with "[S] <rest of message>", so the `slice` below
31
- // strips the extra prefix. This avoid leaking the subgraph name to leak to the tests themselves.
32
- expect(causes.map((e) => e.message.slice(4))).toStrictEqual(expectedErrorMessages);
33
- }
34
-
35
- describe('@link(import:) argument', () => {
36
- test('errors on misformed values', () => {
37
- const schema = gql`
38
- extend schema @link(
39
- url: "https://specs.apollo.dev/federation/v2.0",
40
- import: [
41
- 2,
42
- { foo: "bar" },
43
- { name: "@key", badName: "foo"},
44
- { name: 42 },
45
- { as: "bar" },
46
- ]
47
- )
48
-
49
- type Query {
50
- q: Int
51
- }
52
- `;
53
-
54
- expectErrors(schema, [
55
- 'Invalid sub-value 2 for @link(import:) argument: values should be either strings or input object values of the form { name: "<importedElement>", as: "<alias>" }.',
56
- 'Unknown field "foo" for sub-value {foo: "bar"} of @link(import:) argument.',
57
- 'Unknown field "badName" for sub-value {name: "@key", badName: "foo"} of @link(import:) argument.',
58
- 'Invalid value for the "name" field for sub-value {name: 42} of @link(import:) argument: must be a string.',
59
- 'Invalid sub-value {as: "bar"} for @link(import:) argument: missing mandatory "name" field.',
60
- ]);
61
- });
62
-
63
- test('errors on mismatch between name and alias', () => {
64
- const schema = gql`
65
- extend schema @link(
66
- url: "https://specs.apollo.dev/federation/v2.0",
67
- import: [
68
- { name: "@key", as: "myKey" },
69
- { name: "FieldSet", as: "@fieldSet" },
70
- ]
71
- )
72
-
73
- type Query {
74
- q: Int
75
- }
76
- `;
77
-
78
- expectErrors(schema, [
79
- 'Invalid @link import renaming: directive "@key" imported name should start with a \'@\' character, but got "myKey".',
80
- 'Invalid @link import renaming: type "FieldSet" imported name should not start with a \'@\' character, but got "@fieldSet" (or, if @FieldSet is a directive, then it should be referred to with a \'@\').',
81
- ]);
82
- });
83
-
84
- test('errors on importing unknown elements for known features', () => {
85
- const schema = gql`
86
- extend schema @link(
87
- url: "https://specs.apollo.dev/federation/v2.0",
88
- import: [ "@foo", "key", { name: "@sharable" } ]
89
- )
90
-
91
- type Query {
92
- q: Int
93
- }
94
- `;
95
-
96
- expectErrors(schema, [
97
- 'Cannot import unknown element "@foo".',
98
- 'Cannot import unknown element "key". Did you mean directive "@key"?',
99
- 'Cannot import unknown element "@sharable\". Did you mean "@shareable"?',
100
- ]);
101
- });
102
- });
103
-
104
- describe('removeAllCoreFeatures', () => {
105
- it('removes core (and only core) feature definitions, accounting for aliasing', () => {
106
- const schema = buildSchemaFromAST(gql`
107
- directive @lonk(url: String, as: String, for: Porpoise, import: [lonk__Import]) repeatable on SCHEMA
108
-
109
- scalar lonk__Import
110
-
111
- enum Porpoise {
112
- """
113
- \`SECURITY\` features provide metadata necessary to securely resolve fields.
114
- """
115
- SECURITY
116
-
117
- """
118
- \`EXECUTION\` features provide metadata necessary for operation execution.
119
- """
120
- EXECUTION
121
- }
122
-
123
- extend schema
124
- @lonk(
125
- url: "https://specs.apollo.dev/link/v1.0",
126
- as: "lonk",
127
- import: [
128
- { name: "Purpose", as: "Porpoise" }
129
- ]
130
- )
131
- @lonk(
132
- url: "https://localhost/foobar/v1.0",
133
- as: "foo"
134
- import: [
135
- "bar",
136
- "@baz",
137
- { name: "qux", as: "qax" },
138
- { name: "@quz", as: "@qaz" },
139
- ]
140
- )
141
-
142
- type Query {
143
- q: Int
144
- }
145
-
146
- # Shouldn't remove original spec name
147
- scalar foobar
148
- scalar foobar__Scalar
149
- directive @foobar on FIELD
150
- directive @foobar__directive on FIELD
151
-
152
- # Should remove aliased spec name (other than type "foo")
153
- scalar foo
154
- scalar foo__Scalar
155
- directive @foo on FIELD
156
- directive @foo__directive on FIELD
157
-
158
- # Should remove imports (prefixed or not)
159
- type bar implements foo__bar {
160
- someField: foo!
161
- }
162
- interface foo__bar {
163
- someField: foo!
164
- }
165
- directive @baz on FIELD
166
- directive @foo__baz on FIELD
167
-
168
- # Shouldn't remove original import names
169
- input qux {
170
- someField: ID!
171
- }
172
- directive @quz on FIELD
173
-
174
- # Should remove aliased import names (and prefixed original)
175
- union qax = bar
176
- enum foo__qax {
177
- SOME_VALUE
178
- }
179
- scalar foo__qux
180
- directive @qaz on FIELD
181
- directive @foo__qaz on FIELD
182
- directive @foo__quz on FIELD
183
- `);
184
-
185
- removeAllCoreFeatures(schema);
186
- schema.validate();
187
-
188
- expect(schema.elementByCoordinate("@lonk")).toBeUndefined();
189
- expect(schema.elementByCoordinate("lonk__Import")).toBeUndefined();
190
- expect(schema.elementByCoordinate("Porpoise")).toBeUndefined();
191
- expect(schema.elementByCoordinate("foobar")).toBeDefined();
192
- expect(schema.elementByCoordinate("foobar__Scalar")).toBeDefined();
193
- expect(schema.elementByCoordinate("@foobar")).toBeDefined();
194
- expect(schema.elementByCoordinate("@foobar__directive")).toBeDefined();
195
- expect(schema.elementByCoordinate("foo")).toBeDefined();
196
- expect(schema.elementByCoordinate("foo__Scalar")).toBeUndefined();
197
- expect(schema.elementByCoordinate("@foo")).toBeUndefined();
198
- expect(schema.elementByCoordinate("@foo__directive")).toBeUndefined();
199
- expect(schema.elementByCoordinate("bar")).toBeUndefined();
200
- expect(schema.elementByCoordinate("foo__bar")).toBeUndefined();
201
- expect(schema.elementByCoordinate("@baz")).toBeUndefined();
202
- expect(schema.elementByCoordinate("@foo__baz")).toBeUndefined();
203
- expect(schema.elementByCoordinate("qux")).toBeDefined();
204
- expect(schema.elementByCoordinate("@quz")).toBeDefined();
205
- expect(schema.elementByCoordinate("qax")).toBeUndefined();
206
- expect(schema.elementByCoordinate("foo__qax")).toBeUndefined();
207
- expect(schema.elementByCoordinate("foo__qux")).toBeUndefined();
208
- expect(schema.elementByCoordinate("@qaz")).toBeUndefined();
209
- expect(schema.elementByCoordinate("@foo__qaz")).toBeUndefined();
210
- expect(schema.elementByCoordinate("@foo__quz")).toBeUndefined();
211
- });
212
- });