Skip to content

Commit a2a572a

Browse files
committed
Add additional types to schema builder
1 parent 5c09136 commit a2a572a

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Reflection;
2+
using GraphQL.Types;
3+
4+
namespace Our.Umbraco.GraphQL.Builders
5+
{
6+
public interface ISchemaBuilder
7+
{
8+
ISchema Build<TSchema>(params TypeInfo[] additionalTypes);
9+
ISchema Build(TypeInfo schemaType, params TypeInfo[] additionalTypes);
10+
}
11+
}

src/Our.Umbraco.GraphQL/Builders/SchemaBuilder.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Our.Umbraco.GraphQL.Builders
88
{
9-
public class SchemaBuilder
9+
public class SchemaBuilder : ISchemaBuilder
1010
{
1111
private readonly IGraphTypeAdapter _graphTypeAdapter;
1212
private readonly GraphVisitor _visitor;
@@ -17,9 +17,9 @@ public SchemaBuilder(IGraphTypeAdapter graphTypeAdapter, GraphVisitor visitor)
1717
_visitor = visitor;
1818
}
1919

20-
public ISchema Build<TSchema>() => Build(typeof(TSchema).GetTypeInfo());
20+
public ISchema Build<TSchema>(params TypeInfo[] additionalTypes) => Build(typeof(TSchema).GetTypeInfo(), additionalTypes);
2121

22-
public ISchema Build(TypeInfo schemaType)
22+
public ISchema Build(TypeInfo schemaType, params TypeInfo[] additionalTypes)
2323
{
2424
if (schemaType == null) throw new ArgumentNullException(nameof(schemaType));
2525

@@ -33,6 +33,11 @@ public ISchema Build(TypeInfo schemaType)
3333
throw new ArgumentException("'Query' does not have a getter.", nameof(schemaType));
3434
schema.Query = (IObjectGraphType) _graphTypeAdapter.Adapt(queryPropertyInfo.GetMethod.ReturnType.GetTypeInfo());
3535

36+
foreach(var type in additionalTypes)
37+
{
38+
schema.RegisterType(_graphTypeAdapter.Adapt(type));
39+
}
40+
3641
_visitor?.Visit(schema);
3742

3843
return schema;

test/Our.Umbraco.GraphQL.Tests/Builders/SchemaBuilderTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Configuration;
23
using System.Reflection;
34
using FluentAssertions;
45
using GraphQL.Types;
@@ -79,6 +80,21 @@ public void Build_WithVisitor_CallsVisitWithSchema()
7980
visitor.Received(1).Visit(Arg.Is(schema));
8081
}
8182

83+
84+
[Fact]
85+
public void Build_WithAdditionalTypes_AddToAdditionalTypes()
86+
{
87+
var graphTypeAdapter = Substitute.For<IGraphTypeAdapter>();
88+
graphTypeAdapter.Adapt(Arg.Is(typeof(Query).GetTypeInfo())).Returns(new ObjectGraphType());
89+
graphTypeAdapter.Adapt(Arg.Is(typeof(MyType).GetTypeInfo()))
90+
.Returns(new ObjectGraphType {Name = "MyType"});
91+
var schemaBuilder = CreateSUT(graphTypeAdapter);
92+
93+
var schema = schemaBuilder.Build<SchemaWithQuery>(typeof(MyType).GetTypeInfo());
94+
95+
schema.FindType(nameof(MyType)).Should().NotBeNull();
96+
}
97+
8298
private class EmptySchema
8399
{
84100
}
@@ -100,5 +116,10 @@ private class SchemaWithQuery
100116
private class Query
101117
{
102118
}
119+
120+
private class MyType
121+
{
122+
public string Name { get; set; }
123+
}
103124
}
104125
}

0 commit comments

Comments
 (0)