Skip to content

Commit 5f6eb40

Browse files
ardatankamilkisiela
authored andcommitted
Introduce Apollo v4 support & Drop Apollo v2/v3 and Node 16 support
1 parent abc5f4d commit 5f6eb40

File tree

23 files changed

+632
-738
lines changed

23 files changed

+632
-738
lines changed

.changeset/dull-camels-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-modules': minor
3+
---
4+
5+
Introduce `createApolloGateway` for Apollo Server v4

.changeset/heavy-owls-look.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'graphql-modules': major
3+
---
4+
5+
Drop Node 16 and Apollo v2 and v3 support
6+
7+
`createApolloExecutor` and `createSchemaForApollo` have been removed in favor of `createApolloGateway`.
8+
9+
You can create `gateway` instance for Apollo Server v4 like this, and pass it directly to `ApolloServer` constructor. You don't need to pass `schema` or `executor` anymore.
10+
11+
```ts
12+
const gateway = application.createApolloGateway()
13+
14+
const apolloServer = new ApolloServer({
15+
gateway
16+
})
17+
```

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
strategy:
4040
matrix:
4141
os: [ubuntu-latest] # remove windows to speed up the tests
42-
node-version: [12, 16, 18]
42+
node-version: [18, 20]
4343
graphql_version:
4444
- 15
4545
- 16
@@ -49,7 +49,7 @@ jobs:
4949
- name: Setup env
5050
uses: the-guild-org/shared-config/setup@main
5151
with:
52-
nodeVersion: 18
52+
nodeVersion: 20
5353
- name: Use GraphQL v${{matrix.graphql_version}}
5454
run: node ./scripts/match-graphql.js ${{matrix.graphql_version}}
5555
- name: Cache Jest

benchmark/basic.case.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,36 @@ let showedError = false;
9696

9797
const executeAppWithDI = appWithDI.createExecution();
9898
const executeApp = app.createExecution();
99-
const apolloWithDIExecutor = appWithDI.createApolloExecutor();
99+
const apolloGWWithDI = appWithDI.createApolloGateway();
100+
const apolloGWWithDILoadResult$ = apolloGWWithDI.load();
100101
const executeApolloWithDI = (args: ExecutionArgs) => {
101-
return apolloWithDIExecutor({
102-
schema: args.schema,
103-
document: args.document,
104-
operationName: args.operationName,
105-
context: args.contextValue,
106-
request: {
107-
variables: args.variableValues,
108-
},
109-
});
102+
return apolloGWWithDILoadResult$.then(({ executor }) =>
103+
executor({
104+
schema: args.schema,
105+
document: args.document,
106+
operationName: args.operationName,
107+
context: args.contextValue,
108+
request: {
109+
variables: args.variableValues,
110+
},
111+
})
112+
);
110113
};
111114

112-
const apolloExecutor = app.createApolloExecutor();
115+
const apolloGW = app.createApolloGateway();
116+
const apolloGWLoadResult$ = apolloGW.load();
113117
const executeApollo = (args: ExecutionArgs) => {
114-
return apolloExecutor({
115-
schema: args.schema,
116-
document: args.document,
117-
operationName: args.operationName,
118-
context: args.contextValue,
119-
request: {
120-
variables: args.variableValues,
121-
},
122-
});
118+
return apolloGWLoadResult$.then(({ executor }) =>
119+
executor({
120+
schema: args.schema,
121+
document: args.document,
122+
operationName: args.operationName,
123+
context: args.contextValue,
124+
request: {
125+
variables: args.variableValues,
126+
},
127+
})
128+
);
123129
};
124130

125131
const query = parse(/* GraphQL */ `
@@ -162,11 +168,11 @@ const suites: Record<string, { name: string; runner: Function }> = {
162168
},
163169
'apollo-with-id': {
164170
name: 'ApolloServer (DI)',
165-
runner: () => graphql(appWithDI.schema, executeApolloWithDI as any),
171+
runner: () => graphql(appWithDI.schema, executeApolloWithDI),
166172
},
167173
apollo: {
168174
name: 'ApolloServer',
169-
runner: () => graphql(app.schema, executeApollo as any),
175+
runner: () => graphql(app.schema, executeApollo),
170176
},
171177
};
172178

examples/apollo-subscriptions/src/app/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ import { PostModule } from './post/post.module';
44

55
export const graphqlApplication = createApplication({
66
modules: [PostModule],
7-
providers: [PubSub],
7+
providers: [
8+
{
9+
provide: PubSub,
10+
useValue: new PubSub(),
11+
},
12+
],
813
});
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import 'reflect-metadata';
22
import express from 'express';
33
import { createServer } from 'http';
4-
import { ApolloServer } from 'apollo-server-express';
5-
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
4+
import { ApolloServer } from '@apollo/server';
5+
import { expressMiddleware } from '@apollo/server/express4';
6+
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
67
import { graphqlApplication } from './app';
78
import { WebSocketServer } from 'ws';
89
import { useServer } from 'graphql-ws/lib/use/ws';
10+
import bodyParser from 'body-parser';
11+
import cors from 'cors';
912

10-
const { schema, createExecution, createSubscription, createApolloExecutor } =
13+
const { schema, createExecution, createSubscription, createApolloGateway } =
1114
graphqlApplication;
1215

13-
const executor = createApolloExecutor();
16+
const gateway = createApolloGateway();
1417

1518
const app = express();
1619
const httpServer = createServer(app);
1720
// Creating the WebSocket subscription server
1821

1922
const server = new ApolloServer({
20-
schema,
21-
executor,
23+
gateway,
2224
plugins: [
2325
// Proper shutdown for the HTTP server.
2426
ApolloServerPluginDrainHttpServer({ httpServer }),
@@ -35,11 +37,9 @@ const server = new ApolloServer({
3537
],
3638
});
3739

38-
server.applyMiddleware({ app });
39-
4040
const wsServer = new WebSocketServer({
4141
server: httpServer,
42-
path: server.graphqlPath,
42+
path: '/graphql',
4343
});
4444

4545
const execute = createExecution();
@@ -49,7 +49,24 @@ const subscribe = createSubscription();
4949
// telling the WebSocketServer to start listening
5050
const serverCleanup = useServer({ schema, execute, subscribe }, wsServer);
5151

52-
httpServer.listen({ port: 4000 }, () => {
52+
async function main() {
53+
await server.start();
54+
55+
app.use(
56+
'/graphql',
57+
cors<cors.CorsRequest>(),
58+
bodyParser.json(),
59+
expressMiddleware(server)
60+
);
61+
62+
httpServer.listen({ port: 4000 }, () => {
63+
// tslint:disable-next-line: no-console
64+
console.info(`🚀 Server ready at http://localhost:4000/graphql`);
65+
});
66+
}
67+
68+
main().catch((error) => {
5369
// tslint:disable-next-line: no-console
54-
console.info(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
70+
console.error(error);
71+
process.exit(1);
5572
});

examples/basic-with-dependency-injection/src/index.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ import 'reflect-metadata';
22
import { createApplication } from 'graphql-modules';
33
import { BlogModule } from './modules/blog';
44
import { UserModule } from './modules/user';
5-
import express from 'express';
6-
import { graphqlHTTP } from 'express-graphql';
5+
import http from 'http';
6+
import { createHandler } from 'graphql-http/lib/use/http';
77

88
const app = createApplication({
99
modules: [BlogModule, UserModule],
1010
});
1111

12-
const server = express();
13-
14-
const execute = app.createExecution();
15-
16-
server.use(
17-
'/graphql',
18-
graphqlHTTP({
19-
schema: app.schema,
20-
customExecuteFn: execute as any,
21-
graphiql: true,
22-
})
23-
);
12+
// Create the GraphQL over HTTP Node request handler
13+
const handler = createHandler({
14+
schema: app.schema,
15+
execute: app.createExecution(),
16+
});
2417

25-
server.listen(4000, () => {
26-
console.log('Live http://localhost:4000/graphql');
18+
// Create a HTTP server using the listener on `/graphql`
19+
const server = http.createServer((req, res) => {
20+
if (req.url?.startsWith('/graphql')) {
21+
handler(req, res);
22+
} else {
23+
res.writeHead(404).end();
24+
}
2725
});
26+
27+
server.listen(4000);
28+
console.log('Listening to port 4000');

examples/basic/src/index.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ declare global {
88

99
import 'reflect-metadata';
1010
import { createApplication } from 'graphql-modules';
11-
import express from 'express';
12-
import { graphqlHTTP } from 'express-graphql';
11+
import http from 'http';
12+
import { createHandler } from 'graphql-http/lib/use/http';
1313
import { UserModule } from './app/user/user.module';
1414
import { AuthModule } from './app/auth/auth.module';
1515
import { SocialNetworkModule } from './app/social-network/social-network.module';
1616

17-
const server = express();
1817
const app = createApplication({
1918
modules: [UserModule, AuthModule, SocialNetworkModule],
2019
});
21-
const execute = app.createExecution();
2220

23-
server.use(
24-
'/graphql',
25-
graphqlHTTP((request: any) => ({
26-
schema: app.schema,
27-
graphiql: true,
28-
customExecuteFn: execute as any,
29-
context: { request },
30-
}))
31-
);
21+
// Create the GraphQL over HTTP Node request handler
22+
const handler = createHandler({
23+
schema: app.schema,
24+
execute: app.createExecution(),
25+
});
3226

33-
server.listen(4000, () => {
34-
console.log('Live http://localhost:4000/graphql');
27+
// Create a HTTP server using the listener on `/graphql`
28+
const server = http.createServer((req, res) => {
29+
if (req.url?.startsWith('/graphql')) {
30+
handler(req, res);
31+
} else {
32+
res.writeHead(404).end();
33+
}
3534
});
35+
36+
server.listen(4000);
37+
console.log('Listening to port 4000');

examples/graphql-yoga/src/app/post/pubsub.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { InjectionToken, FactoryProvider, Scope } from 'graphql-modules';
2-
import { createPubSub, PubSub as TPubSub } from '@graphql-yoga/node';
2+
import { createPubSub, PubSub as TPubSub } from 'graphql-yoga';
33
import { Post } from './types';
44

55
type PubSub = TPubSub<{
66
POST_ADDED: [
77
{
88
postAdded: Post;
9-
}
9+
},
1010
];
1111
}>;
1212

examples/graphql-yoga/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import 'reflect-metadata';
2-
import { createServer } from '@graphql-yoga/node';
2+
import { createServer } from 'http';
3+
import { createYoga } from 'graphql-yoga';
34
import { useGraphQLModules } from '@envelop/graphql-modules';
45
import { app } from './app';
56

6-
const server = createServer({
7+
const yoga = createYoga({
78
plugins: [useGraphQLModules(app)],
89
});
910

10-
server.start().then(() => {
11+
const server = createServer(yoga);
12+
13+
server.listen(4000, () => {
1114
// tslint:disable-next-line: no-console
1215
console.info(`🚀 Server ready at http://localhost:4000/graphql`);
1316
});

0 commit comments

Comments
 (0)