GraphQL - add support for standard scalar types in the schema (#1011)

* GraphQL: register BuiltInTypes

* GraphQLMatcher_For_ValidSchema_And_CorrectGraphQL_Mutation_IsMatch
This commit is contained in:
Stef Heyenrath
2023-10-16 16:13:53 +02:00
committed by GitHub
parent 62fa4666b5
commit f7cd4b100e
2 changed files with 38 additions and 10 deletions

View File

@@ -137,9 +137,14 @@ public class GraphQLMatcher : IStringMatcher
} }
} }
private static ISchema BuildSchema(string schema) private static ISchema BuildSchema(string typeDefinitions)
{ {
return Schema.For(schema); var schema = Schema.For(typeDefinitions);
// #984
schema.RegisterTypes(schema.BuiltInTypeMappings.Select(x => x.graphType).ToArray());
return schema;
} }
} }
#endif #endif

View File

@@ -11,7 +11,10 @@ namespace WireMock.Net.Tests.Matchers;
public class GraphQLMatcherTests public class GraphQLMatcherTests
{ {
private const string TestSchema = @" private const string TestSchema = @"
scalar DateTime
input MessageInput { input MessageInput {
date: DateTime
content: String content: String
author: String author: String
} }
@@ -24,20 +27,21 @@ public class GraphQLMatcherTests
type Mutation { type Mutation {
createMessage(input: MessageInput): Message createMessage(input: MessageInput): Message
createAnotherMessage(date: DateTime, content: String, author: String): Message
updateMessage(id: ID!, input: MessageInput): Message updateMessage(id: ID!, input: MessageInput): Message
} }
type Query { type Query {
greeting:String greeting: String
students:[Student] students: [Student]
studentById(id:ID!):Student studentById(id: ID!):Student
} }
type Student { type Student {
id:ID! id: ID!
firstName:String firstName: String
lastName:String lastName: String
fullName:String fullName: String
}"; }";
[Fact] [Fact]
@@ -57,7 +61,7 @@ public class GraphQLMatcherTests
} }
[Fact] [Fact]
public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQLQuery_IsMatch() public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQL_Query_IsMatch()
{ {
// Arrange // Arrange
var input = @"{ var input = @"{
@@ -76,6 +80,25 @@ public class GraphQLMatcherTests
matcher.GetPatterns().Should().Contain(TestSchema); matcher.GetPatterns().Should().Contain(TestSchema);
} }
[Fact]
public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQL_Mutation_IsMatch()
{
// Arrange
var input = @"{
""query"": ""mutation CreateAnotherMessage($date: DateTime!, $content: String!, $author: String!) { createAnotherMessage(date: $date, content: $content, author: $author) { id } }"",
""variables"": { ""date"": ""2007-12-03T10:15:30Z"", ""content"": ""--content--"", ""author"": ""--author--"" }
}";
// Act
var matcher = new GraphQLMatcher(TestSchema);
var result = matcher.IsMatch(input);
// Assert
result.Score.Should().Be(MatchScores.Perfect);
matcher.GetPatterns().Should().Contain(TestSchema);
}
[Fact] [Fact]
public void GraphQLMatcher_For_ValidSchema_And_IncorrectQuery_IsMismatch() public void GraphQLMatcher_For_ValidSchema_And_IncorrectQuery_IsMismatch()
{ {