Skip to content

Commit 9db552a

Browse files
committed
remove tests for combined keys, refactor
1 parent 2285b90 commit 9db552a

4 files changed

Lines changed: 87 additions & 206 deletions

File tree

__tests__/__fixtures__/federatedServiceExtendingUser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const typeDefs = gql`
88
empty: ID
99
}
1010
11-
extend type User @key(fields: "id") {
12-
id: Int! @external
11+
extend type User @key(fields: "nodeId") {
12+
nodeId: ID! @external
1313
firstName: String! @external
1414
lastName: String! @external
1515
fullName: String! @requires(fields: "firstName lastName")

__tests__/__snapshots__/schema.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`schema and _service.sdl: _service.sdl 1`] = `
4-
"type Email implements Node @key(fields: \\"id\\") @key(fields: \\"nodeId\\") {
4+
"type Email implements Node @key(fields: \\"nodeId\\") {
55
\\"A globally unique identifier. Can be used in various places throughout the system to identify this single value.\\"
66
nodeId: ID!
77
id: Int!
@@ -111,7 +111,7 @@ type Query {
111111
): UsersEmail
112112
}
113113
114-
type User implements Node @key(fields: \\"id\\") @key(fields: \\"nodeId\\") {
114+
type User implements Node @key(fields: \\"nodeId\\") {
115115
\\"A globally unique identifier. Can be used in various places throughout the system to identify this single value.\\"
116116
nodeId: ID!
117117
id: Int!
@@ -140,7 +140,7 @@ input UserCondition {
140140
lastName: String
141141
}
142142
143-
type UsersEmail implements Node @key(fields: \\"userId emailId\\") @key(fields: \\"nodeId\\") {
143+
type UsersEmail implements Node @key(fields: \\"nodeId\\") {
144144
\\"A globally unique identifier. Can be used in various places throughout the system to identify this single value.\\"
145145
nodeId: ID!
146146
userId: Int!

__tests__/integration.test.ts

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import * as http from "http";
33
import { postgraphile } from "postgraphile";
44
import { ApolloGateway } from "@apollo/gateway";
55
import { startFederatedServiceExtendingUser } from "./__fixtures__/federatedServiceExtendingUser";
6-
import { ApolloServer } from "apollo-server";
6+
import { ApolloServer, ServerInfo } from "apollo-server";
77
import * as pg from "pg";
88
import axios from "axios";
9+
import { GraphQLSchema } from "graphql";
910

1011
let pgPool: pg.Pool | null;
1112

@@ -51,9 +52,25 @@ function toUrl(
5152
: `http://${obj.address}:${obj.port}`;
5253
}
5354

54-
test("federated service", async () => {
55+
async function withFederatedExternalServices(
56+
startExternalServices: {
57+
[serviceName: string]: () => ApolloServer | Promise<ApolloServer>;
58+
},
59+
cb: (_: { serverInfo: ServerInfo; schema: GraphQLSchema }) => Promise<any>
60+
) {
5561
const postgraphileServer = await startPostgraphile();
56-
const serviceExtendingUser = startFederatedServiceExtendingUser();
62+
const externalServices = await Promise.all(
63+
Object.entries(startExternalServices).map(
64+
async ([name, serviceBuilder]) => {
65+
const service = await serviceBuilder();
66+
return {
67+
name,
68+
service,
69+
url: toUrl(await service.listen({ port: 0, host: "127.0.0.1" })),
70+
};
71+
}
72+
)
73+
);
5774
let server: ApolloServer | undefined;
5875

5976
try {
@@ -62,48 +79,55 @@ test("federated service", async () => {
6279
name: "postgraphile",
6380
url: toUrl(postgraphileServer.address()!) + "/graphql",
6481
},
65-
{
66-
name: "serviceExtendingUser",
67-
url: toUrl(
68-
await serviceExtendingUser.listen({ port: 0, host: "127.0.0.1" })
69-
),
70-
},
82+
...externalServices,
7183
];
7284

7385
const { schema, executor } = await new ApolloGateway({
7486
serviceList,
7587
}).load();
7688

77-
expect(schema).toMatchSnapshot("federated schema");
78-
7989
server = new ApolloServer({
8090
schema,
8191
executor,
8292
});
8393

84-
const running = await server.listen({ port: 0, host: "127.0.0.1" });
85-
86-
debugger;
87-
const result = await axios.post(running.url, {
88-
query: `{ allUsersList(first: 1) { firstName, lastName, fullName} }`,
89-
});
94+
const serverInfo = await server.listen({ port: 0, host: "127.0.0.1" });
9095

91-
expect(result.data).toMatchObject({
92-
data: {
93-
allUsersList: [
94-
{
95-
firstName: "alicia",
96-
fullName: "alicia keys",
97-
lastName: "keys",
98-
},
99-
],
100-
},
101-
});
96+
await cb({ serverInfo, schema });
10297
} finally {
10398
await postgraphileServer.close();
104-
await serviceExtendingUser.stop();
99+
for (const external of externalServices) {
100+
await external.service.stop();
101+
}
105102
if (server) {
106103
await server.stop();
107104
}
108105
}
106+
}
107+
108+
test("federated service", async () => {
109+
await withFederatedExternalServices(
110+
{
111+
serviceExteningUser: startFederatedServiceExtendingUser,
112+
},
113+
async ({ serverInfo, schema }) => {
114+
expect(schema).toMatchSnapshot("federated schema");
115+
116+
const result = await axios.post(serverInfo.url, {
117+
query: `{ allUsersList(first: 1) { firstName, lastName, fullName} }`,
118+
});
119+
120+
expect(result.data).toMatchObject({
121+
data: {
122+
allUsersList: [
123+
{
124+
firstName: "alicia",
125+
fullName: "alicia keys",
126+
lastName: "keys",
127+
},
128+
],
129+
},
130+
});
131+
}
132+
);
109133
});

0 commit comments

Comments
 (0)