Skip to content

Commit

Permalink
Added support for directives on input object types. (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jan 28, 2019
1 parent d52a3d6 commit 1a143c4
Show file tree
Hide file tree
Showing 24 changed files with 658 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HotChocolate.Types
{
public class InputObjectTypeDescriptorDescriptors
public class InputObjectTypeDescriptorTests
{
[Fact]
public void Field_Ignore_PropertyIsExcluded()
Expand Down
288 changes: 288 additions & 0 deletions src/Core/Types.Tests/Types/InputObjectTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using ChilliCream.Testing;
using HotChocolate.Configuration;
using HotChocolate.Language;
Expand Down Expand Up @@ -63,6 +64,279 @@ public void EnsureInputObjectTypeKindIsCorret()
Assert.Equal(TypeKind.InputObject, kind);
}

[Fact]
public void GenericInputObject_AddDirectives_NameArgs()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Directive("foo").Field(f => f.Id).Directive("foo"));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void GenericInputObject_AddDirectives_NameArgs2()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Directive(new NameString("foo"))
.Field(f => f.Id)
.Directive(new NameString("foo")));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void GenericInputObject_AddDirectives_DirectiveNode()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Directive(new DirectiveNode("foo"))
.Field(f => f.Id)
.Directive(new DirectiveNode("foo")));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void GenericInputObject_AddDirectives_DirectiveClassInstance()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Directive(new FooDirective())
.Field(f => f.Id)
.Directive(new FooDirective()));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void GenericInputObject_AddDirectives_DirectiveType()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Directive<FooDirective>()
.Field(f => f.Id)
.Directive<FooDirective>());

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void InputObject_AddDirectives_NameArgs()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType(
d => d.Directive("foo")
.Field("id")
.Type<StringType>()
.Directive("foo"));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void InputObject_AddDirectives_NameArgs2()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Directive(new NameString("foo"))
.Field("id")
.Type<StringType>()
.Directive(new NameString("foo")));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void InputObject_AddDirectives_DirectiveNode()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType(
d => d.Directive(new DirectiveNode("foo"))
.Field("id")
.Type<StringType>()
.Directive(new DirectiveNode("foo")));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void InputObject_AddDirectives_DirectiveClassInstance()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType(
d => d.Directive(new FooDirective())
.Field("id")
.Type<StringType>()
.Directive(new FooDirective()));

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

[Fact]
public void InputObject_AddDirectives_DirectiveType()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();
schemaContext.Directives.RegisterDirectiveType<FooDirectiveType>();

// act
var fooType = new InputObjectType(
d => d.Directive<FooDirective>()
.Field("id")
.Type<StringType>()
.Directive<FooDirective>());

// assert
schemaContext.Types.RegisterType(fooType);
INeedsInitialization init = fooType;
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.NotEmpty(fooType.Directives["foo"]);
Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
}

private static ObjectValueNode CreateObjectLiteral()
{
return new ObjectValueNode(new List<ObjectFieldNode>
Expand Down Expand Up @@ -121,4 +395,18 @@ public class SerializationInputObject2
new SerializationInputObject1()
};
}

public class FooDirectiveType
: DirectiveType<FooDirective>
{
protected override void Configure(
IDirectiveTypeDescriptor<FooDirective> descriptor)
{
descriptor.Name("foo");
descriptor.Location(DirectiveLocation.InputObject)
.Location(DirectiveLocation.InputFieldDefinition);
}
}

public class FooDirective { }
}
Loading

0 comments on commit 1a143c4

Please sign in to comment.