Skip to content

chore(dependencies): migrate apollo-server 3->4 #300

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

Open
wants to merge 1 commit into
base: feature/PE-7535_major_3
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
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
"url": "https://github.com/ar-io/ar-io-node"
},
"dependencies": {
"@apollo/server": "^4.11.3",
"@apollo/server-plugin-landing-page-graphql-playground": "^4.0.1",
"@ar.io/sdk": "^3.4.0-alpha.3",
"@aws-lite/client": "^0.22.4",
"@aws-lite/s3": "^0.2.6",
"@clickhouse/client": "^1.10.1",
"@dha-team/arbundles": "^1.0.1",
"@permaweb/aoconnect": "^0.0.63",
"apollo-server-express": "^3.13.0",
"arweave": "^1.15.5",
"axios": "^1.7.9",
"better-sqlite3": "^9.4.5",
Expand All @@ -28,6 +29,7 @@
"fastq": "^1.18.0",
"fs-extra": "^11.3.0",
"graphql": "^16.10.0",
"graphql-tag": "^2.12.6",
"ioredis": "^5.4.2",
"json-canonicalize": "^1.0.6",
"lmdb": "^3.2.2",
Expand Down Expand Up @@ -62,6 +64,7 @@
"@testcontainers/localstack": "^10.17.2",
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
"@types/better-sqlite3": "7.6.3",
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/express-prometheus-middleware": "^1.2.3",
"@types/express-serve-static-core": "^5.0.6",
Expand Down Expand Up @@ -98,7 +101,8 @@
},
"resolutions": {
"gc-stats/nan": "2.18.0",
"express-prometheus-middleware/prometheus-gc-stats/gc-stats": "1.4.1"
"express-prometheus-middleware/prometheus-gc-stats/gc-stats": "1.4.1",
"lru-cache": "8.x"
},
"scripts": {
"build": "yarn clean && npx tsc --project ./tsconfig.prod.json && yarn copy-files",
Expand Down
24 changes: 8 additions & 16 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { default as cors } from 'cors';
import cors from 'cors';
import express from 'express';
import { Server } from 'node:http';

Expand All @@ -26,7 +26,7 @@ import { arIoRouter } from './routes/ar-io.js';
import { arnsRouter } from './routes/arns.js';
import { dataRouter } from './routes/data/index.js';
import { arweaveRouter } from './routes/arweave.js';
import { apolloServer } from './routes/graphql/index.js';
import { makeApolloServerMiddleware } from './routes/graphql/index.js';
import { openApiRouter } from './routes/openapi.js';
import * as system from './system.js';

Expand Down Expand Up @@ -66,20 +66,12 @@ app.use(dataRouter);
app.use(arweaveRouter);

// GraphQL
const apolloServerInstanceGql = apolloServer(system.gqlQueryable, {
introspection: true,
persistedQueries: false,
const apolloServerMiddleware = await makeApolloServerMiddleware({
db: system.gqlQueryable,
});

let server: Server;
apolloServerInstanceGql.start().then(() => {
apolloServerInstanceGql.applyMiddleware({
app: app as any,
path: '/graphql',
});
server = app.listen(config.PORT, () => {
log.info(`Listening on port ${config.PORT}`);
});
});
app.use(apolloServerMiddleware);

export { server };
export const server: Server = app.listen(config.PORT, () => {
log.info(`Listening on port ${config.PORT}`);
});
15 changes: 12 additions & 3 deletions src/database/composite-clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import * as winston from 'winston';
import sql from 'sql-bricks';
import { ClickHouseClient, createClient } from '@clickhouse/client';
import { ValidationError } from 'apollo-server-express';
import { ApolloServerErrorCode } from '@apollo/server/errors';
import { GraphQLError } from 'graphql';

import {
b64UrlToHex,
Expand Down Expand Up @@ -69,7 +70,11 @@ export function decodeTransactionGqlCursor(cursor: string | undefined) {

return { height, blockTransactionIndex, isDataItem, id, indexedAt };
} catch (error) {
throw new ValidationError('Invalid transaction cursor');
throw new GraphQLError('Invalid transaction cursor', {
extensions: {
code: ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
},
});
}
}

Expand All @@ -87,7 +92,11 @@ export function decodeBlockGqlCursor(cursor: string | undefined) {

return { height };
} catch (error) {
throw new ValidationError('Invalid block cursor');
throw new GraphQLError('Invalid block cursor', {
extensions: {
code: ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
},
});
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/database/standalone-sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
import { strict as assert } from 'node:assert';
import { after, before, beforeEach, describe, it } from 'node:test';
import { ValidationError } from 'apollo-server-express';
import crypto from 'node:crypto';
import fs from 'node:fs';

Expand Down Expand Up @@ -108,7 +107,7 @@ describe('SQLite GraphQL cursor functions', () => {
decodeTransactionGqlCursor('123');
},
{
name: ValidationError.name,
name: 'GraphQLError',
message: 'Invalid transaction cursor',
},
);
Expand Down Expand Up @@ -140,7 +139,7 @@ describe('SQLite GraphQL cursor functions', () => {
decodeBlockGqlCursor('123');
},
{
name: ValidationError.name,
name: 'GraphQLError',
message: 'Invalid block cursor',
},
);
Expand Down
15 changes: 12 additions & 3 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ValidationError } from 'apollo-server-express';
import { ApolloServerErrorCode } from '@apollo/server/errors';
import { GraphQLError } from 'graphql';
import Sqlite from 'better-sqlite3';
import crypto from 'node:crypto';
import os from 'node:os';
Expand Down Expand Up @@ -122,7 +123,11 @@ export function decodeTransactionGqlCursor(cursor: string | undefined) {

return { height, blockTransactionIndex, dataItemId, indexedAt, id };
} catch (error) {
throw new ValidationError('Invalid transaction cursor');
throw new GraphQLError('Invalid transaction cursor', {
extensions: {
code: ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
},
});
}
}

Expand All @@ -140,7 +145,11 @@ export function decodeBlockGqlCursor(cursor: string | undefined) {

return { height };
} catch (error) {
throw new ValidationError('Invalid block cursor');
throw new GraphQLError('Invalid block cursor', {
extensions: {
code: ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
},
});
}
}

Expand Down
55 changes: 30 additions & 25 deletions src/routes/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,45 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {
ApolloServerPluginLandingPageDisabled,
ApolloServerPluginLandingPageGraphQLPlayground,
} from 'apollo-server-core';
import {
ApolloServer,
ApolloServerExpressConfig,
gql,
} from 'apollo-server-express';

import { ApolloServer } from '@apollo/server';
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
import { expressMiddleware } from '@apollo/server/express4';
import { DocumentNode } from 'graphql';
import gql from 'graphql-tag';

import { readFileSync } from 'node:fs';

import { GqlQueryable } from '../../types.js';
import { resolvers } from './resolvers.js';

const typeDefsUrl = new URL('./schema/types.graphql', import.meta.url);
const typeDefs = gql(readFileSync(typeDefsUrl, 'utf8'));
const typeDefs: DocumentNode | undefined = gql(
readFileSync(typeDefsUrl, 'utf8'),
);

const apolloServer = (
db: GqlQueryable,
opts: ApolloServerExpressConfig = {},
) => {
return new ApolloServer({
interface ApolloServerContext {
db: GqlQueryable;
}

export const makeApolloServerMiddleware = async (
context: ApolloServerContext,
): Promise<any> => {
const apolloServer = new ApolloServer<ApolloServerContext>({
typeDefs,
resolvers,
debug: false,
plugins: [
ApolloServerPluginLandingPageDisabled(),
ApolloServerPluginLandingPageGraphQLPlayground(),
],
context: () => {
return { db };
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
introspection: true,
persistedQueries: {
ttl: 300, // 5 minutes
},
...opts,
});
};

export { apolloServer };
await apolloServer.start();

return expressMiddleware<ApolloServerContext>(apolloServer, {
context: async (): Promise<ApolloServerContext> => {
return { ...context };
},
});
};
Loading