Add application queries

This commit is contained in:
Dominik Korsa 2021-02-13 13:34:29 +01:00
parent 36a93b10d1
commit f1e42085be
No known key found for this signature in database
GPG key ID: 9F6F7E66CBF4C1D9
4 changed files with 57 additions and 9 deletions

View file

@ -11,7 +11,7 @@ import CreateUserResolver from './resolvers/authenticate-prompt/create-user-reso
import LoginResolver from './resolvers/authenticate-prompt/login-resolver';
import PromptInfoResolver from './resolvers/authenticate-prompt/prompt-info-resolver';
import SetSymbolResolver from './resolvers/authenticate-prompt/set-symbol-resolver';
import CreateApplicationResolver from './resolvers/developer/create-application';
import ApplicationResolver from './resolvers/developer/application';
import LoginStateResolver from './resolvers/developer/get-login-state';
import type { WebsiteAPIContext } from './types';
@ -24,7 +24,7 @@ export default async function registerWebsiteApi(server: MyFastifyInstance): Pro
SetSymbolResolver,
CreateUserResolver,
LoginStateResolver,
CreateApplicationResolver,
ApplicationResolver,
],
});
const apolloServer = new ApolloServer({

View file

@ -1,7 +1,28 @@
import { Field, ObjectType } from 'type-graphql';
import type Application from '../../../database/entities/application';
@ObjectType()
export default class ApplicationInfo {
@Field(() => String)
public id!: string;
@Field(() => String)
public name!: string;
@Field(() => String, {
nullable: true,
})
public iconUrl!: string | null;
@Field(() => String)
public iconColor!: string;
public static fromEntity(entity: Application): ApplicationInfo {
return {
id: entity._id.toHexString(),
iconColor: entity.iconColor,
iconUrl: entity.iconUrl,
name: entity.name,
};
}
}

View file

@ -1,14 +1,14 @@
/* eslint-disable class-methods-use-this */
import { UserInputError } from 'apollo-server-fastify';
import {
Arg, Ctx, Mutation, Resolver, UnauthorizedError,
Arg, Ctx, Mutation, Query, Resolver, UnauthorizedError,
} from 'type-graphql';
import Application from '../../../../database/entities/application';
import ApplicationInfo from '../../models/application-info';
import type { WebsiteAPIContext } from '../../types';
@Resolver(() => ApplicationInfo)
export default class CreateApplicationResolver {
@Resolver()
export default class ApplicationResolver {
@Mutation(() => ApplicationInfo)
public async createApplication(
@Arg('name') name: string,
@ -28,8 +28,33 @@ export default class CreateApplicationResolver {
application.name = name;
await application.save();
return {
id: application._id.toHexString(),
};
return ApplicationInfo.fromEntity(application);
}
@Query(() => [ApplicationInfo])
public async applications(
@Ctx() { sessionData }: WebsiteAPIContext,
): Promise<ApplicationInfo[]> {
if (!sessionData.loginState) throw new UnauthorizedError();
const applications = await Application.find({
where: {
developerId: sessionData.loginState.developerId,
},
});
return applications.map((app) => ApplicationInfo.fromEntity(app));
}
@Query(() => ApplicationInfo, {
nullable: true,
})
public async application(
@Arg('id') id: string,
@Ctx() { sessionData }: WebsiteAPIContext,
): Promise<ApplicationInfo | null> {
if (!sessionData.loginState) throw new UnauthorizedError();
const application = await Application.findOne(id);
if (!application) return null;
if (!application.developerId.equals(sessionData.loginState.developerId)) throw new UnauthorizedError();
return ApplicationInfo.fromEntity(application);
}
}

View file

@ -64,7 +64,9 @@ export default class NewAppDialog extends Vue {
@Watch('value')
valueChanged(value: boolean) {
if (!value) this.name = '';
if (value) return;
this.name = '';
this.error = false;
}
get valid() {