# GraphQL
Extends Helper
GraphQL helper allows to send additional requests to a GraphQl endpoint during acceptance tests. Axios (opens new window) 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
GraphQL: {
endpoint: 'http://site.com/graphql/',
onRequest: (request) => {
request.headers.auth = '123';
}
}
# Access From Helpers
Send GraphQL requests by accessing _executeQuery method:
this.helpers['GraphQL']._executeQuery({
url,
data,
});
# Methods
# Parameters
config
# _executeQuery
Executes query via axios call
# Parameters
requestobject (opens new window)
# _prepareGraphQLRequest
Prepares request for axios call
# Parameters
operationobject (opens new window)headersobject (opens new window)
Returns object (opens new window) graphQLRequest
# amBearerAuthenticated
Adds a header for Bearer authentication
// we use secret function to hide token from logs
I.amBearerAuthenticated(secret('heregoestoken'))
# Parameters
accessToken(string (opens new window) | CodeceptJS.Secret) Bearer access token
# haveRequestHeaders
Sets request headers for all requests of this test
# Parameters
headersobject (opens new window) headers list
# sendMutation
Send query to GraphQL endpoint over http
I.sendMutation(`
mutation createUser($user: UserInput!) {
createUser(user: $user) {
id
name
email
}
}
`,
{ user: {
name: 'John Doe',
email: '[email protected]'
}
},
});
# Parameters
mutationString (opens new window)variablesobject (opens new window)? that may go along with the mutationoptionsobject (opens new window)? are additional query optionsheadersobject (opens new window)?
Returns any Promise
# sendQuery
Send query to GraphQL endpoint over http. Returns a response as a promise.
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
queryString (opens new window)variablesobject (opens new window)? that may go along with the queryoptionsobject (opens new window)? are additional query optionsheadersobject (opens new window)?
Returns any Promise