Skip to content

Commit

Permalink
Dynamic schema creation example
Browse files Browse the repository at this point in the history
  • Loading branch information
tlil committed Jan 16, 2017
1 parent ff946f2 commit d27b4ab
Showing 1 changed file with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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}",
};
}
}
}

0 comments on commit d27b4ab

Please sign in to comment.