Exemplo n.º 1
0
        public ServiceProviderFixture()
        {
            var clrToGraphTypeMappings = new ClrToGraphTypeMappings();
            var schema = new EmptySchema();
            IEnumerable <(Type clrType, Type graphType)> typeMappings = schema.BuiltInTypeMappings;

            foreach (var(clrType, graphType) in typeMappings)
            {
                if (!clrToGraphTypeMappings.ContainsKey(clrType))
                {
                    clrToGraphTypeMappings.Add(clrType, graphType);
                }
            }

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddGraphQLSchemaDeclareService(null, () => clrToGraphTypeMappings);
            ServiceProvider = serviceCollection.BuildServiceProvider();
        }
Exemplo n.º 2
0
        protected Type GetGraphTypeFromRealType(Type realType, TypeToGraphTypeTransformerOptions options)
        {
            var isTypeList = IsTypeList(realType);

            if (isTypeList)
            {
                var genericType = typeof(ListGraphType <>);
                var clrType     = realType.IsArray ? realType.GetElementType() : realType.GenericTypeArguments.First();

                realType = GetRealType(clrType);

                typeMappings.TryGetValue(realType, out Type graphType);

                if (options.AddNullableInfo && !IsTypeNullable(clrType, clrType))
                {
                    graphType = GenerateNonNullGraphType(graphType);
                }

                Type[] typeArgs  = { graphType };
                var    finalType = genericType.MakeGenericType(typeArgs);
                return(finalType);
            }

            if (realType == typeof(DateTime))
            {
                return(typeof(DateTimeGraphType));
            }

            if (realType.IsEnum)
            {
                var    gType     = typeof(EnumerationGraphType <>);
                Type[] typeArgs  = { realType };
                var    finalType = gType.MakeGenericType(typeArgs);
                return(finalType);
            }

            return(typeMappings.ContainsKey(realType) ? typeMappings[realType] : null);
        }