File size: 3,203 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import http from 'k6/http'
import { Rotation } from './rotation.ts'
import { User } from './user.ts'
import { login } from './login.ts'

type Node = {
  id: string
}

interface CreateableClass<T, P> {
  new (gql: GraphQL, id: string): T
  randParams(gqp: GraphQL): P
  name: string
}

// GraphQL is a helper class to interact
// with the GraphQL API.
export class GraphQL {
  constructor(
    public token: string,
    public host: string = 'http://localhost:3030',
  ) {}

  // We use any here since we don't have an automated way to type GraphQL queries yet. Types are introduced on the method level.
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  query(query: string, variables: Record<string, unknown> = {}): any {
    const res = http.post(
      this.host + '/api/graphql',
      JSON.stringify({ query, variables }),
      {
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${this.token}`,
        },
      },
    )

    if (res.status !== 200) {
      console.log('Sent:', JSON.stringify({ query, variables }))
      throw new Error(`Unexpected status code: ${res.status}\n` + res.body)
    }

    const body = JSON.parse(res.body as string) as unknown as {
      errors: unknown[]
      data: object
    }

    if (body.errors) {
      throw new Error(`GraphQL errors: ${JSON.stringify(body.errors)}`)
    }

    return body.data
  }

  // userID returns the ID of the currently logged in user.
  get userID(): string {
    return this.query(
      `{
        user {
          id
        }
      }`,
    ).user.id
  }

  protected create<T>(ClassType: CreateableClass<T, unknown>): T {
    return this.createWith(ClassType, ClassType.randParams(this))
  }

  private createWith<T, P>(ClassType: CreateableClass<T, P>, params: P): T {
    const id = this.query(
      `mutation Create${ClassType.name}($input: Create${ClassType.name}Input!){create${ClassType.name}(input: $input){id}}`,
      {
        input: params,
      },
    )[`create${ClassType.name}`].id
    return new ClassType(this, id)
  }

  protected list<T>(ClassType: CreateableClass<T, unknown>): T[] {
    return this.query(`{${ClassType.name.toLowerCase()}s{nodes{id}}}`)[
      `${ClassType.name}s`
    ].nodes.map((n: Node) => new ClassType(this, n.id))
  }

  get rotations(): Rotation[] {
    return this.list(Rotation)
  }

  rotation(id: string): Rotation {
    return new Rotation(this, id)
  }

  genRotation(): Rotation {
    return this.create(Rotation)
  }

  get users(): User[] {
    return this.query(`{users{nodes{id}}}`).users.nodes.map((n: Node) =>
      this.user(n.id),
    )
  }

  user(id: string): User {
    return new User(this, id)
  }

  genUser(): User {
    return this.create(User)
  }

  // newAdmin will generate a user and then log in as that user, returning the token.
  //
  // The returned User object is the admin user, linked to the _original_ token (which makes it safe to call delete() on it).
  newAdmin(): [GraphQL, User] {
    const params = User.randParams(this)
    params.role = 'admin'
    const user = this.createWith(User, params)
    return [new GraphQL(login(params.username, params.password)), user]
  }
}