Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/WireMock.Net/Matchers/GraphQLMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,14 @@ private static bool TryGetGraphQLRequest(string input, [NotNullWhen(true)] out G
}
}

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
39 changes: 31 additions & 8 deletions test/WireMock.Net.Tests/Matchers/GraphQLMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ namespace WireMock.Net.Tests.Matchers;
public class GraphQLMatcherTests
{
private const string TestSchema = @"
scalar DateTime

input MessageInput {
date: DateTime
content: String
author: String
}
Expand All @@ -24,20 +27,21 @@ type Message {

type Mutation {
createMessage(input: MessageInput): Message
createAnotherMessage(date: DateTime, content: String, author: String): Message
updateMessage(id: ID!, input: MessageInput): Message
}

type Query {
greeting:String
students:[Student]
studentById(id:ID!):Student
greeting: String
students: [Student]
studentById(id: ID!):Student
}

type Student {
id:ID!
firstName:String
lastName:String
fullName:String
id: ID!
firstName: String
lastName: String
fullName: String
}";

[Fact]
Expand All @@ -57,7 +61,7 @@ public void GraphQLMatcher_For_ValidSchema_And_CorrectQuery_IsMatch()
}

[Fact]
public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQLQuery_IsMatch()
public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQL_Query_IsMatch()
{
// Arrange
var input = @"{
Expand All @@ -76,6 +80,25 @@ public void GraphQLMatcher_For_ValidSchema_And_CorrectGraphQLQuery_IsMatch()
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]
public void GraphQLMatcher_For_ValidSchema_And_IncorrectQuery_IsMismatch()
{
Expand Down