Add login state query

This commit is contained in:
Dominik Korsa 2021-02-12 11:32:30 +01:00
parent 084f38bcbf
commit a1064b2a10
No known key found for this signature in database
GPG key ID: 546F986F71A6FE6E
5 changed files with 50 additions and 4 deletions

View file

@ -5,6 +5,7 @@ export const getUserQuery = gql`query GetUser($login: String!) {
login login
name name
url url
avatarUrl
} }
} }
`; `;
@ -13,6 +14,7 @@ export interface User {
login: string; login: string;
name: string | null; name: string | null;
url: string; url: string;
avatarUrl: string;
} }
export interface GetUserQueryResult { export interface GetUserQueryResult {

View file

@ -3,8 +3,6 @@ import gql from 'graphql-tag';
export const getViewerQuery = gql`query GetViewer { export const getViewerQuery = gql`query GetViewer {
viewer { viewer {
login login
name
avatarUrl
id id
} }
} }
@ -12,8 +10,6 @@ export const getViewerQuery = gql`query GetViewer {
export interface Viewer { export interface Viewer {
login: string; login: string;
name: string | null;
avatarUrl: string;
id: string; id: string;
} }

View file

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

View file

@ -0,0 +1,15 @@
import { Field, ObjectType } from 'type-graphql';
@ObjectType()
export default class LoginState {
@Field(() => String, {
nullable: true,
})
public name!: string | null;
@Field(() => String)
public login!: string;
@Field(() => String)
public avatarUrl!: string;
}

View file

@ -0,0 +1,31 @@
/* eslint-disable class-methods-use-this */
import { Ctx, Query, Resolver } from 'type-graphql';
import Developer from '../../../../database/entities/developer';
import { getUser } from '../../../../graphql/github/sdk';
import LoginState from '../../models/login-state';
import type { WebsiteAPIContext } from '../../types';
@Resolver(LoginState)
export default class LoginStateResolver {
@Query(() => LoginState, {
nullable: true,
})
public async loginState(
@Ctx() { sessionData }: WebsiteAPIContext,
): Promise<LoginState | null> {
if (!sessionData.loginState) return null;
const developer = await Developer.findOne(sessionData.loginState.developerId);
if (!developer) {
console.error('Developer not found');
// eslint-disable-next-line no-param-reassign
sessionData.loginState = null;
return null;
}
const gitHubUser = await getUser(developer.gitHubLogin);
return {
login: gitHubUser.login,
name: gitHubUser.name,
avatarUrl: gitHubUser.avatarUrl,
};
}
}