Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Apollo v4 support & Drop Apollo v2/v3 and Node 16 support #2444

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dull-camels-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-modules': minor
---

Introduce `createApolloGateway` for Apollo Server v4
5 changes: 5 additions & 0 deletions .changeset/graphql-modules-2444-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-modules': patch
---
dependencies updates:
- Removed dependency [`@graphql-tools/wrap@^10.0.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/wrap/v/10.0.0) (from `dependencies`)
17 changes: 17 additions & 0 deletions .changeset/heavy-owls-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'graphql-modules': major
---

Drop Node 16 and Apollo v2 and v3 support

`createApolloExecutor` and `createSchemaForApollo` have been removed in favor of `createApolloGateway`.

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.

```ts
const gateway = application.createApolloGateway()

const apolloServer = new ApolloServer({
gateway
})
```
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ jobs:
uses: the-guild-org/shared-config/.github/workflows/ci-node-matrix.yml@main
with:
script: 'yarn build && yarn benchmark:basic'
nodeVersions: '[16,18]'
nodeVersions: '[18,20]'
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest] # remove windows to speed up the tests
node-version: [12, 16, 18]
node-version: [18, 20]
graphql_version:
- 15
- 16
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Setup env
uses: the-guild-org/shared-config/setup@main
with:
nodeVersion: 18
nodeVersion: 20
- name: Use GraphQL v${{matrix.graphql_version}}
run: node ./scripts/match-graphql.js ${{matrix.graphql_version}}
- name: Cache Jest
Expand Down
50 changes: 28 additions & 22 deletions benchmark/basic.case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,36 @@ let showedError = false;

const executeAppWithDI = appWithDI.createExecution();
const executeApp = app.createExecution();
const apolloWithDIExecutor = appWithDI.createApolloExecutor();
const apolloGWWithDI = appWithDI.createApolloGateway();
const apolloGWWithDILoadResult$ = apolloGWWithDI.load();
const executeApolloWithDI = (args: ExecutionArgs) => {
return apolloWithDIExecutor({
schema: args.schema,
document: args.document,
operationName: args.operationName,
context: args.contextValue,
request: {
variables: args.variableValues,
},
});
return apolloGWWithDILoadResult$.then(({ executor }) =>
executor({
schema: args.schema,
document: args.document,
operationName: args.operationName,
context: args.contextValue,
request: {
variables: args.variableValues,
},
})
);
};

const apolloExecutor = app.createApolloExecutor();
const apolloGW = app.createApolloGateway();
const apolloGWLoadResult$ = apolloGW.load();
const executeApollo = (args: ExecutionArgs) => {
return apolloExecutor({
schema: args.schema,
document: args.document,
operationName: args.operationName,
context: args.contextValue,
request: {
variables: args.variableValues,
},
});
return apolloGWLoadResult$.then(({ executor }) =>
executor({
schema: args.schema,
document: args.document,
operationName: args.operationName,
context: args.contextValue,
request: {
variables: args.variableValues,
},
})
);
};

const query = parse(/* GraphQL */ `
Expand Down Expand Up @@ -162,11 +168,11 @@ const suites: Record<string, { name: string; runner: Function }> = {
},
'apollo-with-id': {
name: 'ApolloServer (DI)',
runner: () => graphql(appWithDI.schema, executeApolloWithDI as any),
runner: () => graphql(appWithDI.schema, executeApolloWithDI),
},
apollo: {
name: 'ApolloServer',
runner: () => graphql(app.schema, executeApollo as any),
runner: () => graphql(app.schema, executeApollo),
},
};

Expand Down
7 changes: 6 additions & 1 deletion examples/apollo-subscriptions/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ import { PostModule } from './post/post.module';

export const graphqlApplication = createApplication({
modules: [PostModule],
providers: [PubSub],
providers: [
{
provide: PubSub,
useValue: new PubSub(),
},
],
});
39 changes: 28 additions & 11 deletions examples/apollo-subscriptions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import 'reflect-metadata';
import express from 'express';
import { createServer } from 'http';
import { ApolloServer } from 'apollo-server-express';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { graphqlApplication } from './app';
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import bodyParser from 'body-parser';
import cors from 'cors';

const { schema, createExecution, createSubscription, createApolloExecutor } =
const { schema, createExecution, createSubscription, createApolloGateway } =
graphqlApplication;

const executor = createApolloExecutor();
const gateway = createApolloGateway();

const app = express();
const httpServer = createServer(app);
// Creating the WebSocket subscription server

const server = new ApolloServer({
schema,
executor,
gateway,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),
Expand All @@ -35,11 +37,9 @@ const server = new ApolloServer({
],
});

server.applyMiddleware({ app });

const wsServer = new WebSocketServer({
server: httpServer,
path: server.graphqlPath,
path: '/graphql',
});

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

