Skip to content

Commit

Permalink
Fix graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Apr 22, 2022
1 parent d829c11 commit 20fac0a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
6 changes: 2 additions & 4 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { RuntimeConfig } from '@nuxt/schema'

export enum Environment {
/**
* Locally, on the developers machine.
Expand Down Expand Up @@ -33,7 +31,7 @@ function getEnvironment(): Environment {
: Environment.LocalDevelopment
}

export function constructConfig(): RuntimeConfig {
export function constructConfig() {
return {
redis: {
port: Number(process.env.REDIS_PORT) || 6380,
Expand All @@ -46,6 +44,6 @@ export function constructConfig(): RuntimeConfig {
},
public: {
environment: getEnvironment(),
}
},
}
}
4 changes: 2 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export default defineNuxtConfig({

nitro: {
hooks: {
'nitro:rollup:before'(ctx) {
'rollup:before'(ctx) {
// Needed for emitting decorator metadata (which is not supported by esbuild)
ctx.rollupConfig?.plugins?.unshift(typescript())
ctx.options.rollupConfig?.plugins?.unshift(typescript())
},
},
// Prevent 'reflect-metadata' from being treeshaked (since we don't explicitly use the import it would otherwise be removed)
Expand Down
13 changes: 13 additions & 0 deletions patches/nitropack+0.3.5.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/nitropack/dist/runtime/entries/nitro-dev.mjs b/node_modules/nitropack/dist/runtime/entries/nitro-dev.mjs
index 943f3e9..9b691a3 100644
--- a/node_modules/nitropack/dist/runtime/entries/nitro-dev.mjs
+++ b/node_modules/nitropack/dist/runtime/entries/nitro-dev.mjs
@@ -28,5 +28,6 @@ server.listen(listenAddress, () => {
address: typeof _address === "string" ? { socketPath: _address } : { host: "localhost", port: _address.port }
});
});
-process.on("unhandledRejection", (err) => console.error("[nitro] [dev] [unhandledRejection] " + err));
-process.on("uncaughtException", (err) => console.error("[nitro] [dev] [uncaughtException] " + err));
+// CHANGED: Better error handling with stack traces
+process.on("unhandledRejection", (err) => console.error("[nitro] [dev] [unhandledRejection]", err));
+process.on("uncaughtException", (err) => console.error("[nitro] [dev] [uncaughtException]", err));
14 changes: 6 additions & 8 deletions server/api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
ApolloServerPluginDrainHttpServer,
ApolloServerPluginLandingPageLocalDefault,
} from 'apollo-server-core'
import { defineEventHandler } from 'h3'
import { Environment } from '../../config'
import { configure as configureTsyringe } from '../tsyringe.config'
import { buildContext } from '../context'
import { loadSchema } from '../schema'
import PassportInitializer from '../user/passport-initializer'
import { useRuntimeConfig } from '#app'
const config = useRuntimeConfig().public
// import { useRuntimeConfig } from 'nuxt/dist/app'
// const config = useRuntimeConfig().public
const config = {
environment: Environment.LocalDevelopment,
}

// Create express instance
const app = express()
Expand Down Expand Up @@ -50,8 +52,4 @@ void configureTsyringe().then(() => {
void startServer()
})

export default defineEventHandler((event) => {
return {
api: 'works',
}
})
export default app
13 changes: 11 additions & 2 deletions server/user/passport-initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ import { RedisClientType } from 'redis'
import { inject, injectable } from 'tsyringe'
import { AuthService } from './auth.service'
import EmailStrategy from './auth.email.strategy'
const config = useRuntimeConfig()
// const config = useRuntimeConfig()
import { Environment } from '~/config'
const config = {
public: {
environment: Environment.LocalDevelopment,
},
session: {
primarySecret: 'session_secret',
secondarySecret: 'session_secret',
},
}

@injectable()
export default class PassportInitializer {
Expand Down Expand Up @@ -52,7 +61,7 @@ export default class PassportInitializer {
name: 'session',
cookie: {
// Serve secure cookies (requires HTTPS, so only in production)
secure: config.environment === Environment.Production,
secure: config.public.environment === Environment.Production,
// Blocks the access cooky from javascript, preventing XSS attacks
httpOnly: true,
// Blocks sending a cookie in a cross-origin request, protects somewhat against CORS attacks
Expand Down
10 changes: 9 additions & 1 deletion server/utils/services.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import { promisify } from 'util'
import redis, { RedisClientType } from 'redis'
import { Environment } from '~/config'
const config = useRuntimeConfig().public
// const config = useRuntimeConfig().public
const config = {
environment: Environment.LocalDevelopment,
redis: {
host: 'localhost',
port: 6380,
password: 'jabref',
},
}

export async function createRedisClient(): Promise<RedisClientType<any, any>> {
if (config.environment === Environment.LocalDevelopment) {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@nuxt/types",
"@types/node",
"@types/jest",
"vue-apollo",
"@pinia/nuxt",
]
},
Expand Down

0 comments on commit 20fac0a

Please sign in to comment.