Executing Queries and Mutations

Webtau follows GraphQL's https://graphql.org/learn/serving-over-http/ Serving over HTTP best practices when invoking GraphQL servers over HTTP.It therefore assumes the server responds to requests to /graphql so you do not need to specify that in the URL in your configuration. Requests allow providing:a query/mutation string variables an operation name Webtau will default to issuing POST requests according to the https://graphql.org/learn/serving-over-http/#post-request best practices and will expect a 200 status code and a response with a data or errors field.The following example demonstrates most of these query features: package scenarios.graphql import static org.testingisdocumenting.webtau.WebTauGroovyDsl.* def listAllQuery = ''' { allTasks(uncompletedOnly: false) { id description } } ''' def taskByIdQuery = ''' query taskById($id: ID!) { taskById(id: $id) { id description completed } } ''' def completeMutation = ''' mutation complete($id: ID!) { complete(id: $id) } ''' scenario("list all tasks") { graphql.execute(listAllQuery) { // Execute a simple query with no variables errors.should == null // Validate there were no errors body.data.allTasks.id.should == ["a", "b", "c"] // Access response data with the full path allTasks.id.should == ["a", "b", "c"] // Access response data via a shortcut allowing omitting of `body.data` id.should == ["a", "b", "c"] // For single query requests, access response data via a shortcut allowing omitting of `body.data` and the query name } } scenario("complete a task") { graphql.execute(completeMutation, [id: "a"]) { // Execute a mutation with a variables map errors.should == null complete.should == true } graphql.execute(taskByIdQuery, [id: "a"]) { errors.should == null taskById.id.should == "a" taskById.completed.should == true } } scenario("cannot complete a completed task") { graphql.execute(completeMutation, [id: "b"]) { // Execute a mutation with a variables map errors.should == null complete.should == true } graphql.execute(completeMutation, [id: "b"]) { // force an error errors[0].message.shouldNot == null complete.should == null } }

Response Assertions

Response assertions follow a similar pattern to REST APIs.For Groovy specifically, there are shortcuts for accessing data in the response directly as demonstrated in the example above. You may access errors directly via errors or fields in the response directly with the field names, omitting the data field.