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

chore: upgrade apollo to v4 and fix related issues #429

Merged
merged 5 commits into from
Nov 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: '16.15'
node-version-file: 'package.json'
cache: 'yarn'

- name: Start MongoDB
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM registry.gitlab.com/openbeta/openbeta-nodejs-docker:16
FROM registry.gitlab.com/openbeta/openbeta-nodejs-docker:18

ENV APP_DIR=/apps/openbeta-graphql

Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
"@types/underscore": "^1.11.4",
"cross-env": "^7.0.3",
"husky": "^8.0.1",
"jest": "29.4.2",
"jest-extended": "^3.2.3",
"mongodb-memory-server": "^8.12.2",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"mongodb-memory-server": "^10.1.2",
"nock": "^13.3.0",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"ts-jest": "^29.2.5",
"ts-standard": "^12.0.0",
"typescript": "4.9.5",
"wait-for-expect": "^3.0.2"
},
"dependencies": {
"@apollo/server": "^4.11.2",
"@babel/runtime": "^7.17.2",
"@google-cloud/storage": "^6.9.5",
"@graphql-tools/schema": "^8.3.1",
Expand All @@ -36,9 +37,7 @@
"@turf/convex": "^6.5.0",
"@turf/helpers": "^6.5.0",
"@types/uuid": "^8.3.3",
"apollo-datasource-mongodb": "^0.5.4",
"apollo-server-core": "^3.13.0",
"apollo-server-express": "^3.13.0",
"apollo-datasource-mongodb": "^0.6.0",
"auth0": "^3.4.0",
"axios": "^1.3.6",
"body-parser": "^1.20.2",
Expand All @@ -48,26 +47,27 @@
"dotenv": "^16.4.4",
"express": "^4.18.2",
"glob": "^10.2.2",
"graphql": "^16.8.1",
"graphql": "^16.9.0",
"graphql-middleware": "^6.1.31",
"graphql-shield": "^7.5.0",
"graphql-tag": "^2.12.6",
"graphql-type-json": "^0.3.2",
"i18n-iso-countries": "^7.5.0",
"immer": "^9.0.15",
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^2.1.4",
"mongoose": "6.4.7",
"mongoose-lean-virtuals": "^0.9.1",
"mongoose": "^7.8.3",
"mongoose-lean-virtuals": "^1.0.0",
"node-fetch": "2",
"p-limit": "^4.0.0",
"pino": "^8.2.0",
"pino": "^9.5.0",
"pino-logflare": "^0.4.2",
"sanitize-html": "^2.7.2",
"sharp": "^0.32.0",
"typesense": "^1.2.1",
"typesense": "^1.8.2",
"underscore": "^1.13.2",
"uuid": "^8.3.2",
"uuid-mongodb": "^2.5.1",
"uuid-mongodb": "^2.6.0",
"yup": "^1.1.1"
},
"scripts": {
Expand Down Expand Up @@ -109,6 +109,6 @@
},
"type": "module",
"engines": {
"node": ">=16.14.0"
"node": ">=18.20.0 <19"
}
}
2 changes: 1 addition & 1 deletion src/__tests__/areas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloServer } from 'apollo-server-express'
import { ApolloServer } from '@apollo/server'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/bulkImport.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ApolloServer} from "apollo-server-express";
import {ApolloServer} from "@apollo/server";
import muuid from "uuid-mongodb";
import express from "express";
import {InMemoryDB} from "../utils/inMemoryDB.js";
Expand Down Expand Up @@ -49,7 +49,6 @@ describe('bulkImportAreas', () => {
// "59f1d95a-627d-4b8c-91b9-389c7424cb54" instead of base64 "WfHZWmJ9S4yRuTicdCTLVA==".
user = muuid.mode('relaxed').v4()
userUuid = muuidToString(user)

bulkImport = BulkImportDataSource.getInstance()
climbs = MutableClimbDataSource.getInstance()
})
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloServer } from 'apollo-server-express'
import { ApolloServer } from '@apollo/server'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/organizations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ApolloServer } from 'apollo-server-express'
import { ApolloServer } from '@apollo/server'
import muuid from 'uuid-mongodb'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
import MutableOrganizationDataSource from '../model/MutableOrganizationDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { OperationType, OrganizationEditableFieldsType, OrganizationType, OrgType } from '../db/OrganizationTypes.js'
import { changelogDataSource } from '../model/ChangeLogDataSource.js'
import ChangeLogDataSource from '../model/ChangeLogDataSource.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { muuidToString } from '../utils/helpers.js'
import { validate as validateMuuid } from 'uuid'
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('organizations API', () => {
// eslint-disable-next-line
await new Promise(res => setTimeout(res, 1000))

const orgHistory = await changelogDataSource.getOrganizationChangeSets()
const orgHistory = await ChangeLogDataSource.getInstance().getOrganizationChangeSets()
expect(orgHistory).toHaveLength(2)

// verify changes in most recent order
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/ticks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloServer } from 'apollo-server-express'
import { ApolloServer } from '@apollo/server'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('ticks API', () => {
}
},
userUuid,
roles: ['user_admin'],
roles: [], // ['user_admin'],
app
})

