-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update graphql and apollo server
- Loading branch information
1 parent
f4efa38
commit d435e25
Showing
18 changed files
with
647 additions
and
1,504 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import express from 'express' | ||
import setupMiddlewares from './middlewares' | ||
import setupApolloServer from './apollo-server' | ||
import setupRoutes from './routes' | ||
import setupSwagger from './swagger' | ||
import setupStaticFiles from './static-files' | ||
|
||
const app = express() | ||
setupApolloServer(app) | ||
setupStaticFiles(app) | ||
setupSwagger(app) | ||
setupMiddlewares(app) | ||
setupRoutes(app) | ||
export default app | ||
import express, { Express } from 'express' | ||
|
||
import setupMiddlewares from '@/main/config/middlewares' | ||
import setupRoutes from '@/main/config/routes' | ||
import setupSwagger from '@/main/config/swagger' | ||
import setupStaticFiles from '@/main/config/static-files' | ||
import { setupApolloServer } from '../graphql/apollo' | ||
|
||
export const setupApp = async (): Promise<Express> => { | ||
const app = express() | ||
setupStaticFiles(app) | ||
setupSwagger(app) | ||
setupMiddlewares(app) | ||
setupRoutes(app) | ||
|
||
const apolloServer = setupApolloServer() | ||
await apolloServer.start() | ||
apolloServer.applyMiddleware({ app }) | ||
|
||
return app | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './apollo-server' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils' | ||
import { ForbiddenError } from 'apollo-server-express' | ||
import { GraphQLSchema } from 'graphql' | ||
|
||
import { makeAuthMiddleware } from '@/main/factories' | ||
import { ForbiddenError, SchemaDirectiveVisitor } from 'apollo-server-express' | ||
import { defaultFieldResolver, GraphQLField } from 'graphql' | ||
|
||
export class AuthDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition (field: GraphQLField<any, any>): any { | ||
const { resolve = defaultFieldResolver } = field | ||
field.resolve = async (parent, args, context, info) => { | ||
const request = { | ||
accessToken: context?.req?.headers?.['x-access-token'] | ||
} | ||
const httpResponse = await makeAuthMiddleware().handle(request) | ||
if (httpResponse.statusCode === 200) { | ||
Object.assign(context.req, httpResponse.body) | ||
return resolve.call(this, parent, args, context, info) | ||
} else { | ||
throw new ForbiddenError(httpResponse.body) | ||
export const authDirectiveTransformer = (schema: GraphQLSchema): GraphQLSchema => { | ||
return mapSchema(schema, { | ||
[MapperKind.OBJECT_FIELD]: (fieldConfig) => { | ||
const authDirective = getDirective(schema, fieldConfig, 'auth') | ||
|
||
if (authDirective) { | ||
const { resolve } = fieldConfig | ||
fieldConfig.resolve = async (parent, args, context, info) => { | ||
const request = { | ||
accessToken: context?.req?.headers?.['x-access-token'] | ||
} | ||
const httpResponse = await makeAuthMiddleware().handle(request) | ||
if (httpResponse.statusCode === 200) { | ||
Object.assign(context.req, httpResponse.body) | ||
return resolve.call(this, parent, args, context, info) | ||
} else { | ||
throw new ForbiddenError(httpResponse.body) | ||
} | ||
} | ||
} | ||
return fieldConfig | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
import { AuthDirective } from './auth-directive' | ||
|
||
export default { | ||
auth: AuthDirective | ||
} | ||
export * from './auth-directive' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import 'module-alias/register' | ||
import app from '@/main/config/app' | ||
|
||
import env from '@/main/config/env' | ||
import { MongoHelper } from '@/infra/db' | ||
|
||
MongoHelper.connect(env.mongoUrl) | ||
.then(() => { | ||
.then(async () => { | ||
const { setupApp } = await import('./config/app') | ||
const app = await setupApp() | ||
app.listen(env.port, () => console.log(`Server running at http://localhost:${env.port}`)) | ||
}) | ||
.catch(e => console.error(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters