-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
test/GraphQL.Conventions.Tests/Adapters/Engine/DynamicConstructionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}", | ||
}; | ||
} | ||
} | ||
} |