You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[Using your GraphQL files as your API Contracts from Central Contract Repository](#using-your-graphql-files-as-your-api-contracts-from-central-contract-repository)
11
+
-[GraphQL](#graphql)
12
+
-[Introduction](#introduction)
13
+
-[What Can You Achieve with Specmatic's GraphQL Support?](#what-can-you-achieve-with-specmatics-graphql-support)
14
+
-[Quick start with sample projects](#quick-start-with-sample-projects)
15
+
-[Starting a stub server](#starting-a-stub-server)
16
+
-[Stubbing out specific values using example data](#stubbing-out-specific-values-using-example-data)
17
+
-[Providing a custom examples directory](#providing-a-custom-examples-directory)
-[Using your GraphQL files as your API Contracts from Central Contract Repository](#using-your-graphql-files-as-your-api-contracts-from-central-contract-repository)
29
28
<!-- TOC -->
30
29
31
30
## Introduction
@@ -50,13 +49,13 @@ Here are sample projects in different languages and frameworks that demonstrate
50
49
51
50
README of each of these projects include
52
51
* detailed animated architecture diagram
53
-
* instructions on how to start stub/mock server using graphql spec with custom examples, simulating delays, errors etc.
52
+
* instructions on how to start stub server using graphql spec with custom examples, simulating delays, errors etc.
54
53
* instructions on how to isolate the System Under Test when running contract tests
55
54
* example CI workflow setups using GitHub Actions
56
55
57
-
## Starting a stub/mock server
56
+
## Starting a stub server
58
57
59
-
To start a stub/mock server using Specmatic, you can use the following steps:
58
+
To start a stub server using Specmatic, you can use the following steps:
60
59
61
60
**Step 1: Create a graphql SDL file**
62
61
@@ -82,7 +81,7 @@ type Product {
82
81
}
83
82
```
84
83
85
-
**Step 2: Startthestub/mockserver**
84
+
**Step 2: Startthestubserver**
86
85
87
86
RunthefollowingcommandtostarttheGraphQLstub:
88
87
@@ -122,9 +121,7 @@ You will receive a response with a random product ID, name, inventory, and type
122
121
}
123
122
```
124
123
125
-
## Customizing stub/mock server
126
-
127
-
### Returning custom responses
124
+
## Stubbing out specific values using example data
128
125
129
126
Above, we saw Specmatic generating random output based on the provided `product-api.graphql` spec. Instead, in order to receive specific example values, you can create an example YAML files containing specific data for `findAvailableProducts` query.
130
127
@@ -156,10 +153,11 @@ response: [
156
153
}
157
154
]
158
155
```
156
+
159
157
{: .note}
160
158
**Note:** Keep the file `product-api.graphql` and directory `product-api_examples` at same level.
161
159
162
-
**Step 2: Start the stub/mock server**
160
+
**Step 2: Start the stub server**
163
161
164
162
Run the following command to start the GraphQL stub:
165
163
@@ -201,11 +199,11 @@ This time though, you will receive a response with the specified example defined
201
199
}
202
200
```
203
201
204
-
#### Providing custom examples dir
202
+
#### Providing a custom examples directory
205
203
206
204
By convention, Specmatic automatically loads all examples defined for the GraphQL SDL file, say `<graphql_sdl_filename>.graphql`, from a colocated directory called `<graphql_sdl_filename>_examples`. However, in case your examples are in a different directory, you can pass it as an argument programmatically or through CLI args while running tests or service virtualization.
207
205
208
-
###Deep dive into examples
206
+
#### Example file file format
209
207
210
208
Let us now take deeper look at the external example format.
211
209
* At the top level we have two YAML root nodes called `request` and `response`
@@ -214,7 +212,7 @@ Let us now take deeper look at the external example format.
214
212
*`headers`: you can add your `HTTP` headers here
215
213
*`response` holds responses with JSON syntax for readability, syntax highlighting and also as an aid to copy and paste of real responses from actual application logs etc.
216
214
217
-
####HTTP Headers
215
+
## HTTP Headers
218
216
219
217
Although GraphQL SDL files do not support HTTP request headers, you may directly add them to your Specmatic example files in `httpHeaders` under the `request` key, as seen in the example yaml below. The headers will be leveraged if present both by the contract tests as well as service virtualization.
220
218
@@ -243,7 +241,7 @@ response: [
243
241
]
244
242
```
245
243
246
-
####GraphQL Variables
244
+
## GraphQL Variables
247
245
248
246
Specmatic supports usage of GraphQL variables seamlessly. You only need to make sure that the externalised example is structured such that it uses the actual field values inline instead of variables in the query. Here is an example.
249
247
@@ -290,7 +288,7 @@ response: [
290
288
{: .note}
291
289
**Note**: It is recommended to specify the type of the variable e.g. `$pageSize: Int!`. If the type is not specified explicitly you may face some issues since Specmatic will implicitly cast the variable to a certain type which may be invalid sometimes.
292
290
293
-
###Delayed responses
291
+
## Delayed responses
294
292
295
293
To simulate delay in the response, you can use the `delay-in-milliseconds` key in your example YAML file. For example:
296
294
@@ -319,10 +317,11 @@ response: [
319
317
]
320
318
321
319
delay-in-milliseconds: 5000
322
-
````
320
+
```
321
+
323
322
This will introduce a 5-second delay before the response is sent back to the client.
324
323
325
-
### Dynamic Field Selection from Example Responses
324
+
## Dynamic Field Selection from Example Responses
326
325
327
326
When running a GraphQL stub using Specmatic, you can provide an example query that includes all possible fields and sub-selections. Specmatic uses this example to generate the stub response.
328
327
@@ -464,7 +463,7 @@ Here are some simple steps to try this out:
464
463
---
465
464
This setup allows you to test how Specmatic reuses the example provided, adapting the response to the requested fields.
466
465
467
-
### Multi-Query Requests
466
+
## Multi-Query Requests
468
467
469
468
The Specmatic GraphQL stub server supports multi-query requests, allowing you to send a single request with multiple queries and receive a consolidated response. This feature is useful when you want to retrieve data from different queries in a single API call. Additionally, **multi-query requests with variables** are supported, making it flexible for dynamic requests.
470
469
@@ -654,7 +653,7 @@ This request showcases how Specmatic's GraphQL stub server can process multi-que
654
653
655
654
This example demonstrates how Specmatic processes multiple queries in a single request and returns the expected responses for each query. You can adapt this setup for various use cases, leveraging the existing folder structure for organizing examples.
656
655
657
-
### GraphQL Scalar Types
656
+
## GraphQL Scalar Types
658
657
659
658
In GraphQL, you can define [custom scalar types](https://graphql.org/learn/schema/#scalar-types) to handle specialized data, such as dates or monetary values, that require specific serialization and deserialization logic. For example:
0 commit comments