|
85 | 85 | </div><section style="max-width: 640px; margin-left: auto; margin-right: auto;"> |
86 | 86 | <article> |
87 | 87 | <header><h1>Executing Queries and Mutations</h1></header> |
88 | | -<p>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 } }</p> |
| 88 | +<p>Before diving further into writing tests for your GraphQL server, please read through the HTTP testing documentation starting with the HTTP/data-node Data node page as much of the same core principles apply to GraphQL also.The main GraphQL specific features are covered in the subsequent pages: GraphQL/queries-and-mutations Queries and Mutations GraphQL/customized-graphql-urls Customized GraphQL URLs GraphQL/report Report 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 } }</p> |
89 | 89 | </article> |
90 | 90 |
|
91 | 91 | <article> |
|
121 | 121 | "page" : { |
122 | 122 | "type" : "Page", |
123 | 123 | "content" : [ { |
| 124 | + "type" : "Paragraph", |
| 125 | + "content" : [ { |
| 126 | + "text" : "Before diving further into writing tests for your GraphQL server, please read through the HTTP testing documentation", |
| 127 | + "type" : "SimpleText" |
| 128 | + }, { |
| 129 | + "type" : "SoftLineBreak" |
| 130 | + }, { |
| 131 | + "text" : "starting with the ", |
| 132 | + "type" : "SimpleText" |
| 133 | + }, { |
| 134 | + "url" : "/webtau/HTTP/data-node", |
| 135 | + "isFile" : false, |
| 136 | + "type" : "Link", |
| 137 | + "content" : [ { |
| 138 | + "text" : "Data node page", |
| 139 | + "type" : "SimpleText" |
| 140 | + } ] |
| 141 | + }, { |
| 142 | + "text" : " as much of the same core principles apply to GraphQL also.", |
| 143 | + "type" : "SimpleText" |
| 144 | + } ] |
| 145 | + }, { |
| 146 | + "type" : "Paragraph", |
| 147 | + "content" : [ { |
| 148 | + "text" : "The main GraphQL specific features are covered in the subsequent pages:", |
| 149 | + "type" : "SimpleText" |
| 150 | + } ] |
| 151 | + }, { |
| 152 | + "bulletMarker" : "*", |
| 153 | + "tight" : true, |
| 154 | + "type" : "BulletList", |
| 155 | + "content" : [ { |
| 156 | + "type" : "ListItem", |
| 157 | + "content" : [ { |
| 158 | + "type" : "Paragraph", |
| 159 | + "content" : [ { |
| 160 | + "url" : "/webtau/GraphQL/queries-and-mutations", |
| 161 | + "isFile" : false, |
| 162 | + "type" : "Link", |
| 163 | + "content" : [ { |
| 164 | + "text" : "Queries and Mutations", |
| 165 | + "type" : "SimpleText" |
| 166 | + } ] |
| 167 | + } ] |
| 168 | + } ] |
| 169 | + }, { |
| 170 | + "type" : "ListItem", |
| 171 | + "content" : [ { |
| 172 | + "type" : "Paragraph", |
| 173 | + "content" : [ { |
| 174 | + "url" : "/webtau/GraphQL/customized-graphql-urls", |
| 175 | + "isFile" : false, |
| 176 | + "type" : "Link", |
| 177 | + "content" : [ { |
| 178 | + "text" : "Customized GraphQL URLs", |
| 179 | + "type" : "SimpleText" |
| 180 | + } ] |
| 181 | + } ] |
| 182 | + } ] |
| 183 | + }, { |
| 184 | + "type" : "ListItem", |
| 185 | + "content" : [ { |
| 186 | + "type" : "Paragraph", |
| 187 | + "content" : [ { |
| 188 | + "url" : "/webtau/GraphQL/report", |
| 189 | + "isFile" : false, |
| 190 | + "type" : "Link", |
| 191 | + "content" : [ { |
| 192 | + "text" : "Report", |
| 193 | + "type" : "SimpleText" |
| 194 | + } ] |
| 195 | + } ] |
| 196 | + } ] |
| 197 | + } ] |
| 198 | + }, { |
124 | 199 | "id" : "executing-queries-and-mutations", |
125 | 200 | "title" : "Executing Queries and Mutations", |
126 | 201 | "type" : "Section", |
|
281 | 356 | } ] |
282 | 357 | } ] |
283 | 358 | } ], |
284 | | - "lastModifiedTime" : 1677186557934, |
| 359 | + "lastModifiedTime" : 1681825041900, |
285 | 360 | "tocItem" : { |
286 | 361 | "chapterTitle" : "GraphQL", |
287 | 362 | "pageTitle" : "Queries And Mutations", |
|
0 commit comments