Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 183019b

Browse filesBrowse files
Unit Tests for Field Ordering (#146)
1 parent 3878da9 commit 183019b
Copy full SHA for 183019b

File tree

8 files changed

+305
-21
lines changed
Filter options

8 files changed

+305
-21
lines changed

‎src/graphql-aspnet/Configuration/GraphQLSchemaBuilderExtensions.cs

Copy file name to clipboardExpand all lines: src/graphql-aspnet/Configuration/GraphQLSchemaBuilderExtensions.cs
+28-20Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ static GraphQLSchemaBuilderExtensions()
4545
/// <param name="serviceCollection">The service collection to clear.</param>
4646
public static void Clear(IServiceCollection serviceCollection)
4747
{
48-
if (!SCHEMA_REGISTRATIONS.TryGetValue(serviceCollection, out var value))
48+
lock (SCHEMA_REGISTRATIONS)
4949
{
50-
return;
51-
}
50+
if (!SCHEMA_REGISTRATIONS.TryGetValue(serviceCollection, out var value))
51+
{
52+
return;
53+
}
5254

53-
value.Clear();
54-
SCHEMA_REGISTRATIONS.Remove(serviceCollection);
55+
value.Clear();
56+
SCHEMA_REGISTRATIONS.Remove(serviceCollection);
57+
}
5558
}
5659

5760
/// <summary>
@@ -60,12 +63,15 @@ public static void Clear(IServiceCollection serviceCollection)
6063
/// </summary>
6164
public static void Clear()
6265
{
63-
foreach (var collection in SCHEMA_REGISTRATIONS.Values)
66+
lock (SCHEMA_REGISTRATIONS)
6467
{
65-
collection.Clear();
66-
}
68+
foreach (var injectorCollection in SCHEMA_REGISTRATIONS.Values)
69+
{
70+
injectorCollection.Clear();
71+
}
6772

68-
SCHEMA_REGISTRATIONS.Clear();
73+
SCHEMA_REGISTRATIONS.Clear();
74+
}
6975
}
7076

