Skip to content

Commit 0597a73

Browse files
StefHCopilot
andauthored
Create GraphQL project (#1334)
* Create new project for GraphQL * ... * . * ok? * Update src/WireMock.Net.Shared/Extensions/AnyOfExtensions.cs Co-authored-by: Copilot <[email protected]> * -- * ... --------- Co-authored-by: Copilot <[email protected]>
1 parent 0d510cd commit 0597a73

38 files changed

+266
-230
lines changed

WireMock.Net Solution.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.ConsoleApp.Usi
136136
EndProject
137137
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Tests.UsingNuGet", "test\WireMock.Net.Tests.UsingNuGet\WireMock.Net.Tests.UsingNuGet.csproj", "{BBA332C6-28A9-42E7-9C4D-A0816E52A198}"
138138
EndProject
139+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.GraphQL", "src\WireMock.Net.GraphQL\WireMock.Net.GraphQL.csproj", "{B6269AAC-170A-4346-8B9A-444DED3D9A45}"
140+
EndProject
139141
Global
140142
GlobalSection(SolutionConfigurationPlatforms) = preSolution
141143
Debug|Any CPU = Debug|Any CPU
@@ -326,6 +328,10 @@ Global
326328
{BBA332C6-28A9-42E7-9C4D-A0816E52A198}.Debug|Any CPU.Build.0 = Debug|Any CPU
327329
{BBA332C6-28A9-42E7-9C4D-A0816E52A198}.Release|Any CPU.ActiveCfg = Release|Any CPU
328330
{BBA332C6-28A9-42E7-9C4D-A0816E52A198}.Release|Any CPU.Build.0 = Release|Any CPU
331+
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
332+
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Debug|Any CPU.Build.0 = Debug|Any CPU
333+
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Release|Any CPU.ActiveCfg = Release|Any CPU
334+
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Release|Any CPU.Build.0 = Release|Any CPU
329335
EndGlobalSection
330336
GlobalSection(SolutionProperties) = preSolution
331337
HideSolutionNode = FALSE
@@ -379,6 +385,7 @@ Global
379385
{BFEF8990-65B3-4274-310F-7355F0B84035} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
380386
{1F80A6E6-D146-4E40-9EA8-49DB8494239F} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
381387
{BBA332C6-28A9-42E7-9C4D-A0816E52A198} = {0BB8B634-407A-4610-A91F-11586990767A}
388+
{B6269AAC-170A-4346-8B9A-444DED3D9A45} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
382389
EndGlobalSection
383390
GlobalSection(ExtensibilityGlobals) = postSolution
384391
SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright © WireMock.Net
2+
3+
namespace WireMock.Models.GraphQL;
4+
5+
public interface ISchemaData;

src/WireMock.Net.Minimal/Matchers/GraphQLMatcher.cs renamed to src/WireMock.Net.GraphQL/Matchers/GraphQLMatcher.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright © WireMock.Net
22

3-
#if GRAPHQL
43
using System;
54
using System.Collections.Generic;
65
using System.Diagnostics.CodeAnalysis;
@@ -14,17 +13,17 @@
1413
using Stef.Validation;
1514
using WireMock.Exceptions;
1615
using WireMock.Extensions;
17-
using WireMock.Matchers.Models;
16+
using WireMock.GraphQL.Models;
1817
using WireMock.Models;
19-
using WireMock.Util;
18+
using WireMock.Models.GraphQL;
19+
using WireMock.Utils;
2020

2121
namespace WireMock.Matchers;
2222

2323
/// <summary>
2424
/// GrapQLMatcher Schema Matcher
2525
/// </summary>
26-
/// <inheritdoc cref="IStringMatcher"/>
27-
public class GraphQLMatcher : IStringMatcher
26+
public class GraphQLMatcher : IGraphQLMatcher
2827
{
2928
private sealed class GraphQLRequest
3029
{
@@ -54,7 +53,7 @@ private sealed class GraphQLRequest
5453
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
5554
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
5655
public GraphQLMatcher(
57-
AnyOf<string, StringPattern, ISchema> schema,
56+
AnyOf<string, StringPattern, ISchemaData> schema,
5857
MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch,
5958
MatchOperator matchOperator = MatchOperator.Or
6059
) : this(schema, null, matchBehaviour, matchOperator)
@@ -69,7 +68,7 @@ public GraphQLMatcher(
6968
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
7069
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
7170
public GraphQLMatcher(
72-
AnyOf<string, StringPattern, ISchema> schema,
71+
AnyOf<string, StringPattern, ISchemaData> schema,
7372
IDictionary<string, Type>? customScalars,
7473
MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch,
7574
MatchOperator matchOperator = MatchOperator.Or
@@ -94,7 +93,7 @@ public GraphQLMatcher(
9493
break;
9594

9695
case AnyOfType.Third:
97-
_schema = schema.Third;
96+
_schema = ((SchemaDataWrapper)schema.Third).Schema;
9897
break;
9998

10099
default:
@@ -201,13 +200,12 @@ private ISchema BuildSchema(string typeDefinitions)
201200
throw new WireMockException($"The GraphQL Scalar type '{scalarTypeDefinitionName}' is not defined in the CustomScalars dictionary.");
202201
}
203202

204-
// Create a this custom Scalar GraphType (extending the WireMockCustomScalarGraphType<{clrType}> class)
203+
// Create a custom Scalar GraphType (extending the WireMockCustomScalarGraphType<{clrType}> class)
205204
var customScalarGraphType = ReflectionUtils.CreateGenericType(customScalarGraphTypeName, typeof(WireMockCustomScalarGraphType<>), clrType);
206205
schema.RegisterType(customScalarGraphType);
207206
}
208207
}
209208

210209
return schema;
211210
}
212-
}
213-
#endif
211+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright © WireMock.Net
2+
3+
using GraphQL.Types;
4+
using WireMock.Models.GraphQL;
5+
6+
namespace WireMock.Models;
7+
8+
/// <summary>
9+
/// Represents a wrapper for schema data, providing access to the associated schema.
10+
/// </summary>
11+
/// <param name="schema"></param>
12+
public class SchemaDataWrapper(ISchema schema) : ISchemaData
13+
{
14+
/// <summary>
15+
/// Gets the schema associated with the current instance.
16+
/// </summary>
17+
public ISchema Schema { get; } = schema;
18+
}

src/WireMock.Net.Minimal/Matchers/Models/WireMockCustomScalarGraphType.cs renamed to src/WireMock.Net.GraphQL/Models/WireMockCustomScalarGraphType.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright © WireMock.Net
22

3-
#if GRAPHQL
43
using System;
54
using GraphQL.Types;
65

7-
namespace WireMock.Matchers.Models;
6+
// ReSharper disable once CheckNamespace
7+
namespace WireMock.GraphQL.Models;
88

99
/// <inheritdoc />
1010
public abstract class WireMockCustomScalarGraphType<T> : ScalarGraphType
@@ -28,5 +28,4 @@ public abstract class WireMockCustomScalarGraphType<T> : ScalarGraphType
2828

2929
return (T)Convert.ChangeType(value, typeof(T));
3030
}
31-
}
32-
#endif
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright © WireMock.Net
2+
3+
using System.Runtime.CompilerServices;
4+
5+
// [assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
6+
7+
// Needed for Moq in the UnitTest project
8+
// [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright © WireMock.Net
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using GraphQL.Types;
6+
using Stef.Validation;
7+
using WireMock.Matchers;
8+
using WireMock.Matchers.Request;
9+
using WireMock.Models;
10+
using WireMock.Models.GraphQL;
11+
12+
namespace WireMock.RequestBuilders;
13+
14+
/// <summary>
15+
/// IRequestBuilderExtensions extensions for GraphQL.
16+
/// </summary>
17+
// ReSharper disable once InconsistentNaming
18+
public static class IRequestBuilderExtensions
19+
{
20+
/// <summary>
21+
/// WithBodyAsGraphQL: The GraphQL body as a string.
22+
/// </summary>
23+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
24+
/// <param name="schema">The GraphQL schema.</param>
25+
/// <param name="matchBehaviour">The match behaviour. (Default is <c>MatchBehaviour.AcceptOnMatch</c>).</param>
26+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
27+
public static IRequestBuilder WithGraphQLSchema(this IRequestBuilder requestBuilder, string schema, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
28+
{
29+
return Guard.NotNull(requestBuilder).Add(new RequestMessageGraphQLMatcher(matchBehaviour, schema));
30+
}
31+
32+
/// <summary>
33+
/// WithBodyAsGraphQL: The GraphQL schema as a string.
34+
/// </summary>
35+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
36+
/// <param name="schema">The GraphQL schema.</param>
37+
/// <param name="customScalars">A dictionary defining the custom scalars used in this schema. (optional)</param>
38+
/// <param name="matchBehaviour">The match behaviour. (Default is <c>MatchBehaviour.AcceptOnMatch</c>).</param>
39+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
40+
public static IRequestBuilder WithGraphQLSchema(this IRequestBuilder requestBuilder, string schema, IDictionary<string, Type>? customScalars, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
41+
{
42+
return Guard.NotNull(requestBuilder).Add(new RequestMessageGraphQLMatcher(matchBehaviour, schema, customScalars));
43+
}
44+
45+
/// <summary>
46+
/// WithBodyAsGraphQL: The GraphQL schema as a <see cref="ISchema"/>.
47+
/// </summary>
48+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
49+
/// <param name="schema">The GraphQL schema.</param>
50+
/// <param name="matchBehaviour">The match behaviour. (Default is <c>MatchBehaviour.AcceptOnMatch</c>).</param>
51+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
52+
public static IRequestBuilder WithGraphQLSchema(this IRequestBuilder requestBuilder, ISchema schema, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
53+
{
54+
return Guard.NotNull(requestBuilder).Add(new RequestMessageGraphQLMatcher(matchBehaviour, new SchemaDataWrapper(schema)));
55+
}
56+
57+
/// <summary>
58+
/// WithBodyAsGraphQL: The GraphQL schema as a <see cref="ISchema"/>.
59+
/// </summary>
60+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
61+
/// <param name="schema">The GraphQL schema.</param>
62+
/// <param name="customScalars">A dictionary defining the custom scalars used in this schema. (optional)</param>
63+
/// <param name="matchBehaviour">The match behaviour. (Default is <c>MatchBehaviour.AcceptOnMatch</c>).</param>
64+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
65+
public static IRequestBuilder WithGraphQLSchema(this IRequestBuilder requestBuilder, ISchema schema, IDictionary<string, Type>? customScalars, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
66+
{
67+
return Guard.NotNull(requestBuilder).Add(new RequestMessageGraphQLMatcher(matchBehaviour, new SchemaDataWrapper(schema), customScalars));
68+
}
69+
70+
/// <summary>
71+
/// WithBodyAsGraphQL: The GraphQL schema as a <see cref="ISchemaData"/>.
72+
/// </summary>
73+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
74+
/// <param name="schema">The GraphQL schema.</param>
75+
/// <param name="matchBehaviour">The match behaviour. (Default is <c>MatchBehaviour.AcceptOnMatch</c>).</param>
76+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
77+
public static IRequestBuilder WithGraphQLSchema(this IRequestBuilder requestBuilder, ISchemaData schema, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
78+
{
79+
return Guard.NotNull(requestBuilder).Add(new RequestMessageGraphQLMatcher(matchBehaviour, schema));
80+
}
81+
82+
/// <summary>
83+
/// WithBodyAsGraphQL: The GraphQL schema as a <see cref="ISchemaData"/>.
84+
/// </summary>
85+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
86+
/// <param name="schema">The GraphQL schema.</param>
87+
/// <param name="customScalars">A dictionary defining the custom scalars used in this schema. (optional)</param>
88+
/// <param name="matchBehaviour">The match behaviour. (Default is <c>MatchBehaviour.AcceptOnMatch</c>).</param>
89+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
90+
public static IRequestBuilder WithGraphQLSchema(this IRequestBuilder requestBuilder, ISchemaData schema, IDictionary<string, Type>? customScalars, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
91+
{
92+
return Guard.NotNull(requestBuilder).Add(new RequestMessageGraphQLMatcher(matchBehaviour, schema, customScalars));
93+
}
94+
}

src/WireMock.Net.Minimal/Util/ReflectionUtils.cs renamed to src/WireMock.Net.GraphQL/Utils/ReflectionUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Reflection;
77
using System.Reflection.Emit;
88

9-
namespace WireMock.Util;
9+
namespace WireMock.Utils;
1010

1111
internal static class ReflectionUtils
1212
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>GraphQL support for WireMock.Net</Description>
5+
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
6+
<Authors>Stef Heyenrath</Authors>
7+
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net8.0</TargetFrameworks>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
9+
<PackageTags>wiremock;matchers;matcher;graphql</PackageTags>
10+
<RootNamespace>WireMock</RootNamespace>
11+
<ProjectGuid>{B6269AAC-170A-4346-8B9A-444DED3D9A45}</ProjectGuid>
12+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
13+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
16+
<CodeAnalysisRuleSet>../WireMock.Net/WireMock.Net.ruleset</CodeAnalysisRuleSet>
17+
<SignAssembly>true</SignAssembly>
18+
<AssemblyOriginatorKeyFile>../WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
19+
<!--<DelaySign>true</DelaySign>-->
20+
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
21+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
22+
</PropertyGroup>
23+
24+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
25+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
26+
</PropertyGroup>
27+
28+
<ItemGroup>
29+
<PackageReference Include="GraphQL.NewtonsoftJson" Version="8.2.1" />
30+
</ItemGroup>
31+
32+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
33+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<ProjectReference Include="..\WireMock.Net.Shared\WireMock.Net.Shared.csproj" />
38+
</ItemGroup>
39+
40+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
41+
<PackageReference Include="Nullable" Version="1.3.1">
42+
<PrivateAssets>all</PrivateAssets>
43+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
44+
</PackageReference>
45+
</ItemGroup>
46+
47+
</Project>

src/WireMock.Net.Minimal/Models/GraphQLSchemaDetails.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using AnyOfTypes;
66
using Newtonsoft.Json;
7+
using WireMock.Models.GraphQL;
78

89
namespace WireMock.Models;
910

@@ -22,17 +23,16 @@ public class GraphQLSchemaDetails
2223
/// </summary>
2324
public StringPattern? SchemaAsStringPattern { get; set; }
2425

25-
#if GRAPHQL
2626
/// <summary>
27-
/// The GraphQL schema as a <seealso cref="GraphQL.Types.ISchema"/>.
27+
/// The GraphQL schema as a <seealso cref="ISchemaData"/>.
2828
/// </summary>
29-
public GraphQL.Types.ISchema? SchemaAsISchema { get; set; }
29+
public ISchemaData? SchemaAsISchemaData { get; set; }
3030

3131
/// <summary>
3232
/// The GraphQL Schema.
3333
/// </summary>
3434
[JsonIgnore]
35-
public AnyOf<string, StringPattern, GraphQL.Types.ISchema>? Schema
35+
public AnyOf<string, StringPattern, ISchemaData>? Schema
3636
{
3737
get
3838
{
@@ -46,15 +46,14 @@ public class GraphQLSchemaDetails
4646
return SchemaAsStringPattern;
4747
}
4848

49-
if (SchemaAsISchema != null)
49+
if (SchemaAsISchemaData != null)
5050
{
51-
return new AnyOf<string, StringPattern, GraphQL.Types.ISchema>(SchemaAsISchema);
51+
return new AnyOf<string, StringPattern, ISchemaData>(SchemaAsISchemaData);
5252
}
5353

5454
return null;
5555
}
5656
}
57-
#endif
5857

5958
/// <summary>
6059
/// The custom Scalars to define for this schema.

0 commit comments

Comments
 (0)