From d27b4abcdb91d14717af64228f4d61e96325c476 Mon Sep 17 00:00:00 2001 From: Tommy Lillehagen Date: Mon, 16 Jan 2017 19:25:54 +0000 Subject: [PATCH] Dynamic schema creation example --- .../Engine/DynamicConstructionTests.cs | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 test/GraphQL.Conventions.Tests/Adapters/Engine/DynamicConstructionTests.cs diff --git a/test/GraphQL.Conventions.Tests/Adapters/Engine/DynamicConstructionTests.cs b/test/GraphQL.Conventions.Tests/Adapters/Engine/DynamicConstructionTests.cs new file mode 100644 index 00000000..5a8e94bf --- /dev/null +++ b/test/GraphQL.Conventions.Tests/Adapters/Engine/DynamicConstructionTests.cs @@ -0,0 +1,123 @@ +using System.Reflection; +using System.Threading.Tasks; +using GraphQL; +using GraphQL.Conventions.Adapters; +using GraphQL.Conventions.Tests.Templates; +using GraphQL.Conventions.Tests.Templates.Extensions; +using GraphQL.Conventions.Types.Resolution; +using GraphQL.Resolvers; +using GraphQL.Types; +using GraphQL.Utilities; +using Newtonsoft.Json; +using Xunit; + +namespace GraphQL.Conventions.Tests.Adapters.Engine +{ + public class DynamicConstructionTests : TestBase + { + [Fact] + public async Task Can_Construct_And_Describe_Schema_With_Dynamic_Queries() + { + var typeAdapter = new GraphTypeAdapter(); + var typeResolver = new TypeResolver(); + + var userRepositoryInterface = GetGraphType(typeAdapter, typeResolver, typeof(IUserRepository).GetTypeInfo()); + var userRepositoryType = GetGraphType(typeAdapter, typeResolver, typeof(UserRepository).GetTypeInfo()); + var userType = GetGraphType(typeAdapter, typeResolver, typeof(User).GetTypeInfo()); + + var schema = new Schema + { + Query = new ObjectGraphType { Name = "Query" } + }; + + schema.RegisterTypes(userType, userRepositoryInterface, userRepositoryType); + + schema.Query.AddField(new FieldType + { + Name = "users", + ResolvedType = userRepositoryType, + Resolver = new CustomResolver(new UserRepository()), + }); + + schema.ResolveType = (t) => + { + if (t == typeof(IUserRepository)) + { + return userRepositoryInterface; + } + if (t == typeof(UserRepository)) + { + return userRepositoryType; + } + if (t == typeof(User)) + { + return userType; + } + return null; + }; + + var schemaDescription = new SchemaPrinter(schema).Print(); + schemaDescription.ShouldEqualWhenReformatted(@" + interface IUserRepository { + getUserById(id: String): User + } + type Query { + users: UserRepository + } + type User { + id: String + name: String + } + type UserRepository implements IUserRepository { + getUserById(id: String): User + } + "); + + var executer = new DocumentExecuter(); + var result = await executer.ExecuteAsync(schema, null, "{ users { getUserById(id: \"1\") { id name } } }", null); + result.Errors.ShouldEqual(null); + JsonConvert.SerializeObject(result.Data).ShouldEqual("{\"users\":{\"getUserById\":{\"id\":\"1\",\"name\":\"User #1\"}}}"); + } + + private IGraphType GetGraphType(GraphTypeAdapter typeAdapter, TypeResolver typeResolver, TypeInfo typeInfo) + { + var graphTypeInfo = typeResolver.DeriveType(typeInfo); + return typeAdapter.DeriveType(graphTypeInfo); + } + + class CustomResolver : IFieldResolver + { + private readonly UserRepository _userRepository; + + public CustomResolver(UserRepository userRepository) + { + _userRepository = userRepository; + } + + public object Resolve(ResolveFieldContext context) => + _userRepository; + } + + class User + { + public string Id { get; set; } + + public string Name { get; set; } + } + + interface IUserRepository + { + User GetUserById(string id); + } + + class UserRepository : IUserRepository + { + public User GetUserById(string id) => + new User + { + Id = id, + Name = $"User #{id}", + }; + } + } +} \ No newline at end of file