mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-19 19:21:23 +02:00
Updated Request Matching GraphQLMatcher (markdown)
+140
-55
@@ -1,78 +1,163 @@
|
|||||||
# GraphQL Schema (GraphQLMatcher)
|
# GraphQL Schema (GraphQLMatcher)
|
||||||
Can be used to match a GraphQL `Query` using GraphQL Schema (`MUtation` is not yet supported I think...)
|
Can be used to match a GraphQL `Query` using GraphQL Schema (`Mutation` is not yet supported I think...)
|
||||||
|
|
||||||
## Define a mapping which includes a GraphQL Schema which should be used for matching the body:
|
## Define a mappings which include a GraphQL Schema which should be used for matching that body:
|
||||||
### C#
|
### C#
|
||||||
```csharp
|
```csharp
|
||||||
var TestSchema = @"
|
private const string TestSchemaQueryStudents =
|
||||||
input MessageInput {
|
"""
|
||||||
content: String
|
type Query {
|
||||||
author: String
|
students:[Student]
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message {
|
type Student {
|
||||||
id: ID!
|
id:ID!
|
||||||
content: String
|
firstName:String
|
||||||
author: String
|
lastName:String
|
||||||
}
|
fullName:String
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
type Mutation {
|
private const string TestSchemaQueryStudentById =
|
||||||
createMessage(input: MessageInput): Message
|
"""
|
||||||
updateMessage(id: ID!, input: MessageInput): Message
|
type Query {
|
||||||
}
|
studentById(id:ID!):Student
|
||||||
|
}
|
||||||
|
|
||||||
type Query {
|
type Student {
|
||||||
greeting:String
|
id:ID!
|
||||||
students:[Student]
|
firstName:String
|
||||||
studentById(id:ID!):Student
|
lastName:String
|
||||||
}
|
fullName:String
|
||||||
|
}
|
||||||
type Student {
|
""";
|
||||||
id:ID!
|
|
||||||
firstName:String
|
|
||||||
lastName:String
|
|
||||||
fullName:String
|
|
||||||
}";
|
|
||||||
|
|
||||||
var server = WireMockServer.Start();
|
var server = WireMockServer.Start();
|
||||||
server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
.WithPath("/graphql")
|
.WithPath("/graphql")
|
||||||
.UsingPost()
|
.UsingPost()
|
||||||
.WithGraphQLSchema(TestSchema)
|
.WithGraphQLSchema(TestSchemaQueryStudents)
|
||||||
)
|
)
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
.WithBody("GraphQL is ok")
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody(
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"students": [
|
||||||
|
{
|
||||||
|
"id": "1",
|
||||||
|
"firstName": "Alice",
|
||||||
|
"lastName": "Johnson",
|
||||||
|
"fullName": "Alice Johnson"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "2",
|
||||||
|
"firstName": "Bob",
|
||||||
|
"lastName": "Smith",
|
||||||
|
"fullName": "Bob Smith"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.WithPath("/graphql")
|
||||||
|
.UsingPost()
|
||||||
|
.WithGraphQLSchema(TestSchemaQueryStudentById)
|
||||||
|
.WithBody(new WildcardMatcher("\"sid\": \"1\""))
|
||||||
|
)
|
||||||
|
.WithTitle("Student found")
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody(
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"studentById": {
|
||||||
|
"id": "123",
|
||||||
|
"firstName": "John",
|
||||||
|
"lastName": "Doe",
|
||||||
|
"fullName": "John Doe"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.WithPath("/graphql")
|
||||||
|
.UsingPost()
|
||||||
|
.WithGraphQLSchema(TestSchemaQueryStudentById)
|
||||||
|
)
|
||||||
|
.WithTitle("Student not found")
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBody(
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"data": null
|
||||||
|
}
|
||||||
|
""")
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
### JSON Mapping
|
## Use / Test
|
||||||
|
When WireMock.Net is started (see above) with that GraphQL Schema, a client can send GraphQL:
|
||||||
|
|
||||||
|
### Query Students
|
||||||
|
#### Request
|
||||||
|
``` cmd
|
||||||
|
curl --location 'http://localhost:9091/graphql' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--data '{"query":"{\r\n students {\r\n fullName\r\n id\r\n }\r\n}","variables":{}}'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Response
|
||||||
``` json
|
``` json
|
||||||
{
|
{
|
||||||
"Guid": "5a36d1c1-11df-4976-90f9-22cae5dadb38",
|
"data": {
|
||||||
"UpdatedAt": "2023-07-08T17:02:06.1072879Z",
|
"students": [
|
||||||
"Request": {
|
{
|
||||||
"Path": {
|
"id": "1",
|
||||||
"Matchers": [
|
"firstName": "Alice",
|
||||||
{
|
"lastName": "Johnson",
|
||||||
"Name": "WildcardMatcher",
|
"fullName": "Alice Johnson"
|
||||||
"Pattern": "/graphql",
|
},
|
||||||
"IgnoreCase": false
|
{
|
||||||
}
|
"id": "2",
|
||||||
]
|
"firstName": "Bob",
|
||||||
},
|
"lastName": "Smith",
|
||||||
"Methods": [
|
"fullName": "Bob Smith"
|
||||||
"POST"
|
}
|
||||||
],
|
]
|
||||||
"Body": {
|
}
|
||||||
"Matcher": {
|
|
||||||
"Name": "GraphQLMatcher",
|
|
||||||
"Pattern": "\r\n input MessageInput {\r\n content: String\r\n author: String\r\n }\r\n\r\n type Message {\r\n id: ID!\r\n content: String\r\n author: String\r\n }\r\n\r\n type Mutation {\r\n createMessage(input: MessageInput): Message\r\n updateMessage(id: ID!, input: MessageInput): Message\r\n }\r\n\r\n type Query {\r\n greeting:String\r\n students:[Student]\r\n studentById(id:ID!):Student\r\n }\r\n\r\n type Student {\r\n id:ID!\r\n firstName:String\r\n lastName:String\r\n fullName:String \r\n }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Use / Test
|
### Query Student by Id
|
||||||
When WireMock.Net is started (see above) with that GraphQL Schema, a client can send GraphQL
|
#### Request
|
||||||
|
``` cmd
|
||||||
|
curl --location 'http://localhost:9091/graphql' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--data '{"query":"query ($sid: ID!)\r\n{\r\n studentById(id: $sid) {\r\n fullName\r\n id\r\n }\r\n}","variables":{"sid":"1"}}'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Response
|
||||||
|
``` json
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"studentById": {
|
||||||
|
"id": "123",
|
||||||
|
"firstName": "John",
|
||||||
|
"lastName": "Doe",
|
||||||
|
"fullName": "John Doe"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user