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