codeceptjs
Version:
Modern Era Acceptance Testing Framework for NodeJS
123 lines (87 loc) • 2.4 kB
Markdown
---
id: GraphQL
title: GraphQL
---
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
## GraphQL
**Extends Helper**
GraphQL helper allows to send additional requests to a GraphQl endpoint during acceptance tests.
[Axios][1] library is used to perform requests.
## Configuration
- endpoint: GraphQL base URL
- timeout: timeout for requests in milliseconds. 10000ms by default
- defaultHeaders: a list of default headers
- onRequest: a async function which can update request object.
## Example
```js
GraphQL: {
endpoint: 'http://site.com/graphql/',
onRequest: (request) => {
request.headers.auth = '123';
}
}
```
## Access From Helpers
Send GraphQL requests by accessing `_executeQuery` method:
```js
this.helpers['GraphQL']._executeQuery({
url,
data,
});
```
## Methods
### Parameters
- `config`
### _executeQuery
Executes query via axios call
#### Parameters
- `request` **[object][2]**
### _prepareGraphQLRequest
Prepares request for axios call
#### Parameters
- `operation` **[object][2]**
- `headers` **[object][2]**
### sendMutation
Send query to GraphQL endpoint over http
```js
I.sendMutation(`
mutation createUser($user: UserInput!) {
createUser(user: $user) {
id
name
email
}
}
`,
{ user: {
name: 'John Doe',
email: 'john@xmail.com'
}
},
});
```
#### Parameters
- `mutation` **[String][3]**
- `variables` **[object][2]** that may go along with the mutation
- `options` **[object][2]** are additional query options
- `headers` **[object][2]**
### sendQuery
Send query to GraphQL endpoint over http.
Returns a response as a promise.
```js
const response = await I.sendQuery('{ users { name email }}');
// with variables
const response = await I.sendQuery(
'query getUser($id: ID) { user(id: $id) { name email }}',
{ id: 1 },
)
const user = response.data.data;
```
#### Parameters
- `query` **[String][3]**
- `variables` **[object][2]** that may go along with the query
- `options` **[object][2]** are additional query options
- `headers` **[object][2]**
[1]: https://github.com/axios/axios
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String