示例#1
0
        public GenericType(IGraphQLBuilder builder = null)
        {
            try
            {
                var typedClass = typeof(T);
                var props      = typedClass.GetProperties();
                foreach (var prop in props)
                {
                    Type propGraphQLType = null;

                    propGraphQLType = prop.PropertyType.TryConvertToGraphQLType();

                    if (propGraphQLType != null && GraphQLExtensions.IsExtendedGraphQLType(propGraphQLType))
                    {
                        var resolvedType = propGraphQLType;
                        builder
                        .GetType()
                        .GetInterface("IGraphQLBuilder")
                        .GetMethod("Type")
                        .MakeGenericMethod(resolvedType)
                        .Invoke(builder, null);
                    }
                    else if (propGraphQLType is null)
                    {
                        var resolvedType = propGraphQLType ?? prop.PropertyType;

                        builder
                        .GetType()
                        .GetInterface("IGraphQLBuilder")
                        .GetMethod("Type")
                        .MakeGenericMethod(resolvedType)
                        .Invoke(builder, null);

                        propGraphQLType = typeof(GenericType <>).MakeGenericType(resolvedType);

                        if (propGraphQLType is null)
                        {
                            throw new InvalidCastException(
                                      $"{prop.Name} was not automatically convertible into a GraphQL type. " +
                                      $"Try explicitly adding this Type through the GraphQL-Core middleware.");
                        }

                        propGraphQLType = GraphQLCoreTypeWrapperGenerator.GetDerivedGenericUserType(propGraphQLType);

                        if (propGraphQLType is null)
                        {
                            throw new NotSupportedException(
                                      $"{prop.Name} is a custom type but was not registered through builder. " +
                                      $"Try explicitly adding this Type through the Graph-Core middleware");
                        }
                    }

                    Field(propGraphQLType, prop.Name);
                }
            }
            catch (Exception e)
            {
                throw new GraphQLCoreTypeException($"An attempt to create a generic type for type {nameof(T)} failed. Refer to inner exception for details", e);
            }
        }