Mirrored from GitHub
github.com/roostorg/coop
1import { isCoopErrorOfType } from '../../utils/errors.js';
2import { type GQLMutationLoginArgs } from '../generated.js';
3import { type ResolverMap } from '../resolvers.js';
4import { gqlErrorResult, gqlSuccessResult } from '../utils/gqlResult.js';
5
6const typeDefs = /* GraphQL */ `
7 type Query {
8 me: User @publicResolver
9 getSSORedirectUrl(emailAddress: String!): String @publicResolver
10 }
11
12 type Mutation {
13 login(input: LoginInput!): LoginResponse! @publicResolver
14 logout: Boolean
15 }
16
17 input LoginInput {
18 email: String!
19 password: String!
20 remember: Boolean
21 }
22
23 type LoginSuccessResponse {
24 user: User!
25 }
26
27 union LoginResponse =
28 | LoginSuccessResponse
29 | LoginUserDoesNotExistError
30 | LoginIncorrectPasswordError
31 | LoginSsoRequiredError
32
33 type LoginSsoRequiredError implements Error {
34 title: String!
35 status: Int!
36 type: [String!]!
37 pointer: String
38 detail: String
39 requestId: String
40 }
41
42 type LoginUserDoesNotExistError implements Error {
43 title: String!
44 status: Int!
45 type: [String!]!
46 pointer: String
47 detail: String
48 requestId: String
49 }
50
51 type LoginIncorrectPasswordError implements Error {
52 title: String!
53 status: Int!
54 type: [String!]!
55 pointer: String
56 detail: String
57 requestId: String
58 }
59`;
60
61const Query: ResolverMap = {
62 async me(_: unknown, __: unknown, context) {
63 return context.getUser();
64 },
65 async getSSORedirectUrl(_: unknown, { emailAddress }, context) {
66 return context.services.SSOService.getSSORedirectUrlForUserEmail(
67 emailAddress,
68 );
69 },
70};
71
72const Mutation: ResolverMap = {
73 async login(_: unknown, params: GQLMutationLoginArgs, context) {
74 try {
75 return gqlSuccessResult(
76 { user: await context.dataSources.userAPI.login(params, context) },
77 'LoginSuccessResponse',
78 );
79 } catch (e) {
80 if (isCoopErrorOfType(e, 'LoginUserDoesNotExistError')) {
81 return gqlErrorResult(e, '/input/email');
82 } else if (isCoopErrorOfType(e, 'LoginIncorrectPasswordError')) {
83 return gqlErrorResult(e, '/input/password');
84 } else {
85 throw e;
86 }
87 }
88 },
89 async logout(_: unknown, __: unknown, context) {
90 return context.dataSources.userAPI.logout(context);
91 },
92};
93
94const resolvers = {
95 Mutation,
96 Query,
97};
98
99export { typeDefs, resolvers };