Skip to content

Commit bd1692d

Browse files
committedMar 4, 2024·
add graphql steps
1 parent cb13221 commit bd1692d

8 files changed

+93
-29
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22
lib/
33
test-e2e/report.xml
4+
.idea
5+
report.html

‎CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.18.0
2+
- added support of GraphQL along with specific steps:
3+
- _I create GraphQL request {string}_
4+
- _I add {gqlRequestProperty} to GraphQL {string}:_
5+
16
## 0.17.0
27
- added experimental support of websockets (API may change in future)
38

‎package-lock.json

+2-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-api",
3-
"version": "0.17.0",
3+
"version": "0.18.0",
44
"description": "api steps for @qavajs project",
55
"main": "./index.js",
66
"scripts": {
@@ -11,11 +11,6 @@
1111
"test:e2e": "qavajs run --config test-e2e/api.ts",
1212
"debug:e2e": "qavajs run --config test-e2e/api.ts --tags @debug"
1313
},
14-
"husky": {
15-
"hooks": {
16-
"pre-commit": "npm run lint:write"
17-
}
18-
},
1914
"repository": {
2015
"type": "git",
2116
"url": "git+https://github.com/qavajs/steps-api.git"
@@ -57,7 +52,6 @@
5752
"eslint-plugin-import": "^2.29.0",
5853
"eslint-plugin-prettier": "^5.0.1",
5954
"express": "^4.18.2",
60-
"husky": "^8.0.3",
6155
"prettier": "^3.0.3",
6256
"ts-node": "^10.9.1",
6357
"typescript": "^5.2.2",

‎src/GraphQl.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default class GraphQl {
2+
method = 'POST';
3+
headers = {'Content-Type': 'application/json'}
4+
_query = '';
5+
_variables = {};
6+
body = '';
7+
8+
private updateBody() {
9+
this.body = JSON.stringify({query: this._query, variables: this._variables});
10+
};
11+
12+
set query(query: string) {
13+
this._query = query;
14+
this.updateBody();
15+
};
16+
17+
set variables(variables: string) {
18+
this._variables = JSON.parse(variables);
19+
this.updateBody();
20+
};
21+
}

‎src/apiConstructionSteps.ts

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import memory from '@qavajs/memory';
2-
import { DataTable, When } from '@cucumber/cucumber';
3-
import { dataTable2Object, sendHttpRequest } from './utils';
2+
import {DataTable, When} from '@cucumber/cucumber';
3+
import {dataTable2Object, sendHttpRequest} from './utils';
4+
import GraphQl from './GraphQl';
45

56
/**
67
* Create request template and save it to memory
@@ -13,6 +14,16 @@ When('I create {string} request {string}', function (method: string, key: string
1314
memory.setValue(key, { method });
1415
});
1516

17+
/**
18+
* Create GraphQL request template and save it to memory
19+
* @example
20+
* When I create GraphQL request 'request'
21+
*/
22+
When('I create GraphQL request {string}', function (key: string) {
23+
const blankRequest = new GraphQl();
24+
memory.setValue(key, blankRequest);
25+
});
26+
1627
/**
1728
* Add data table of headers to request
1829
* @param {string} requestKey - memory key of request
@@ -24,7 +35,7 @@ When('I create {string} request {string}', function (method: string, key: string
2435
*/
2536
When('I add headers to {string}:', async function (requestKey: string, headersDataTable: DataTable) {
2637
const request: RequestInit = await memory.getValue(requestKey);
27-
request.headers = await dataTable2Object(headersDataTable);
38+
request.headers = Object.assign(await dataTable2Object(headersDataTable), request.headers);
2839
});
2940

3041
/**
@@ -37,7 +48,7 @@ When('I add headers to {string}:', async function (requestKey: string, headersDa
3748
*/
3849
When('I add {string} headers to {string}', async function (headersKey: string, requestKey: string) {
3950
const request: RequestInit = await memory.getValue(requestKey);
40-
request.headers = await memory.getValue(headersKey);
51+
request.headers = Object.assign(await memory.getValue(headersKey), request.headers);
4152
});
4253

4354
/**
@@ -58,6 +69,28 @@ When('I add body to {string}:', async function (requestKey: string, body: string
5869
request.body = await memory.getValue(body);
5970
});
6071

72+
/**
73+
* Add query or variables to GraphQL request.
74+
* @param {string} property - one of GraphQl specific properties "query" or "variables"
75+
* @param {string} requestKey - memory key of request
76+
* @param {string} valueString - multiline string to be set as GraphQl body value.
77+
*
78+
* @example
79+
* When I add query to GraphQL '$request':
80+
* """
81+
* query {
82+
* characters(page: 2, filter: { name: "rick" }) {
83+
* results {
84+
* name
85+
* }
86+
* }
87+
* }
88+
**/
89+
When('I add {gqlRequestProperty} to GraphQL {string}:', async function (property: string, requestKey: string, valueString: string) {
90+
const request: any = await memory.getValue(requestKey);
91+
request[property] = (await memory.getValue(valueString));
92+
});
93+
6194
/**
6295
* Add form data body to request
6396
* @param {string} requestKey - memory key of request

‎src/parameterTypes.ts

+11
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ defineParameterType({
2424
transformer: (p) => p,
2525
useForSnippets: false,
2626
});
27+
28+
/**
29+
* Used to initialize one of two possible GraphQl body properties
30+
*
31+
* @returns {string}
32+
*/
33+
defineParameterType({
34+
name: 'gqlRequestProperty',
35+
regexp: /query|variables/,
36+
transformer: s => s
37+
});

‎test-e2e/features/apiConstruction.feature

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
Feature: Construction API
22

3+
Scenario: GraphQL send
4+
Given I create GraphQL request 'request'
5+
And I add 'https://rickandmortyapi.com/graphql' url to '$request'
6+
And I add query to GraphQL '$request':
7+
"""
8+
query { characters(page: 2, filter: { name: "rick" }) {
9+
results { name }
10+
}
11+
}
12+
"""
13+
And I send '$request' request and save response as 'response'
14+
And I parse '$response' body as json
15+
Then Response '$response' Status Code to be equal '200'
16+
317
Scenario: Verify simple send
418
Given I create 'GET' request 'request'
519
And I add 'https://jsonplaceholder.typicode.com/todos/1' url to '$request'

0 commit comments

Comments
 (0)
Please sign in to comment.