Knowledgebase

GraphQL


Background

If you want to know more about using k6 for GraphQL testing, check out the blog post Load testing GraphQL with k6.


Example

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
import http from "k6/http";
import { sleep } from "k6";

let accessToken = "YOUR_GITHUB_ACCESS_TOKEN";

export default function() {

  let query = `
    query FindFirstIssue {
      repository(owner:"loadimpact", name:"k6") {
        issues(first:1) {
          edges {
            node {
              id
              number
              title
            }
          }
        }
      }
    }`;

  let headers = {
    'Authorization': `Bearer ${accessToken}`,
    "Content-Type": "application/json"
  };

  let res = http.post("https://api.github.com/graphql",
    JSON.stringify({ query: query }),
    {headers: headers}
  );

  if (res.status === 200) {
    console.log(JSON.stringify(res.body));
    let body = JSON.parse(res.body);
    let issue = body.data.repository.issues.edges[0].node;
    console.log(issue.id, issue.number, issue.title);

    let mutation = `
      mutation AddReactionToIssue {
        addReaction(input:{subjectId:"${issue.id}",content:HOORAY}) {
          reaction {
            content
          }
          subject {
            id
          }
        }
    }`;

    res = http.post("https://api.github.com/graphql",
      JSON.stringify({query: mutation}),
      {headers: headers}
    );
  }
  sleep(0.3);
}