Skip to content

Commit

Permalink
Merge pull request #114 from osstotalsoft/apollo-server4-fixes
Browse files Browse the repository at this point in the history
Node Upgrade and  Apollo server v4 fixes
  • Loading branch information
alexandra-c committed Feb 27, 2023
2 parents 4b208c3 + 2a54e01 commit cac94df
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 86 deletions.
32 changes: 32 additions & 0 deletions __tests__/answers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@ const path = require('path')
const rimraf = require('rimraf')
const assert = require('yeoman-assert')
const helpers = require('yeoman-test')
const { projectNameQ, getQuestions } = require('../generators/app/questions')
const { findIndex } = require('ramda')

describe('generator-graphql-rocket:app question validations', () => {
it('project name input does not have an acceptable format', () => {
const name = '& - a!'
const validationResult = projectNameQ.validate(name)
assert.notEqual(validationResult, true)
})

it('project name input has an acceptable format', () => {
const name = 'my-project_a'
const validationResult = projectNameQ.validate(name)
assert.equal(validationResult, true)
})

it('helm chart name input does not have an acceptable format', () => {
const name = '& - a!'
const questions = getQuestions('test')
const qIndex = findIndex(q => q.name === 'helmChartName', questions)
const validationResult = questions[qIndex].validate(name)
assert.notEqual(validationResult, true)
})

it('helm chart name input has an acceptable format', () => {
const name = 'my-chart_a'
const questions = getQuestions('test')
const qIndex = findIndex(q => q.name === 'helmChartName', questions)
const validationResult = questions[qIndex].validate(name)
assert.equal(validationResult, true)
})
})

