public void EnsureInputObjectTypeKindIsCorret() { // arrange Schema schema = Create(); InputObjectType object1Type = schema.GetType <InputObjectType>("Object1"); // act TypeKind kind = object1Type.Kind; // assert Assert.Equal(TypeKind.InputObject, kind); }
public void Initialize_IgnoreProperty_PropertyIsNotInSchemaType() { // arrange // act var fooType = new InputObjectType <SimpleInput>( d => d.Field(f => f.Id).Ignore()); // assert fooType = CreateType(fooType); Assert.Collection(fooType.Fields, t => Assert.Equal("name", t.Name)); }
public void GenericInputObject_AddDirectives_NameArgs() { // arrange // act var fooType = new InputObjectType <SimpleInput>( d => d.Directive("foo").Field(f => f.Id).Directive("foo")); // assert fooType = CreateType(fooType, b => b.AddDirectiveType <FooDirectiveType>()); Assert.NotEmpty(fooType.Directives["foo"]); Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]); }
private ObjectValueNode FormatResultObject( object resultValue, InputObjectType type, Path path) { if (resultValue is IReadOnlyDictionary <string, object?> map) { var fields = new List <ObjectFieldNode>(); var processed = 0; foreach (var field in type.Fields) { if (map.TryGetValue(field.Name, out var fieldValue)) { IValueNode value = FormatResultInternal(fieldValue, field.Type, path); fields.Add(new ObjectFieldNode(field.Name, value)); processed++; } } if (processed < map.Count) { var invalidFieldNames = new List <string>(); foreach (KeyValuePair <string, object?> item in map) { if (!type.Fields.ContainsField(item.Key)) { invalidFieldNames.Add(item.Key); } } throw InvalidInputFieldNames(type, invalidFieldNames, path); } return(new ObjectValueNode(fields)); } if (resultValue is ObjectValueNode node) { return(node); } if (type.RuntimeType != typeof(object) && type.RuntimeType.IsInstanceOfType(resultValue)) { return(FormatValueObject(resultValue, type, path)); } throw FormatResultObject_InvalidObjectKind(type, resultValue.GetType(), path); }
public void ParseLiteral() { // arrange Schema schema = Create(); InputObjectType inputObjectType = schema.GetType <InputObjectType>("Object1"); ObjectValueNode literal = CreateObjectLiteral(); // act object obj = inputObjectType.ParseLiteral(literal); // assert Assert.IsType <SerializationInputObject1>(obj); obj.MatchSnapshot(); }
public void ParseLiteral() { // arrange Schema schema = Create(); InputObjectType object1Type = schema.GetType <InputObjectType>("Object1"); ObjectValueNode literal = CreateObjectLiteral(); // act object obj = object1Type.ParseLiteral(literal); // assert Assert.IsType <SerializationInputObject1>(obj); Assert.Equal(Snapshot.Current(), Snapshot.New(obj)); }
public void GenericInputObjectType_DynamicName_NonGeneric() { // act ISchema schema = SchemaBuilder.New() .AddInputObjectType <SimpleInput>(d => d .Name(dep => dep.Name + "Foo") .DependsOn(typeof(StringType))) .ModifyOptions(o => o.StrictValidation = false) .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("StringFoo"); Assert.NotNull(type); }
public void InputObjectTypeExtension_AddField() { // arrange // act ISchema schema = SchemaBuilder.New() .AddQueryType <QueryType>() .AddType <FooType>() .AddType <FooTypeExtension>() .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("FooInput"); Assert.True(type.Fields.ContainsField("test")); }
public static IValueNode ParseValue( InputObjectType inputObjectType, object obj) { if (inputObjectType == null) { throw new ArgumentNullException(nameof(inputObjectType)); } if (obj == null) { return(NullValueNode.Default); } return(ParseObject(new HashSet <object>(), inputObjectType, obj)); }
private static IValueNode ParseObjectList( HashSet <object> processed, InputObjectType elementType, IEnumerable enumerable) { List <IValueNode> list = new List <IValueNode>(); foreach (object element in enumerable) { if (!processed.Contains(element)) { list.Add(ParseObject(processed, elementType, element)); } } return(new ListValueNode(list)); }
public void InputObjectType_DynamicName() { // act ISchema schema = SchemaBuilder.New() .AddInputObjectType(d => d .Name(dep => dep.Name + "Foo") .DependsOn <StringType>() .Field("bar") .Type <StringType>()) .ModifyOptions(o => o.StrictValidation = false) .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("StringFoo"); Assert.NotNull(type); }
public void GenericInputObjectType_DynamicName_NonGeneric() { // act var schema = Schema.Create(c => { c.RegisterType(new InputObjectType <SimpleInput>(d => d .Name(dep => dep.Name + "Foo") .DependsOn(typeof(StringType)))); c.Options.StrictValidation = false; }); // assert InputObjectType type = schema.GetType <InputObjectType>("StringFoo"); Assert.NotNull(type); }
public void ParseValue() { // arrange Schema schema = Create(); InputObjectType object1Type = schema.GetType <InputObjectType>("Object1"); SerializationInputObject1 object1Instance = new SerializationInputObject1 { Foo = new SerializationInputObject2() }; // act IValueNode value = InputObjectDefaultSerializer.ParseValue(object1Type, object1Instance); // assert Assert.IsType <ObjectValueNode>(value); Assert.Equal(Snapshot.Current(), Snapshot.New(value)); }
public void InputObject_AddDirectives_DirectiveType() { // arrange // act var fooType = new InputObjectType( d => d.Directive <FooDirective>() .Field("id") .Type <StringType>() .Directive <FooDirective>()); // assert fooType = CreateType(fooType, b => b.AddDirectiveType <FooDirectiveType>()); Assert.NotEmpty(fooType.Directives["foo"]); Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]); }
public void InputObject_AddDirectives_NameArgs2() { // arrange // act var fooType = new InputObjectType <SimpleInput>(d => d .Name("Bar") .Directive(new NameString("foo")) .Field("id") .Type <StringType>() .Directive(new NameString("foo"))); // assert fooType = CreateType(fooType, b => b.AddDirectiveType <FooDirectiveType>()); Assert.NotEmpty(fooType.Directives["foo"]); Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]); }
public void InputObjectTypeExtension_SetTypeContextData() { // arrange // act ISchema schema = SchemaBuilder.New() .AddQueryType <QueryType>() .AddType <FooType>() .AddType(new InputObjectTypeExtension(d => d .Name("FooInput") .Extend() .OnBeforeCreate(c => c.ContextData["foo"] = "bar"))) .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("FooInput"); Assert.True(type.ContextData.ContainsKey("foo")); }
public void InputObjectTypeExtension_SetDirectiveOnType() { // arrange // act ISchema schema = SchemaBuilder.New() .AddQueryType <QueryType>() .AddType <FooType>() .AddType(new InputObjectTypeExtension(d => d .Name("FooInput") .Directive("dummy"))) .AddDirectiveType <DummyDirective>() .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("FooInput"); Assert.True(type.Directives.Contains("dummy")); }
private ObjectValueNode FormatValueObject( object runtimeValue, InputObjectType type, Path path) { var fields = new List <ObjectFieldNode>(); var fieldValues = new object?[type.Fields.Count]; type.GetFieldValues(runtimeValue, fieldValues); for (var i = 0; i < fieldValues.Length; i++) { InputField field = type.Fields[i]; var fieldValue = fieldValues[i]; Path fieldPath = path.Append(field.Name); if (field.IsOptional) { IOptional optional = (IOptional)fieldValue !; if (optional.HasValue) { AddField(optional.Value, field.Name, field.Type, fieldPath); } } else { AddField(fieldValue, field.Name, field.Type, fieldPath); } } return(new ObjectValueNode(fields)); void AddField( object?fieldValue, NameString fieldName, IInputType fieldType, Path fieldPath) { IValueNode value = FormatValueInternal(fieldValue, fieldType, fieldPath); fields.Add(new ObjectFieldNode(fieldName, value)); } }
public void InputObjectType_DynamicName() { // act var schema = Schema.Create(c => { c.RegisterType(new InputObjectType(d => d .Name(dep => dep.Name + "Foo") .DependsOn <StringType>() .Field("bar") .Type <StringType>())); c.Options.StrictValidation = false; }); // assert InputObjectType type = schema.GetType <InputObjectType>("StringFoo"); Assert.NotNull(type); }
private static bool TryCreateNativeReflectionDeserializer( InputObjectType inputObjectType, Type nativeType, out Func <ObjectValueNode, object> deserializer) { ConstructorInfo nativeTypeConstructor = nativeType.GetConstructors() .FirstOrDefault(t => t.GetParameters().Length == 0); if (nativeTypeConstructor != null) { deserializer = literal => InputObjectDefaultDeserializer .ParseLiteral(inputObjectType, literal); return(true); } deserializer = null; return(false); }
public static Func <ObjectValueNode, object> Create( Action <SchemaError> reportError, InputObjectType inputObjectType, Type nativeType) { Func <ObjectValueNode, object> _deserialize; if (!TryCreateNativeTypeParserDeserializer( reportError, inputObjectType, nativeType, out _deserialize) && !TryCreateNativeConstructorDeserializer( nativeType, out _deserialize) && !TryCreateNativeReflectionDeserializer( inputObjectType, nativeType, out _deserialize)) { reportError(new SchemaError( "Could not create a literal parser for input " + $"object type `{inputObjectType.Name}`", inputObjectType)); } return(null); }
public void InputObjectTypeExtension_RepeatableDirectiveOnType() { // arrange // act ISchema schema = SchemaBuilder.New() .AddQueryType <QueryType>() .AddType(new InputObjectType <Foo>(t => t .Directive("dummy_rep"))) .AddType(new InputObjectTypeExtension(d => d .Name("FooInput") .Directive("dummy_rep"))) .AddDirectiveType <RepeatableDummyDirective>() .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("FooInput"); int count = type.Directives["dummy_rep"].Count(); Assert.Equal(2, count); }
public static Func <ObjectValueNode, object> Create( ITypeInitializationContext context, InputObjectType inputObjectType, Type nativeType) { Func <ObjectValueNode, object> _deserialize; if (!TryCreateNativeTypeParserDeserializer( context, inputObjectType, nativeType, out _deserialize) && !TryCreateNativeConstructorDeserializer( nativeType, out _deserialize) && !TryCreateNativeReflectionDeserializer( inputObjectType, nativeType, out _deserialize)) { context.ReportError(new SchemaError( "Could not create a literal parser for input " + $"object type `{inputObjectType.Name}`", inputObjectType)); } return(_deserialize); }
public void Parse_InputObject_WithDefault_Values() { // arrange ISchema schema = SchemaBuilder.New() .AddInputObjectType <Test3Input>() .ModifyOptions(o => o.StrictValidation = false) .Create(); InputObjectType type = schema.GetType <InputObjectType>("Test3Input"); var fieldData = new ObjectValueNode( new ObjectFieldNode("field2", 123)); // act var parser = new InputParser(); var obj = parser.ParseLiteral(fieldData, type, Path.Root.Append("root")); // assert Assert.Equal("DefaultAbc", Assert.IsType <Test3Input>(obj).Field1); }
public void InputObjectTypeExtension_ReplaceDirectiveOnType() { // arrange // act ISchema schema = SchemaBuilder.New() .AddQueryType <QueryType>() .AddType(new InputObjectType <Foo>(t => t .Directive("dummy_arg", new ArgumentNode("a", "a")))) .AddType(new InputObjectTypeExtension(d => d .Name("FooInput") .Directive("dummy_arg", new ArgumentNode("a", "b")))) .AddDirectiveType <DummyWithArgDirective>() .Create(); // assert InputObjectType type = schema.GetType <InputObjectType>("FooInput"); string value = type.Directives["dummy_arg"] .First().GetArgument <string>("a"); Assert.Equal("b", value); }
public void Parse_InputObject_AllIsSet_MissingRequired() { // arrange ISchema schema = SchemaBuilder.New() .AddInputObjectType <Test2Input>() .ModifyOptions(o => o.StrictValidation = false) .Create(); InputObjectType type = schema.GetType <InputObjectType>("Test2Input"); var fieldData = new ObjectValueNode( new ObjectFieldNode("field2", 123)); // act var parser = new InputParser(new DefaultTypeConverter()); void Action() => parser.ParseLiteral(fieldData, type, Path.Root); // assert Assert.Throws <SerializationException>(Action).MatchSnapshot(); }
public void Parse_InputObject_AllIsSet_ConstructorInit() { // arrange ISchema schema = SchemaBuilder.New() .AddInputObjectType <Test2Input>() .ModifyOptions(o => o.StrictValidation = false) .Create(); InputObjectType type = schema.GetType <InputObjectType>("Test2Input"); var fieldData = new ObjectValueNode( new ObjectFieldNode("field1", "abc"), new ObjectFieldNode("field2", 123)); // act var parser = new InputParser(new DefaultTypeConverter()); var runtimeValue = parser.ParseLiteral(fieldData, type, Path.Root); // assert Assert.IsType <Test2Input>(runtimeValue).MatchSnapshot(); }
public void ParseLiteral_ValueIsStringValueNode_ArgumentException() { // arrange ISchema schema = SchemaBuilder.New() .AddQueryType(c => c .Name("Query") .Field("foo") .Type <StringType>() .Resolver("bar")) .AddType(new InputObjectType <SimpleInput>(d => d .Ignore(t => t.Id))) .Create(); InputObjectType type = schema.GetType <InputObjectType>("SimpleInput"); // act Action action = () => type.ParseLiteral(new StringValueNode("foo")); // assert Assert.Throws <SerializationException>(action); }
public void ParseLiteral_ValueIsNullValueNode() { // arrange ISchema schema = SchemaBuilder.New() .AddQueryType(c => c .Name("Query") .Field("foo") .Type <StringType>() .Resolver("bar")) .AddType(new InputObjectType <SimpleInput>(d => d .Ignore(t => t.Id))) .Create(); InputObjectType type = schema.GetType <InputObjectType>("SimpleInput"); // act object result = type.ParseLiteral(NullValueNode.Default); // assert Assert.Null(result); }
public void Deserialize_ValueIsNull() { // arrange ISchema schema = SchemaBuilder.New() .AddQueryType(c => c .Name("Query") .Field("foo") .Type <StringType>() .Resolver("bar")) .AddType(new InputObjectType <SimpleInput>(d => d .Ignore(t => t.Id))) .Create(); InputObjectType type = schema.GetType <InputObjectType>("SimpleInput"); // act bool result = type.TryDeserialize(null, out object value); // assert Assert.Null(value); }