mirror of
https://github.com/snowball-tools/snowballtools-base
synced 2025-08-08 14:24:07 +00:00
Refactor resolver methods to service class
This commit is contained in:
parent
a87d5d785d
commit
09f081eafb
@ -31,7 +31,7 @@ export const main = async (): Promise<void> => {
|
||||
});
|
||||
|
||||
const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString();
|
||||
const resolvers = await createResolvers(db, app, service);
|
||||
const resolvers = await createResolvers(app, service);
|
||||
|
||||
await createAndStartServer(typeDefs, resolvers, server);
|
||||
};
|
||||
|
@ -4,7 +4,6 @@ import { DeepPartial, FindOptionsWhere } from 'typeorm';
|
||||
import { OAuthApp } from '@octokit/oauth-app';
|
||||
|
||||
import { Service } from './service';
|
||||
import { Database } from './database';
|
||||
import { Permission } from './entity/ProjectMember';
|
||||
import { Domain } from './entity/Domain';
|
||||
import { Project } from './entity/Project';
|
||||
@ -13,7 +12,7 @@ import { EnvironmentVariable } from './entity/EnvironmentVariable';
|
||||
const log = debug('snowball:database');
|
||||
|
||||
// TODO: Remove Database argument and refactor code to Service
|
||||
export const createResolvers = async (db: Database, app: OAuthApp, service: Service): Promise<any> => {
|
||||
export const createResolvers = async (app: OAuthApp, service: Service): Promise<any> => {
|
||||
return {
|
||||
Query: {
|
||||
// TODO: add custom type for context
|
||||
@ -201,19 +200,17 @@ export const createResolvers = async (db: Database, app: OAuthApp, service: Serv
|
||||
},
|
||||
|
||||
authenticateGitHub: async (_: any, { code }: { code: string }, context: any) => {
|
||||
// TOO: Move to Service class
|
||||
const { authentication: { token } } = await app.createToken({
|
||||
code
|
||||
});
|
||||
|
||||
await db.updateUser(context.userId, { gitHubToken: token });
|
||||
|
||||
return { token };
|
||||
try {
|
||||
return await service.authenticateGitHub(code, context.userId, app);
|
||||
} catch (err) {
|
||||
log(err);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
unauthenticateGitHub: async (_: any, __: object, context: any) => {
|
||||
try {
|
||||
return db.updateUser(context.userId, { gitHubToken: null });
|
||||
return service.unauthenticateGitHub(context.userId, { gitHubToken: null });
|
||||
} catch (err) {
|
||||
log(err);
|
||||
return false;
|
||||
|
@ -1,6 +1,8 @@
|
||||
import assert from 'assert';
|
||||
import { DeepPartial, FindOptionsWhere } from 'typeorm';
|
||||
|
||||
import { OAuthApp } from '@octokit/oauth-app';
|
||||
|
||||
import { Database } from './database';
|
||||
import { Deployment, Environment } from './entity/Deployment';
|
||||
import { Domain } from './entity/Domain';
|
||||
@ -9,7 +11,6 @@ import { Organization } from './entity/Organization';
|
||||
import { Project } from './entity/Project';
|
||||
import { Permission, ProjectMember } from './entity/ProjectMember';
|
||||
import { User } from './entity/User';
|
||||
|
||||
export class Service {
|
||||
private db: Database;
|
||||
|
||||
@ -357,4 +358,18 @@ export class Service {
|
||||
|
||||
return updateResult;
|
||||
}
|
||||
|
||||
async authenticateGitHub (code:string, userId: string, app: OAuthApp): Promise<{token: string}> {
|
||||
const { authentication: { token } } = await app.createToken({
|
||||
code
|
||||
});
|
||||
|
||||
await this.db.updateUser(userId, { gitHubToken: token });
|
||||
|
||||
return { token };
|
||||
}
|
||||
|
||||
async unauthenticateGitHub (userId: string, data: DeepPartial<User>): Promise<boolean> {
|
||||
return this.db.updateUser(userId, data);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user