httpServer.listen({ port: 4000 }, () => {
async function main() {
await server.start();

app.use(
'/graphql',
cors<cors.CorsRequest>(),
bodyParser.json(),
expressMiddleware(server)
);

httpServer.listen({ port: 4000 }, () => {
// tslint:disable-next-line: no-console
console.info(`🚀 Server ready at http://localhost:4000/graphql`);
});
}

main().catch((error) => {
// tslint:disable-next-line: no-console
console.info(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
console.error(error);
process.exit(1);
});
33 changes: 17 additions & 16 deletions examples/basic-with-dependency-injection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ import 'reflect-metadata';
import { createApplication } from 'graphql-modules';
import { BlogModule } from './modules/blog';
import { UserModule } from './modules/user';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import http from 'http';
import { createHandler } from 'graphql-http/lib/use/http';

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

const server = express();

const execute = app.createExecution();

server.use(
'/graphql',
graphqlHTTP({
schema: app.schema,
customExecuteFn: execute as any,
graphiql: true,
})
);
// Create the GraphQL over HTTP Node request handler
const handler = createHandler({
schema: app.schema,
execute: app.createExecution(),
});

server.listen(4000, () => {
console.log('Live http://localhost:4000/graphql');
// Create a HTTP server using the listener on `/graphql`
const server = http.createServer((req, res) => {
if (req.url?.startsWith('/graphql')) {
handler(req, res);
} else {
res.writeHead(404).end();
}
});

server.listen(4000);
console.log('Listening to port 4000');
32 changes: 17 additions & 15 deletions examples/basic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ declare global {

import 'reflect-metadata';
import { createApplication } from 'graphql-modules';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import http from 'http';
import { createHandler } from 'graphql-http/lib/use/http';
import { UserModule } from './app/user/user.module';
import { AuthModule } from './app/auth/auth.module';
import { SocialNetworkModule } from './app/social-network/social-network.module';

const server = express();
const app = createApplication({
modules: [UserModule, AuthModule, SocialNetworkModule],
});
const execute = app.createExecution();

server.use(
'/graphql',
graphqlHTTP((request: any) => ({
schema: app.schema,
graphiql: true,
customExecuteFn: execute as any,
context: { request },
}))
);
// Create the GraphQL over HTTP Node request handler
const handler = createHandler({
schema: app.schema,
execute: app.createExecution(),
});

server.listen(4000, () => {
console.log('Live http://localhost:4000/graphql');
// Create a HTTP server using the listener on `/graphql`
const server = http.createServer((req, res) => {
if (req.url?.startsWith('/graphql')) {
handler(req, res);
} else {
res.writeHead(404).end();
}
});

server.listen(4000);
console.log('Listening to port 4000');
4 changes: 2 additions & 2 deletions examples/graphql-yoga/src/app/post/pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { InjectionToken, FactoryProvider, Scope } from 'graphql-modules';
import { createPubSub, PubSub as TPubSub } from '@graphql-yoga/node';
import { createPubSub, PubSub as TPubSub } from 'graphql-yoga';
import { Post } from './types';

type PubSub = TPubSub<{
POST_ADDED: [
{
postAdded: Post;
}
},
];
}>;

Expand Down
9 changes: 6 additions & 3 deletions examples/graphql-yoga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'reflect-metadata';
import { createServer } from '@graphql-yoga/node';
import { createServer } from 'http';
import { createYoga } from 'graphql-yoga';
import { useGraphQLModules } from '@envelop/graphql-modules';
import { app } from './app';

const server = createServer({
const yoga = createYoga({
plugins: [useGraphQLModules(app)],
});

server.start().then(() => {
const server = createServer(yoga);

server.listen(4000, () => {
// tslint:disable-next-line: no-console
console.info(`🚀 Server ready at http://localhost:4000/graphql`);
});
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"deploy-website": "cd website && yarn && yarn build && mkdir graphql-modules && mv build/* graphql-modules && mv graphql-modules build"
},
"devDependencies": {
"@apollo/federation": "0.38.1",
"@apollo/subgraph": "2.5.5",
"@apollo/server": "4.9.4",
"@babel/core": "7.23.2",
"@babel/preset-env": "7.23.2",
"@babel/preset-typescript": "7.23.2",
Expand All @@ -34,10 +35,10 @@
"@changesets/changelog-github": "0.4.8",
"@envelop/graphql-modules": "5.0.3",
"@graphql-tools/merge": "9.0.0",
"@graphql-yoga/node": "3.9.1",
"@types/benchmark": "2.1.4",
"graphql-jit": "0.8.4",
"@types/express": "4.17.20",
"@types/cors": "2.8.14",
"@types/jest": "27.5.2",
"@types/node": "18.18.7",
"@types/ramda": "0.29.7",
Expand All @@ -53,14 +54,16 @@
"babel-jest": "29.7.0",
"bob-the-bundler": "1.7.3",
"chalk": "4.1.2",
"cors": "2.8.5",
"dataloader": "2.2.2",
"eslint": "8.52.0",
"express": "4.18.2",
"express-graphql": "0.12.0",
"graphql-http": "1.22.0",
"globby": "11.1.0",
"graphql": "16.8.1",
"graphql-subscriptions": "2.0.0",
"graphql-ws": "5.14.2",
"graphql-yoga": "4.0.5",
"husky": "8.0.3",
"jest": "27.5.1",
"lint-staged": "14.0.1",
Expand Down
1 change: 0 additions & 1 deletion packages/graphql-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
},
"dependencies": {
"@graphql-tools/schema": "^10.0.0",
"@graphql-tools/wrap": "^10.0.0",
"@graphql-typed-document-node/core": "^3.1.0",
"ramda": "^0.29.0"
},
Expand Down
Loading
Loading