mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-19 19:41:15 +02:00
Updated Request Matching GraphQLMatcher (markdown)
+140
-55
@@ -1,78 +1,163 @@
|
||||
# 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#
|
||||
```csharp
|
||||
var TestSchema = @"
|
||||
input MessageInput {
|
||||
content: String
|
||||
author: String
|
||||
}
|
||||
private const string TestSchemaQueryStudents =
|
||||
"""
|
||||
type Query {
|
||||
students:[Student]
|
||||
}
|
||||
|
||||
type Message {
|
||||
id: ID!
|
||||
content: String
|
||||
author: String
|
||||
}
|
||||
type Student {
|
||||
id:ID!
|
||||
firstName:String
|
||||
lastName:String
|
||||
fullName:String
|
||||
}
|
||||
""";
|
||||
|
||||
type Mutation {
|
||||
createMessage(input: MessageInput): Message
|
||||
updateMessage(id: ID!, input: MessageInput): Message
|
||||
}
|
||||
private const string TestSchemaQueryStudentById =
|
||||
"""
|
||||
type Query {
|
||||
studentById(id:ID!):Student
|
||||
}
|
||||
|
||||
type Query {
|
||||
greeting:String
|
||||
students:[Student]
|
||||
studentById(id:ID!):Student
|
||||
}
|
||||
|
||||
type Student {
|
||||
id:ID!
|
||||
firstName:String
|
||||
lastName:String
|
||||
fullName:String
|
||||
}";
|
||||
type Student {
|
||||
id:ID!
|
||||
firstName:String
|
||||
lastName:String
|
||||
fullName:String
|
||||
}
|
||||
""";
|
||||
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/graphql")
|
||||
.UsingPost()
|
||||
.WithGraphQLSchema(TestSchema)
|
||||
.WithGraphQLSchema(TestSchemaQueryStudents)
|
||||
)
|
||||
.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
|
||||
{
|
||||
"Guid": "5a36d1c1-11df-4976-90f9-22cae5dadb38",
|
||||
"UpdatedAt": "2023-07-08T17:02:06.1072879Z",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/graphql",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"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 }"
|
||||
}
|
||||
}
|
||||
}
|
||||
"data": {
|
||||
"students": [
|
||||
{
|
||||
"id": "1",
|
||||
"firstName": "Alice",
|
||||
"lastName": "Johnson",
|
||||
"fullName": "Alice Johnson"
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"firstName": "Bob",
|
||||
"lastName": "Smith",
|
||||
"fullName": "Bob Smith"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Use / Test
|
||||
When WireMock.Net is started (see above) with that GraphQL Schema, a client can send GraphQL
|
||||
### Query Student by Id
|
||||
#### 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