Skip to content

Commit

Permalink
feat: updated to the newest ecosystem dependencies
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Apollo deprecated the @apollo/federation, we switched to @apollo/subgraph, but that requires changes in the package.json. Updating to this version will break the existing app, unless you follow the upgrades visible in the ./scaffold/package.json file
  • Loading branch information
lgandecki committed Aug 21, 2023
1 parent 1aeca74 commit 99b0ecd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 78 deletions.
64 changes: 36 additions & 28 deletions scaffold/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,57 @@
]
},
"dependencies": {
"@apollo/federation": "0.17.0",
"apollo-server-express": "2.16.1",
"@apollo/server": "^4.9.0",
"@apollo/subgraph": "^2.5.1",
"body-parser": "^1.20.2",
"cookie-parser": "1.4.5",
"cors": "^2.8.5",
"express": "4.17.1",
"graphql": "15.5.1",
"graphql-tag": "2.11.0",
"lodash": "4.17.19",
"graphql": "16.7.1",
"graphql-tag": "2.12.6",
"lodash": "4.17.21",
"portable-fetch": "3.0.0",
"tsconfig-paths": "3.9.0"
},
"devDependencies": {
"@graphql-codegen/add": "^4.0.1",
"@graphql-codegen/cli": "^3.2.1",
"@graphql-codegen/typescript": "^3.0.1",
"@graphql-codegen/add": "5.0.0",
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/typescript": "4.0.1",
"@graphql-codegen/typescript-mongodb": "2.4.6",
"@graphql-codegen/typescript-operations": "^3.0.1",
"@graphql-codegen/typescript-resolvers": "^3.1.0",
"@graphql-tools/graphql-file-loader": "6.0.14",
"@graphql-tools/load": "6.0.14",
"@graphql-tools/merge": "6.0.14",
"@graphql-codegen/typescript-operations": "4.0.1",
"@graphql-codegen/typescript-resolvers": "4.0.1",
"@graphql-tools/graphql-file-loader": "8.0.0",
"@graphql-tools/load": "8.0.0",
"@graphql-tools/merge": "9.0.0",
"@jest/globals": "26.1.0",
"@tsconfig/node18": "^18.2.0",
"@types/body-parser": "^1.19.2",
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/jest": "29.4.0",
"@types/lodash": "^4.14.196",
"@types/node": "14.0.23",
"@types/shelljs": "0.8.8",
"@typescript-eslint/eslint-plugin": "4.5.0",
"@typescript-eslint/parser": "4.5.0",
"eslint": "7.12.0",
"eslint-config-airbnb-typescript": "12.0.0",
"eslint-import-resolver-typescript": "2.3.0",
"eslint-config-prettier": "6.14.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-prettier": "3.1.4",
"@typescript-eslint/eslint-plugin": "6.2.1",
"@typescript-eslint/parser": "6.2.1",
"chimp": "latest",
"chimp-graphql-codegen-plugin": "latest",
"jest": "29.4.0",
"eslint": "8.46.0",
"eslint-config-airbnb-typescript": "17.1.0",
"eslint-config-prettier": "8.10.0",
"eslint-import-resolver-typescript": "3.5.5",
"eslint-plugin-import": "2.28.0",
"eslint-plugin-prettier": "5.0.0",
"jest": "29.6.2",
"lint-staged": "10.2.11",
"nodemon": "2.0.4",
"prettier": "2.0.5",
"shelljs": "0.8.4",
"testdouble": "3.16.1",
"prettier": "3.0.1",
"shelljs": "0.8.5",
"testdouble": "3.18.0",
"testdouble-jest": "2.0.0",
"ts-jest": "29.0.5",
"ts-node": "8.10.2",
"typescript": "4.7.4"
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "5.1.6"
}
}
29 changes: 11 additions & 18 deletions scaffold/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import express from "express";
import { RootInterface } from "./root";
import { dataSources } from "./dataSources";

export type GqlContext = AppContext & {
dataSources: ReturnType<typeof dataSources>;
};
export type GqlContext = RootInterface & { req: RequestType };

export type AppContext = RootInterface & {
type RequestType = {
headers: {
[key: string]: string | string[];
[key: string]: string | string[] | undefined;
};
jwt?: string;
};

export const appContext = (root: RootInterface) => ({
req,
}: {
req: express.Request;
}): AppContext => {
return {
...root,
headers: req.headers,
jwt: req.cookies.jwt,
};
};
export const appContext =
(root: RootInterface) =>
async ({ req }: { req: RequestType }): Promise<GqlContext> => {
return {
...root,
req,
};
};
49 changes: 29 additions & 20 deletions scaffold/src/createApp.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import { ApolloServer } from "apollo-server-express";
import express from "express";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import { json } from "body-parser";
import cors from "cors";
import cookieParser from "cookie-parser";
import express from "express";
import http from "http";
import { schema } from "~generated/graphql/schema";
import { appContext } from "~app/context";
import { dataSources } from "./dataSources";
import { GqlContext, appContext } from "~app/context";
import { root } from "./root";

const apollo = new ApolloServer<GqlContext>({
schema,
});

export const createApp = () => {
apollo.start().then(async () => {
const app = express();

app.use([cookieParser()]);
const httpServer = http.createServer(app);

const corsOptions = {
origin: 'http://localhost:3000',
credentials: true
}

const apollo = new ApolloServer({
schema,
dataSources,
context: appContext(root),
origin: "http://localhost:3000",
credentials: true,
};

app.use(
"/graphql",
cors<cors.CorsRequest>(corsOptions),
json(),
expressMiddleware(apollo, {
context: appContext(root),
}),
);

const port = process.env.PORT || 4000;
httpServer.listen({ port }, () => {
console.log(`🚀 Server ready at http://localhost:${port}/graphql`);
});

apollo.applyMiddleware({ app, cors: corsOptions });

return app;
};

});
3 changes: 0 additions & 3 deletions scaffold/src/dataSources.ts

This file was deleted.

7 changes: 1 addition & 6 deletions scaffold/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import { createApp } from "./createApp";

const port = process.env.PORT || 4000
createApp().listen({ port }, () =>
console.log(`🚀 Server ready at http://localhost:${port}/graphql`)
);
import "./createApp";
2 changes: 1 addition & 1 deletion src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const fixGenerated = async (projectMainPath: string) => {
};

const prettifyGenerated = async (projectMainPath: string, modulesPath = 'src') => {
await execQuietly(`npx prettier --write "${modulesPath}/**/*.ts" "generated/**/*.ts" --loglevel error`, {
await execQuietly(`npx prettier --write "${modulesPath}/**/*.ts" "generated/**/*.ts" --log-level error`, {
cwd: projectMainPath,
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/generate/templates/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import { buildFederatedSchema } from '@apollo/federation';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { resolvers } from '{{generatedPrefix}}/graphql/resolvers';
import gql from 'graphql-tag';

const typeDefs = gql`{{{schemaString}}}`;

const schema = buildFederatedSchema([
const schema = buildSubgraphSchema([
{
typeDefs,
resolvers,
Expand Down

0 comments on commit 99b0ecd

Please sign in to comment.