1import gql from 'graphql-tag';
2
3import { CurrentUserQuery } from '../../../graphql/generated';
4import { graphqlClient, withErrorHandlingAsync } from '../client';
5
6export const UserQuery = {
7  async currentUserAsync(): Promise<CurrentUserQuery['meActor']> {
8    const data = await withErrorHandlingAsync(
9      graphqlClient
10        .query<CurrentUserQuery>(
11          gql`
12            query CurrentUser {
13              meActor {
14                __typename
15                id
16                ... on UserActor {
17                  primaryAccount {
18                    id
19                  }
20                  username
21                }
22                ... on Robot {
23                  firstName
24                }
25                accounts {
26                  id
27                  users {
28                    actor {
29                      id
30                    }
31                    permissions
32                  }
33                }
34              }
35            }
36          `,
37          /* variables */ undefined,
38          {
39            additionalTypenames: ['User', 'SSOUser'],
40          }
41        )
42        .toPromise()
43    );
44
45    return data.meActor;
46  },
47};
48