7177
/// <summary>
@@ -96,7 +102,7 @@ public static ISchemaBuilder<TSchema> AddGraphQL<TSchema>(
96102
where TSchema : class, ISchema
97103
{
98104
Validation.ThrowIfNull(serviceCollection, nameof(serviceCollection));
99-
var injectorCollection = GetSchemaInjectorCollection(serviceCollection);
105+
var injectorCollection = GetOrAddSchemaInjectorCollection(serviceCollection);
100106
if (injectorCollection.ContainsKey(typeof(TSchema)))
101107
{
102108
throw new GraphTypeDeclarationException(
@@ -166,24 +172,26 @@ public static void UseGraphQL(this IServiceProvider serviceProvider)
166172
}
167173

168174
/// <summary>
169-
/// Get or create schema injector collection for the service collection.
175+
/// Get or create a schema injector collection for the service collection being harnessed.
170176
/// </summary>
171177
/// <param name="serviceCollection">The service collection to create schema injector collection for</param>
172178
/// <returns>Existing or new schema injector collection</returns>
173-
private static ISchemaInjectorCollection GetSchemaInjectorCollection(IServiceCollection serviceCollection)
179+
private static ISchemaInjectorCollection GetOrAddSchemaInjectorCollection(IServiceCollection serviceCollection)
174180
{
175181
if (SCHEMA_REGISTRATIONS.TryGetValue(serviceCollection, out var value))
176-
{
177182
return value;
178-
}
179183

180-
var injectorCollection = new SchemaInjectorCollection()
184+
lock (SCHEMA_REGISTRATIONS)
181185
{
182-
ServiceCollection = serviceCollection,
183-
};
184-
serviceCollection.AddSingleton<ISchemaInjectorCollection>(injectorCollection);
185-
value = injectorCollection;
186-
SCHEMA_REGISTRATIONS.Add(serviceCollection, value);
186+
if (SCHEMA_REGISTRATIONS.TryGetValue(serviceCollection, out value))
187+
return value;
188+
189+
var injectorCollection = new SchemaInjectorCollection(serviceCollection);
190+
191+
serviceCollection.AddSingleton<ISchemaInjectorCollection>(injectorCollection);
192+
value = injectorCollection;
193+
SCHEMA_REGISTRATIONS.Add(serviceCollection, value);
194+
}
187195

188196
return value;
189197
}

‎src/graphql-aspnet/Configuration/SchemaInjectorCollection.cs

Copy file name to clipboardExpand all lines: src/graphql-aspnet/Configuration/SchemaInjectorCollection.cs
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace GraphQL.AspNet.Configuration
1111
{
1212
using System;
1313
using System.Collections.Generic;
14+
using GraphQL.AspNet.Common;
1415
using GraphQL.AspNet.Interfaces.Configuration;
1516
using Microsoft.Extensions.DependencyInjection;
1617

@@ -19,7 +20,16 @@ namespace GraphQL.AspNet.Configuration
1920
/// </summary>
2021
public class SchemaInjectorCollection : Dictionary<Type, ISchemaInjector>, ISchemaInjectorCollection
2122
{
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="SchemaInjectorCollection"/> class.
25+
/// </summary>
26+
/// <param name="serviceCollection">The service collection this set is tracking against.</param>
27+
public SchemaInjectorCollection(IServiceCollection serviceCollection)
28+
{
29+
this.ServiceCollection = Validation.ThrowIfNullOrReturn(serviceCollection, nameof(serviceCollection));
30+
}
31+
2232
/// <inheritdoc />
23-
public IServiceCollection ServiceCollection { get; set; }
33+
public IServiceCollection ServiceCollection { get; }
2434
}
2535
}

‎src/unit-tests/graphql-aspnet-tests/Configuration/ConfigurationSetupTests.cs

Copy file name to clipboardExpand all lines: src/unit-tests/graphql-aspnet-tests/Configuration/ConfigurationSetupTests.cs
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,25 @@ public void AddGraphQL_UseGraphQL_SameSchema()
353353
Assert.IsTrue(schema1.KnownTypes.Contains(typeof(Candle)));
354354
Assert.IsTrue(schema2.KnownTypes.Contains(typeof(Candle)));
355355
}
356+
357+
[Test]
358+
public void AddGraphQL_AttemptingToInitializeSchemaASecondTime_ThrowsException()
359+
{
360+
var service1Collection = new ServiceCollection();
361+
var service2Collection = new ServiceCollection();
362+
363+
service1Collection.AddGraphQL<CandleSchema>(options =>
364+
{
365+
options.AddGraphType<Candle>();
366+
});
367+
368+
Assert.Throws<GraphTypeDeclarationException>(() =>
369+
{
370+
service1Collection.AddGraphQL<CandleSchema>(options =>
371+
{
372+
options.AddGraphType<Candle>();
373+
});
374+
});
375+
}
356376
}
357377
}
+107Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// *************************************************************
2+
// project: graphql-aspnet
3+
// --
4+
// repo: https://github.com/graphql-aspnet
5+
// docs: https://graphql-aspnet.github.io
6+
// --
7+
// License: MIT
8+
// *************************************************************
9+
10+
namespace GraphQL.AspNet.Tests.Execution
11+
{
12+
using System.Text.RegularExpressions;
13+
using System.Threading.Tasks;
14+
using GraphQL.AspNet.Tests.Execution.FieldExecutionorderTestData;
15+
using GraphQL.AspNet.Tests.Framework;
16+
using NUnit.Framework;
17+
18+
[TestFixture]
19+
public class FieldExecutionOrderTests
20+
{
21+
private static readonly Regex _whitespace = new Regex(@"\s+");
22+
23+
[Test]
24+
public async Task OrderTest()
25+
{
26+
var server = new TestServerBuilder()
27+
.AddGraphQL(o =>
28+
{
29+
o.AddType<HamburgerController>();
30+
o.AddType<ChickenController>();
31+
o.ResponseOptions.ExposeExceptions = true;
32+
})
33+
.Build();
34+
35+
var builder = server.CreateQueryContextBuilder()
36+
.AddQueryText("query { " +
37+
" retrieveDefaultChickenMeal {" +
38+
" description " +
39+
" name " +
40+
" id" +
41+
" }" +
42+
" hamburger { " +
43+
" retrieveOtherHamburgerMeal {" +
44+
" weight " +
45+
" name " +
46+
" id" +
47+
" description " +
48+
" }" +
49+
" }" +
50+
" chicken { " +
51+
" retrieveOtherChickenMeal {" +
52+
" id" +
53+
" description " +
54+
" name " +
55+
" }" +
56+
" }" +
57+
" retrieveDefaultHamburgerMeal {" +
58+
" id" +
59+
" description " +
60+
" name " +
61+
" weight " +
62+
" }" +
63+
"}");
64+
65+
var expectedOutput =
66+
@"{
67+
""data"": {
68+
""retrieveDefaultChickenMeal"": {
69+
""description"": ""a top level chicken sandwich"",
70+
""name"": ""Chicken Bacon Ranch"",
71+
""id"": 5
72+
},
73+
""hamburger"": {
74+
""retrieveOtherHamburgerMeal"": {
75+
""weight"": 1.3,
76+
""name"": ""Tiny Hamburger"",
77+
""id"": 5,
78+
""description"": ""a nested Hamburger""
79+
}
80+
},
81+
""chicken"": {
82+
""retrieveOtherChickenMeal"": {
83+
""id"": 5,
84+
""description"": ""a nested chicken sandwich"",
85+
""name"": ""Chicken Lettuce Tomato""
86+
}
87+
},
88+
""retrieveDefaultHamburgerMeal"": {
89+
""id"": 5,
90+
""description"": ""a top level hamburger"",
91+
""name"": ""Hamburger Supreme"",
92+
""weight"": 2.5
93+
}
94+
}
95+
}";
96+
97+
var result = await server.RenderResult(builder);
98+
99+
// must be an exact string with the exact character order for the data
100+
// dont use json matching just to be sure
101+
result = _whitespace.Replace(result, string.Empty);
102+
expectedOutput = _whitespace.Replace(expectedOutput, string.Empty);
103+
104+
Assert.AreEqual(expectedOutput, result);
105+
}
106+
}
107+
}
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// *************************************************************
2+
// project: graphql-aspnet
3+
// --
4+
// repo: https://github.com/graphql-aspnet
5+
// docs: https://graphql-aspnet.github.io
6+
// --
7+
// License: MIT
8+
// *************************************************************
9+
10+
namespace GraphQL.AspNet.Tests.Execution.FieldExecutionorderTestData
11+
{
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Text;
16+
using System.Threading.Tasks;
17+
using GraphQL.AspNet.Attributes;
18+
using GraphQL.AspNet.Controllers;
19+
20+
public class ChickenController : GraphController
21+
{
22+
[QueryRoot]
23+
public ChickenMeal RetrieveDefaultChickenMeal()
24+
{
25+
return new ChickenMeal()
26+
{
27+
Id = 5,
28+
Name = "Chicken Bacon Ranch",
29+
Description = "a top level chicken sandwich",
30+
};
31+
}
32+
33+
[Query]
34+
public ChickenMeal RetrieveOtherChickenMeal()
35+
{
36+
return new ChickenMeal()
37+
{
38+
Id = 5,
39+
Name = "Chicken Lettuce Tomato",
40+
Description = "a nested chicken sandwich",
41+
};
42+
}
43+
}
44+
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// *************************************************************
2+
// project: graphql-aspnet
3+
// --
4+
// repo: https://github.com/graphql-aspnet
5+
// docs: https://graphql-aspnet.github.io
6+
// --
7+
// License: MIT
8+
// *************************************************************
9+
10+
namespace GraphQL.AspNet.Tests.Execution.FieldExecutionorderTestData
11+
{
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Text;
16+
using System.Threading.Tasks;
17+
18+
public class ChickenMeal
19+
{
20+
public int Id { get; set; }
21+
22+
public string Name { get; set; }
23+
24+
public string Description { get; set; }
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// *************************************************************
2+
// project: graphql-aspnet
3+
// --
4+
// repo: https://github.com/graphql-aspnet
5+
// docs: https://graphql-aspnet.github.io
6+
// --
7+
// License: MIT
8+
// *************************************************************
9+
10+
namespace GraphQL.AspNet.Tests.Execution.FieldExecutionorderTestData
11+
{
12+
using GraphQL.AspNet.Attributes;
13+
using GraphQL.AspNet.Controllers;
14+
15+
public class HamburgerController : GraphController
16+
{
17+
[QueryRoot]
18+
public HamburgerMeal RetrieveDefaultHamburgerMeal()
19+
{
20+
return new HamburgerMeal()
21+
{
22+
Id = 5,
23+
Name = "Hamburger Supreme",
24+
Weight = 2.5f,
25+
Description = "a top level hamburger",
26+
};
27+
}
28+
29+
[Query]
30+
public HamburgerMeal RetrieveOtherHamburgerMeal()
31+
{
32+
return new HamburgerMeal()
33+
{
34+
Id = 5,
35+
Name = "Tiny Hamburger",
36+
Weight = 1.3f,
37+
Description = "a nested Hamburger",
38+
};
39+
}
40+
}
41+
}
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// *************************************************************
2+
// project: graphql-aspnet
3+
// --
4+
// repo: https://github.com/graphql-aspnet
5+
// docs: https://graphql-aspnet.github.io
6+
// --
7+
// License: MIT
8+
// *************************************************************
9+
10+
namespace GraphQL.AspNet.Tests.Execution.FieldExecutionorderTestData
11+
{
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Text;
16+
using System.Threading.Tasks;
17+
18+
public class HamburgerMeal
19+
{
20+
public int Id { get; set; }
21+
22+
public string Name { get; set; }
23+
24+
public float Weight { get; set; }
25+
26+
public string Description { get; set; }
27+
}
28+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.