Esempio n. 1
0
        private GraphQLBaseType GetSchemaTypeForWithNoError(Type type)
        {
            if (ReflectionUtilities.IsCollection(type))
            {
                return(this.CreateList(type));
            }

            var underlyingNonNullableType = NonNullable.GetUnderlyingType(type);

            if (underlyingNonNullableType != null)
            {
                return(new GraphQLNonNull(
                           this.GetSchemaTypeForWithNoError(underlyingNonNullableType)));
            }

            var underlyingNullableType = Nullable.GetUnderlyingType(type);

            if (underlyingNullableType != null)
            {
                return(this.GetSchemaTypeFor(underlyingNullableType, underlyingNullableType));
            }

            if (ReflectionUtilities.IsValueType(type))
            {
                return(new GraphQLNonNull(this.GetSchemaTypeFor(type, type)));
            }

            return(this.GetSchemaTypeFor(type, type));
        }
Esempio n. 2
0
        private GraphQLInputType GetSchemaInputTypeForType(Type type)
        {
            var underlyingNonNullableType = NonNullable.GetUnderlyingType(type);

            if (underlyingNonNullableType != null)
            {
                return(new GraphQLNonNull(
                           this.GetSchemaInputTypeFor(underlyingNonNullableType)));
            }

            var underlyingNullableType = Nullable.GetUnderlyingType(type);

            if (underlyingNullableType != null)
            {
                return(this.GetSchemaInputType(underlyingNullableType));
            }

            var inputType = this.GetSchemaInputType(type);

            if (inputType == null)
            {
                throw new GraphQLException($"Unknown input type {type} have you added it to known types?");
            }

            if (ReflectionUtilities.IsValueType(type))
            {
                return(new GraphQLNonNull(inputType));
            }

            return(inputType);
        }
        public void NonNullable_GetUnderlyingType_ReturnsCorrectUnderlyingType()
        {
            var type      = typeof(NonNullable <Test>);
            var wrongType = typeof(string[]);

            var result  = NonNullable.GetUnderlyingType(type);
            var result2 = NonNullable.GetUnderlyingType(wrongType);

            Assert.AreEqual(typeof(Test), result);
            Assert.AreEqual(null, result2);
        }
Esempio n. 4
0
        private async Task <object> TryResolveNonNull(object input, Type inputType, GraphQLFieldSelection selection, IEnumerable <object> path)
        {
            var underlyingType = NonNullable.GetUnderlyingType(inputType);

            if (underlyingType != null)
            {
                input = (input as INonNullable)?.GetValue() ?? input;

                input = await this.CompleteValue(input, underlyingType, selection, this.arguments, path);

                if (input == null)
                {
                    throw new GraphQLResolveException($"Cannot return null for non-nullable field {selection.Name.Value}.");
                }

                return(input);
            }

            return(INVALID_RESULT);
        }
        public void NonNullable_GetUnderlyingType_WithNullType_ThrowsError()
        {
            Type type = null;

            Assert.Throws <ArgumentNullException>(() => NonNullable.GetUnderlyingType(type), "Value cannot be null.\nParameter name: nonNullableType");
        }
        public static Result ChangeValueTypeWithResult(object input, Type target)
        {
            if (input == null || target == null)
            {
                return(new Result(null));
            }

            if (input.GetType() == target)
            {
                return(new Result(input));
            }

            if (input is long && target == typeof(int))
            {
                int result;
                if (int.TryParse(input.ToString(), out result))
                {
                    return(new Result(result));
                }
            }

            var underlyingNonNullableType = NonNullable.GetUnderlyingType(target);

            if (underlyingNonNullableType != null)
            {
                var underlyingValueResult = ChangeValueTypeWithResult(input, underlyingNonNullableType);

                if (!underlyingValueResult.IsValid)
                {
                    return(underlyingValueResult);
                }

                return(new Result(Activator.CreateInstance(target, underlyingValueResult.Value)));
            }

            var underlyingNullableType = Nullable.GetUnderlyingType(target);

            if (underlyingNullableType != null)
            {
                return(ChangeValueTypeWithResult(input, underlyingNullableType));
            }

            if (IsCollection(target) && IsCollection(input.GetType()))
            {
                return(new Result(ChangeToCollection(input, target)));
            }

            if (IsEnum(target) && input is string)
            {
                return(new Result(Enum.Parse(target, input as string)));
            }

            if (target == typeof(ID))
            {
                if (input is string)
                {
                    return(new Result(new ID(input as string)));
                }
                if (input is int)
                {
                    return(new Result(new ID(input.ToString())));
                }
            }

            return(Result.Invalid);
        }