Expand Down
28 changes: 13 additions & 15 deletions src/auth/local-dev/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/*
* This file is a mod of src/auth/middleware.ts and is used when starting the server via `yarn serve-dev`
* It bypasses the authentication for local development
*/
import muuid, { MUUID } from 'uuid-mongodb'
import { AuthUserType } from '../../types.js'
import { logger } from '../../logger.js'

export const localDevBypassAuthContext = (() => {
const testUUID: MUUID = muuid.v4()
const testUUID: MUUID = muuid.v4()

return async ({ req }): Promise<any> => {
const user: AuthUserType = {
roles: ['user_admin', 'org_admin', 'editor'],
uuid: testUUID,
isBuilder: false
}
logger.info(`The user.roles for this session is: ${user.roles.toString()}`)
return { user }
/*
* This file is a mod of src/auth/middleware.ts and is used when starting the server via `yarn serve-dev`
* It bypasses the authentication for local development
*/
export const localDevBypassAuthContext = async ({ req }): Promise<any> => {
const user: AuthUserType = {
roles: ['user_admin', 'org_admin', 'editor'],
uuid: testUUID,
isBuilder: false
}
})()
logger.info(`The user.roles for this session is: ${user.roles.toString()}`)
return { user }
}
10 changes: 8 additions & 2 deletions src/auth/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import muid from 'uuid-mongodb'
import { Request } from 'express'
import { AuthUserType } from '../types.js'
import { verifyJWT } from './util.js'
import { logger } from '../logger.js'

export interface CustomContext {
user: AuthUserType
token?: string
}

const EMTPY_USER: AuthUserType = {
isBuilder: false,
roles: [],
Expand All @@ -12,7 +18,7 @@ const EMTPY_USER: AuthUserType = {
/**
* Create a middleware context for Apollo server
*/
export const createContext = async ({ req }): Promise<any> => {
export const createContext = async ({ req }: { req: Request }): Promise<CustomContext> => {
try {
return await validateTokenAndExtractUser(req)
} catch (e) {
Expand All @@ -21,7 +27,7 @@ export const createContext = async ({ req }): Promise<any> => {
}
}

async function validateTokenAndExtractUser (req: Request): Promise<{ user: AuthUserType, token?: string }> {
async function validateTokenAndExtractUser (req: Request): Promise<CustomContext> {
const { headers } = req
// eslint-disable-next-line @typescript-eslint/dot-notation
const authHeader = String(headers?.['authorization'] ?? '')
Expand Down
9 changes: 5 additions & 4 deletions src/db/edit/streamListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mongoose from 'mongoose'
import { ChangeStream, ChangeStreamDocument, ChangeStreamUpdateDocument } from 'mongodb'
import dot from 'dot-object'

import { changelogDataSource } from '../../model/ChangeLogDataSource.js'
import ChangeLogDataSource from '../../model/ChangeLogDataSource.js'
import { logger } from '../../logger.js'
import {
BaseChangeRecordType,
Expand Down Expand Up @@ -120,7 +120,7 @@ const recordChange = async ({ source, dbOp, fullDocument, updateDescription, _id
updateDescription: dotifyUpdateDescription(updateDescription),
kind: DocumentKind.climbs
}
return await changelogDataSource.record(newDocument).then(async () => await updateClimbIndex(fullDocument as ClimbType, dbOp))
return await ChangeLogDataSource.getInstance().record(newDocument).then(async () => await updateClimbIndex(fullDocument as ClimbType, dbOp))
}
case DocumentKind.areas: {
const newDocument: BaseChangeRecordType = {
Expand All @@ -130,7 +130,7 @@ const recordChange = async ({ source, dbOp, fullDocument, updateDescription, _id
updateDescription: dotifyUpdateDescription(updateDescription),
kind: DocumentKind.areas
}
return await changelogDataSource.record(newDocument).then(async () => await updateAreaIndex(fullDocument as AreaType, dbOp))
return await ChangeLogDataSource.getInstance().record(newDocument).then(async () => await updateAreaIndex(fullDocument as AreaType, dbOp))
}
case DocumentKind.organizations: {
const newDocument: BaseChangeRecordType = {
Expand All @@ -140,7 +140,8 @@ const recordChange = async ({ source, dbOp, fullDocument, updateDescription, _id
updateDescription: dotifyUpdateDescription(updateDescription),
kind: DocumentKind.organizations
}
return await changelogDataSource.record(newDocument).then()
await ChangeLogDataSource.getInstance().record(newDocument)
return
}
default:
exhaustiveCheck(source)
Expand Down
4 changes: 2 additions & 2 deletions src/db/export/Typesense/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const updateAreaIndex = async (area: AreaType | null, op: DBOperation): P
await typesense()?.collections(areaSchema.name).documents().upsert(mongoAreaToTypeSense(area))
break
case 'delete':
await typesense()?.collections(areaSchema.name).documents().delete(area.metadata.area_id.toUUID().toString())
await typesense()?.collections(areaSchema.name).documents(area.metadata.area_id.toUUID().toString()).delete()
break
}
} catch (e) {
Expand Down Expand Up @@ -83,7 +83,7 @@ export const updateClimbIndex = async (climb: ClimbType | null, op: DBOperation)
await typesense()?.collections(climbSchema.name).documents().upsert(mongoClimbToTypeSense(climbExt))
break
case 'delete':
await typesense()?.collections(climbSchema.name).documents().delete(climb._id.toUUID().toString())
await typesense()?.collections(climbSchema.name).documents(climb._id.toUUID().toString()).delete()
break
}
} catch (e) {
Expand Down
6 changes: 5 additions & 1 deletion src/db/import/usa/AreaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const createRoot = async (countryCode: string, shortCode?: string): Promi
throw new Error('ISO code must be alpha 2 or 3')
}
const areaModel = getAreaModel('areas')
const countryNode = createRootNode(isoCountries.toAlpha3(countryCode).toUpperCase())
const code = isoCountries.toAlpha3(countryCode)?.toUpperCase()
if (code == null) {
throw new Error(`Unexpected look up error for ${countryCode}`)
}
const countryNode = createRootNode(code)
const doc = makeDBArea(countryNode)
if (shortCode != null) {
doc.shortCode = shortCode
Expand Down
6 changes: 2 additions & 4 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ export const createIndexes = async (): Promise<void> => {
}

export const gracefulExit = async (exitCode: number = 0): Promise<void> => {
await mongoose.connection.close(function () {
logger.info('Gracefully exiting.')
process.exit(exitCode)
})
await mongoose.connection.close(true)
logger.info('Gracefully exiting.')
}

export const defaultPostConnect = async (changeStreamListener = streamListener): Promise<ChangeStream> => {
Expand Down
3 changes: 1 addition & 2 deletions src/db/utils/jobs/AddCountriesJob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import mongoose from 'mongoose'
import enJson from 'i18n-iso-countries/langs/en.json' assert { type: 'json' }

import { connectDB, gracefulExit } from '../../index.js'
Expand All @@ -13,7 +12,7 @@ const onConnected = async (): Promise<void> => {
}

const insertAllCountries = async (): Promise<void> => {
const areaDS = new MutableAreaDataSource(mongoose.connection.db.collection('areas'))
const areaDS = MutableAreaDataSource.getInstance()
await Promise.all(
Object.keys(enJson.countries).map(async code => {
if (code === 'US') return null
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/area/AreaQueries.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AreaType } from '../../db/AreaTypes'
import { Context } from '../../types'
import { GQLContext } from '../../types'

const AreaQueries = {
cragsWithin: async (_, { filter }, { dataSources }: Context): Promise<AreaType | null> => {
cragsWithin: async (_, { filter }, { dataSources }: GQLContext): Promise<AreaType | null> => {
const { areas } = dataSources
const { bbox, zoom } = filter
return await areas.findCragsWithin(bbox, zoom)
},

countries: async (_, params, { dataSources }: Context): Promise<AreaType[]> => {
countries: async (_, params, { dataSources }: GQLContext): Promise<AreaType[]> => {
const { areas } = dataSources
return await areas.listAllCountries()
}
Expand Down
3 changes: 1 addition & 2 deletions src/graphql/common/typeDef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { gql } from 'apollo-server-express'

import { gql } from 'graphql-tag'
const typeDefs = gql`
scalar Date
scalar MUUID
Expand Down
8 changes: 4 additions & 4 deletions src/graphql/history/HistoryQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
GetAreaHistoryInputFilterType,
GetOrganizationHistoryInputFilterType
} from '../../db/ChangeLogType.js'
import { Context } from '../../types.js'
import { GQLContext } from '../../types.js'

const HistoryQueries = {
getChangeHistory: async (_, { filter }, { dataSources }: Context): Promise<any> => {
getChangeHistory: async (_, { filter }, { dataSources }: GQLContext): Promise<any> => {
const { history } = dataSources
const { uuidList }: GetHistoryInputFilterType = filter ?? {}
// Note: userUuid, fromDate, toDate filters don't currently work.
Expand All @@ -19,14 +19,14 @@ const HistoryQueries = {
return await history.getChangeSets(muidList)
},

getAreaHistory: async (_, { filter }, { dataSources }: Context): Promise<any> => {
getAreaHistory: async (_, { filter }, { dataSources }: GQLContext): Promise<any> => {
const { history } = dataSources
const { areaId }: GetAreaHistoryInputFilterType = filter ?? {}
const id = muid.from(areaId)
return await history.getAreaChangeSets(id)
},

getOrganizationHistory: async (_, { filter }, { dataSources }: Context): Promise<any> => {
getOrganizationHistory: async (_, { filter }, { dataSources }: GQLContext): Promise<any> => {
const { history } = dataSources
const { orgId }: GetOrganizationHistoryInputFilterType = filter ?? {}
return await history.getOrganizationChangeSets(orgId)
Expand Down
Loading
Loading