1
0

api-request.js 760 B

1234567891011121314151617181920212223242526272829
  1. // @ts-check
  2. import http from 'k6/http';
  3. import { check, fail } from 'k6';
  4. export class ShopApiRequest {
  5. constructor(fileName) {
  6. this.document = open('../graphql/' + fileName);
  7. }
  8. /**
  9. * Post the GraphQL request
  10. */
  11. post(variables = {}) {
  12. const res = http.post('http://localhost:3000/shop-api/', {
  13. query: this.document,
  14. variables: JSON.stringify(variables),
  15. }, {
  16. timeout: 120 * 1000,
  17. });
  18. check(res, {
  19. 'Did not error': r => r.json().errors == null && r.status === 200,
  20. });
  21. const result = res.json();
  22. if (result.errors) {
  23. fail('Errored: ' + result.errors[0].message);
  24. }
  25. return res.json();
  26. }
  27. }