describe('test package installers', () => {
jest.setTimeout(10 * 1000)
Expand Down
5 changes: 3 additions & 2 deletions __tests__/installers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ const path = require('path')
const rimraf = require('rimraf')
const assert = require('yeoman-assert')
const helpers = require('yeoman-test')
const { NPM_MIN_VERSION, YARN_MIN_VERSION } = require('../generators/app/constants')

describe('test package installers', () => {
jest.setTimeout(10 * 1000)
const projectName = 'test-graphql'
const tempRoot = `../.tmp`
const dbConnectionName = 'testDatabase'
const npm = '>= 7.16.0'
const yarn = '>= 1.22.4'
const npm = `>= ${NPM_MIN_VERSION}`
const yarn = `>= ${YARN_MIN_VERSION}`

beforeAll(() => {
rimraf.sync(path.join(__dirname, tempRoot))
Expand Down
2 changes: 1 addition & 1 deletion generators/app/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports.YO_RC_FILE = '.yo-rc.json'
module.exports.YARN_MIN_VERSION = '1.22.4'
module.exports.NPM_MIN_VERSION = '7.16.0'
module.exports.NPM_MIN_VERSION = '8.x'
2 changes: 1 addition & 1 deletion generators/app/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module.exports.getQuestions = projectName => [
when: prompts => prompts.addHelm,
default: projectName,
validate: name => {
if (name.match(/[a-z0-9]([-a-z0-9]*[a-z0-9])?/)) {
if (name.match(/^((?!-)[A-Za-z-._\d]{1,63}(?<!-))+$/)) {
return true
}

Expand Down
8 changes: 8 additions & 0 deletions generators/app/templates/infrastructure/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
"rules": {
"node/no-unpublished-require": 0
}
},
{
"files": ["*.graphql"],
"parser": "@graphql-eslint/eslint-plugin",
"plugins": ["@graphql-eslint"],
"rules": {
"@graphql-eslint/known-type-names": "error"
}
}
]
}
2 changes: 1 addition & 1 deletion generators/app/templates/infrastructure/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16-slim
FROM node:18-slim

# Create app directory
WORKDIR /usr/src/app
Expand Down
78 changes: 40 additions & 38 deletions generators/app/templates/infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/index.js",
"type": "commonjs",
"engines": {
"node": ">=16.0.0",
"node": ">=18.x",
"<%=packageManager%>": ">= <%= packageManagerVersion %>"
},
"scripts": {
Expand Down Expand Up @@ -33,27 +33,27 @@
"author": "",
"license": "ISC",
"dependencies": {
"@apollo/datasource-rest": "^4.3.2",
"@apollo/server": "^4.1.1",
"@as-integrations/koa": "^0.2.1",
"@graphql-tools/graphql-file-loader": "^7.4.0",
"@graphql-tools/load": "^7.7.0",
"@apollo/datasource-rest": "^5.0.2",
"@apollo/server": "^4.4.0",
"@as-integrations/koa": "^0.3.0",
"@graphql-tools/graphql-file-loader": "^7.5.16",
"@graphql-tools/load": "^7.8.12",
"@graphql-tools/load-files": "^6.6.1",
"@graphql-tools/merge": "^8.3.6",
"@graphql-tools/schema": "^8.5.0",
"@graphql-tools/merge": "^8.3.18",
"@graphql-tools/schema": "^9.0.16",
"@koa/cors": "^4.0.0",
"@koa/router": "^11.0.1",
"@koa/router": "^12.0.0",
"koa-ignore": "^1.0.1",
<%_ if(dataLayer === "prisma") {_%>
"@prisma/client": "^4.10.0",
"prisma": "^4.10.0",
"prisma-case-format": "^1.2.0",
"@prisma/client": "^4.10.1",
"prisma": "^4.10.1",
"prisma-case-format": "^1.3.1",
<%_ if(withMultiTenancy && hasSharedDb) {_%>
"lodash.set": "^4.3.2",
<%_} _%>
<%_} _%>
"@opentelemetry/exporter-prometheus": "^0.27.0",
"@opentelemetry/sdk-metrics-base": "^0.27.0",
"@opentelemetry/exporter-prometheus": "^0.35.1",
"@opentelemetry/sdk-metrics": "^1.9.1",
"@totalsoft/key-per-file-configuration": "^1.1.0",
"@totalsoft/graceful-shutdown": "^1.1.0",
"@totalsoft/correlation": "^1.1.0",
Expand All @@ -78,58 +78,60 @@
"@totalsoft/message-bus": "^2.4.7",
"@totalsoft/messaging-host": "^2.4.2",
<%_}_%>
"async-mutex": "^0.3.2",
"bluebird": "3.7.2",
"async-mutex": "^0.4.0",
"bluebird": "^3.7.2",
"colors": "^1.4.0",
"console-stamp": "^3.0.6",
"console-stamp": "^3.1.1",
"dotenv": "16.0.1",
"graphql": "16.6.0",
<%_ if(addSubscriptions){ _%>
"graphql-redis-subscriptions": "^2.5.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.9.1",
"ioredis": "^5.2.0",
"ws": "^8.8.0",
"graphql-ws": "^5.11.3",
"ioredis": "^5.3.1",
"ws": "^8.12.1",
<%_}_%>
<%_ if(withRights){ _%>
"graphql-middleware": "^6.1.33",
"graphql-shield": "7.6.4",
<%_}_%>
"graphql-upload": "^13.0.0",
"graphql-upload": "^15.0.2",
"humps": "^2.0.1",
<%_ if(addTracing){ _%>
"jaeger-client": "^3.19.0",
"opentracing": "^0.14.7",
"@totalsoft/opentracing": "^1.1.0",
"@totalsoft/pino-opentracing": "^1.1.0",
<%_}_%>
"jsonwebtoken": "8.5.1",
"jwks-rsa": "^2.1.4",
"koa": "^2.13.4",
"jsonwebtoken": "9.0.0",
"jwks-rsa": "^3.0.1",
"koa": "^2.14.1",
"koa-bodyparser": "^4.3.0",
"koa-jwt": "^4.0.3",
"koa-jwt": "^4.0.4",
"mime-types": "^2.1.35",
"numeral": "^2.0.6",
"path": "^0.12.7",
"ramda": "^0.28.0",
"tedious": "^14.7.0",
"uuid": "^8.3.2",
"pino": "^7.6.3",
"tedious": "^15.1.3",
"uuid": "^9.0.0",
"pino": "^8.11.0",
"pino-abstract-transport": "^1.0.0",
"pino-pretty": "^8.1.0"
"pino-pretty": "^9.4.0"
},
"devDependencies": {
"@graphql-eslint/eslint-plugin": "^3.16.0",
"cross-env": "7.0.3",
"eslint": "^8.19.0",
"eslint-plugin-jest": "^26.5.3",
"eslint": "^8.35.0",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-node": "^11.1.0",
"husky": "^8.0.1",
"jest": "^28.1.2",
"jest-extended": "^3.0.0",
"jest-junit": "^14.0.0",
"lint-staged": "^13.0.3",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"graphql-schema-linter": "^3.0.1",
"husky": "^8.0.3",
"jest": "^29.4.3",
"jest-extended": "^3.2.4",
"jest-junit": "^15.0.0",
"lint-staged": "^13.1.2",
"nodemon": "^2.0.20",
"prettier": "^2.8.4",
"run-script-os": "^1.1.6"
},
"jest": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TenantIdentityApi extends NoCacheRESTDataSource {
return `${request.url}${this.context.externalUser.id}`
}

willSendRequest(request) {
willSendRequest(_path, request) {
request.headers = assoc('Authorization', this.context.token, request.headers)
request.headers = assoc('TenantId', this.context.tenant?.id, request.headers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UserApi extends NoCacheRESTDataSource {
this.context = context
}

willSendRequest(request) {
willSendRequest(_path, request) {
request.headers = assoc('Authorization', this.context.token, request.headers)
<%_ if(withMultiTenancy){ _%>
request.headers = assoc('TenantId', this.context.tenantId, request.headers)
Expand Down
18 changes: 4 additions & 14 deletions generators/app/templates/infrastructure/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ const keyPerFileEnv = require('@totalsoft/key-per-file-configuration')
const configMonitor = keyPerFileEnv.load()

require('console-stamp')(global.console, {
format: ':date(yyyy/mm/dd HH:MM:ss.l, utc)'
format: ':date(yyyy/mm/dd HH:MM:ss.l)'
})

const { createServer } = require("http"),
{ startApolloServer<% if(addMessaging) { %>, startMsgHost <% } %> <% if(addSubscriptions){ %>, startSubscriptionServer <% } %>} = require('./servers'),
{ logger } = require("./startup")

const { startApolloServer } = require("./servers/apollo");
<%_ if(addMessaging) {_%>
const startMsgHost = require("./servers/messaging");
<%_}_%>
<%_ if(addSubscriptions){ _%>
const startSubscriptionServer = require("./servers/subscription");
<%_}_%>

<%_ if(dataLayer == 'prisma') {_%>
const { initialize } = require('./prisma')
initialize({ logger })
Expand All @@ -45,7 +38,7 @@ const httpServer = createServer();
<%_ if(addSubscriptions){ _%>
const subscriptionServer = startSubscriptionServer(httpServer);
<%_}_%>
const apolloServer = startApolloServer(httpServer<% if(addSubscriptions) {%>, subscriptionServer<%}%>)
const { cleanup: cleanupApolloServer } = startApolloServer(httpServer<% if(addSubscriptions) {%>, subscriptionServer<%}%>)
<%_ if(addMessaging) {_%>
const msgHost = startMsgHost();
<%_}_%>
Expand All @@ -63,10 +56,7 @@ async function cleanup() {
<%_ if(addMessaging) {_%>
await msgHost?.stop();
<%_}_%>
await (await apolloServer)?.stop();
<%_ if(addTracing) {_%>
defaultTracer?.close();
<%_}_%>
await cleanupApolloServer()
}

const { gracefulShutdown } = require('@totalsoft/graceful-shutdown');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const tenantIdentification = require("./tenantIdentification");
<%_ if(addTracing){ _%>
const tracingMiddleware = require('./tracing/tracingMiddleware');
<%_}_%>
const loggingMiddleware = require('./logger/loggingMiddleware')


module.exports = {
...validateToken,
Expand All @@ -23,5 +25,6 @@ module.exports = {
<%_ if(addTracing){ _%>
tracingMiddleware,
<%_}_%>
errorHandlingMiddleware
errorHandlingMiddleware,
loggingMiddleware
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

const { endpoint, port } = PrometheusExporter.DEFAULT_OPTIONS;
Expand Down
17 changes: 12 additions & 5 deletions generators/app/templates/infrastructure/src/servers/apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const { ApolloServer } = require("@apollo/server"),
<%_ if(addTracing) {_%>
tracingMiddleware,
<%_}_%>
loggingMiddleware,
jwtTokenValidation,
jwtTokenUserIdentification<% if(withMultiTenancy){ %>,tenantIdentification <%}%><% if(dataLayer == "knex") {%>,contextDbInstance <%}%>
} = require("../middleware"),
loggingMiddleware = require("../middleware/logger/loggingMiddleware"),
{ graphqlUploadKoa } = require("graphql-upload"),
graphqlUploadKoa = require('graphql-upload/graphqlUploadKoa.js'),
cors = require("@koa/cors"),
{ publicRoute } = require("../utils/functions"),
ignore = require("koa-ignore"),
Expand All @@ -29,7 +29,7 @@ const { ApolloServer } = require("@apollo/server"),
metricsPlugin = require("../plugins/metrics/metricsPlugin"),
metricsEnabled = JSON.parse(METRICS_ENABLED);
<%_ if(dataLayer == "knex") {_%>
const { dbInstanceFactory } = require("../db");
const { dbInstanceFactory } = require("../db")
<%_}_%>
const plugins = (httpServer<% if(addSubscriptions) {%>, subscriptionServer<%}%>) => {
Expand All @@ -54,8 +54,8 @@ const plugins = (httpServer<% if(addSubscriptions) {%>, subscriptionServer<%}%>)
];
};
logger.info("Creating Apollo Server...");
const startApolloServer = async (httpServer<% if(addSubscriptions) {%>, subscriptionServer<%}%>) => {
logger.info("Creating Apollo Server...");
const apolloServer = new ApolloServer({
schema,
stopOnTerminationSignals: false,
Expand Down Expand Up @@ -108,7 +108,14 @@ const startApolloServer = async (httpServer<% if(addSubscriptions) {%>, subscrip

httpServer.on('request', app.callback())

return apolloServer
const cleanup = async () => {
await (await apolloServer)?.stop()
<%_ if(addTracing) {_%>
defaultTracer?.close();
<%_}_%>
}

return { apolloServer, cleanup }
};

module.exports = { startApolloServer, plugins };
9 changes: 9 additions & 0 deletions generators/app/templates/infrastructure/src/servers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { startApolloServer } = require('./apollo')
<%_ if(addMessaging) { _%>
const startMsgHost = require('./messaging')
<%_ } _%>
<%_ if(addSubscriptions){ _%>
const startSubscriptionServer = require('./subscription')
<%_ } _%>

module.exports = { startApolloServer<% if(addMessaging) { %>, startMsgHost <% } %> <% if(addSubscriptions){ %>, startSubscriptionServer <% } %>}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const { messagingHost, exceptionHandling, SubscriptionOptions, dispatcher } = require("@totalsoft/messaging-host");
const { msgHandlers, middleware } = require("../messaging");
const loggingMiddleware = require("../middleware/logger/loggingMiddleware");
const loggingMiddleware = require("../middleware");
const { logger } = require("../startup");
<%_ if(addTracing){ _%>
const { JAEGER_DISABLED } = process.env,
tracingEnabled = !JSON.parse(JAEGER_DISABLED);

const skipMiddleware = (_ctx, next) => next();
<%_}_%>
const skipMiddleware = (_ctx, next) => next();

const startMsgHost = () => {
module.exports = function startMsgHost() {
const msgHost = messagingHost();
msgHost
.subscribe(Object.keys(msgHandlers), SubscriptionOptions.PUB_SUB)
Expand All @@ -34,5 +35,3 @@ const startMsgHost = () => {
})
return msgHost;
}

module.exports = startMsgHost;
Loading

0 comments on commit cac94df

Please sign in to comment.