コード例 #1
0
        internal static TypeContext InferTypeContext(object?type)
        {
            if (type is IType t)
            {
                INamedType namedType = t.NamedType();

                if (namedType.IsInputType() && namedType.IsOutputType())
                {
                    return(TypeContext.None);
                }

                if (namedType.IsOutputType())
                {
                    return(TypeContext.Output);
                }

                if (namedType.IsInputType())
                {
                    return(TypeContext.Input);
                }
            }

            if (type is Type ts)
            {
                return(InferTypeContext(ts));
            }

            return(TypeContext.None);
        }
コード例 #2
0
        private IGraphType ConvertTypeReference(INamedType parentType, IGraphType type)
        {
            if (type is NonNullGraphType)
            {
                var nonNull = (NonNullGraphType)type;
                nonNull.ResolvedType = ConvertTypeReference(parentType, nonNull.ResolvedType);
                return(nonNull);
            }

            if (type is ListGraphType)
            {
                var list = (ListGraphType)type;
                list.ResolvedType = ConvertTypeReference(parentType, list.ResolvedType);
                return(list);
            }

            var reference = type as GraphQLTypeReference;
            var result    = reference == null ? type : this[reference.TypeName];

            if (reference != null && result == null)
            {
                throw new ExecutionError($"Unable to resolve reference to type '{reference.TypeName}' on '{parentType.Name}'");
            }

            return(result);
        }
コード例 #3
0
        protected override void Merge(
            ITypeCompletionContext context,
            INamedType type)
        {
            if (type is EnumType enumType)
            {
                // we first assert that extension and type are mutable and by
                // this that they do have a type definition.
                AssertMutable();
                enumType.AssertMutable();

                TypeExtensionHelper.MergeContextData(
                    Definition !,
                    enumType.Definition !);

                TypeExtensionHelper.MergeDirectives(
                    context,
                    Definition !.Directives,
                    enumType.Definition !.Directives);

                TypeExtensionHelper.MergeConfigurations(
                    Definition !.Configurations,
                    enumType.Definition !.Configurations);

                MergeValues(context, Definition !, enumType.Definition !);
            }
            else
            {
                throw new ArgumentException(
                          TypeResources.EnumTypeExtension_CannotMerge,
                          nameof(type));
            }
        }
コード例 #4
0
        public void RegisterSubscriptionType <T>()
            where T : class
        {
            INamedType type = RegisterObjectType <T>();

            Options.SubscriptionTypeName = type.Name;
        }
コード例 #5
0
 private static NamedTypeNode SerializeNamedType(
     INamedType namedType,
     ISet <string> referenced)
 {
     referenced.Add(namedType.Name);
     return(new NamedTypeNode(null, new NameNode(namedType.Name)));
 }
コード例 #6
0
ファイル: TypeTable.cs プロジェクト: stjordanis/magpie-csharp
        public void BindAll()
        {
            // copy the types to a queue because binding a type may cause
            // other generic types to be instantiated, adding to the type
            // table.
            mToBind = new Queue <INamedType>(mTypes.Values.Where(type => (type is Struct) || (type is Union)));

            while (mToBind.Count > 0)
            {
                INamedType type = mToBind.Dequeue();

                //### bob: call the appropriate bind function. this is gross.
                // should use an overridden method or something.
                Struct structure = type as Struct;
                if (structure != null)
                {
                    TypeBinder.Bind(mCompiler, structure);
                }
                else
                {
                    TypeBinder.Bind(mCompiler, (Union)type);
                }
            }

            mToBind = null;
        }
コード例 #7
0
        public void RegisterQueryType <T>()
            where T : class
        {
            INamedType type = RegisterObjectType <T>();

            Options.QueryTypeName = type.Name;
        }
コード例 #8
0
        internal void CompleteInputField(
            ITypeRegistry typeRegistry,
            Action <SchemaError> reportError,
            INamedType parentType)
        {
            if (!_completed)
            {
                DeclaringType = parentType;
                Type          = this.ResolveFieldType <IInputType>(typeRegistry,
                                                                   reportError, _typeReference);

                if (Type != null)
                {
                    CompleteDefaultValue(Type, reportError, parentType);

                    if (parentType is InputObjectType &&
                        Property == null &&
                        typeRegistry.TryGetTypeBinding(parentType, out InputObjectTypeBinding binding) &&
                        binding.Fields.TryGetValue(Name, out InputFieldBinding fieldBinding))
                    {
                        Property = fieldBinding.Property;
                    }
                }
                _completed = true;
            }
        }
コード例 #9
0
 private void CompleteDefaultValue(
     IInputType type,
     Action <SchemaError> reportError,
     INamedType parentType)
 {
     try
     {
         if (DefaultValue == null)
         {
             if (_nativeDefaultValue == null)
             {
                 DefaultValue = new NullValueNode();
             }
             else
             {
                 DefaultValue = type.ParseValue(_nativeDefaultValue);
             }
         }
     }
     catch (Exception ex)
     {
         reportError(new SchemaError(
                         "Could not parse the native value for input field " +
                         $"`{parentType.Name}.{Name}`.", parentType, ex));
     }
 }
コード例 #10
0
        private void ValidateFragmentSpreadIsPossible(
            ISyntaxNode node,
            IDocumentValidatorContext context)
        {
            if (context.Types.Count > 1 &&
                context.Types.TryPeek(out IType type) &&
                type.IsComplexType())
            {
                INamedType typeCondition = type.NamedType();
                INamedType parentType    = context.Types[context.Types.Count - 2].NamedType();

                if (!IsCompatibleType(parentType, typeCondition))
                {
                    context.Errors.Add(
                        ErrorBuilder.New()
                        .SetMessage(
                            "The parent type does not match the type condition on the fragment.")
                        .AddLocation(node)
                        .SetPath(context.CreateErrorPath())
                        .SetExtension("typeCondition", typeCondition.Visualize())
                        .SetFragmentName(node)
                        .SpecifiedBy("sec-Fragment-spread-is-possible")
                        .Build());
                }
            }
        }
コード例 #11
0
        internal void RegisterDependencies(
            ISchemaContext schemaContext,
            Action <SchemaError> reportError,
            INamedType parentType)
        {
            if (!_completed)
            {
                if (_typeReference != null)
                {
                    schemaContext.Types.RegisterType(_typeReference);
                }

                if (_member != null)
                {
                    schemaContext.Resolvers.RegisterResolver(
                        new MemberResolverBinding(parentType.Name, Name, _member));
                }

                foreach (InputField argument in _argumentMap.Values)
                {
                    argument.RegisterDependencies(
                        schemaContext.Types, reportError, parentType);
                }
            }
        }
コード例 #12
0
    private static bool IsCompatibleType(
        IDocumentValidatorContext context,
        INamedType parentType,
        INamedType typeCondition)
    {
        if (parentType.IsAssignableFrom(typeCondition))
        {
            return(true);
        }

        IReadOnlyCollection <ObjectType> types1 = context.Schema.GetPossibleTypes(parentType);
        IReadOnlyCollection <ObjectType> types2 = context.Schema.GetPossibleTypes(typeCondition);

        foreach (ObjectType a in types1)
        {
            foreach (ObjectType b in types2)
            {
                if (ReferenceEquals(a, b))
                {
                    return(true);
                }
            }
        }

        return(false);
    }
コード例 #13
0
        private static void AddInputTypeProperties(
            Dictionary <NameString, InputTypeDescriptorModel> typeDescriptors,
            Dictionary <NameString, INamedTypeDescriptor> leafTypeDescriptors)
        {
            foreach (InputTypeDescriptorModel typeDescriptorModel in typeDescriptors.Values)
            {
                var properties = new List <PropertyDescriptor>();

                foreach (var field in typeDescriptorModel.Model.Fields)
                {
                    INamedTypeDescriptor?fieldType;
                    INamedType           namedType = field.Type.NamedType();

                    if (namedType.IsScalarType() || namedType.IsEnumType())
                    {
                        fieldType = leafTypeDescriptors[namedType.Name];
                    }
                    else
                    {
                        fieldType = GetInputTypeDescriptor(
                            field.Type.NamedType(),
                            typeDescriptors);
                    }

                    properties.Add(
                        new PropertyDescriptor(
                            field.Name,
                            BuildFieldType(
                                field.Type,
                                fieldType)));
                }

                typeDescriptorModel.Descriptor.CompleteProperties(properties);
            }
        }
コード例 #14
0
        public IBuildPlanPolicy CreatePlan(IBuilderContext context, INamedType buildKey)
        {
            var buildMethod = _resolveMethod.MakeGenericMethod(_getTypeFunc(context))
                              .CreateDelegate(typeof(DynamicBuildPlanMethod));

            return(new DynamicMethodBuildPlan((DynamicBuildPlanMethod)buildMethod));
        }
コード例 #15
0
ファイル: TypeInfo.cs プロジェクト: zaneclaes/hotchocolate
        public IType CreateType(INamedType namedType)
        {
            if (Components.Count == 1)
            {
                return(namedType);
            }

            IType current = namedType;

            for (var i = Components.Count - 2; i >= 0; i--)
            {
                switch (Components[i].Kind)
                {
                case TypeComponentKind.Named:
                    throw new InvalidOperationException();

                case TypeComponentKind.NonNull:
                    current = new NonNullType(current);
                    break;

                case TypeComponentKind.List:
                    current = new ListType(current);
                    break;
                }
            }

            return(current);
        }
コード例 #16
0
        internal override void Merge(
            ICompletionContext context,
            INamedType type)
        {
            if (type is InputObjectType inputObjectType)
            {
                TypeExtensionHelper.MergeContextData(
                    Definition,
                    inputObjectType.Definition);

                TypeExtensionHelper.MergeDirectives(
                    context,
                    Definition.Directives,
                    inputObjectType.Definition.Directives);

                TypeExtensionHelper.MergeInputObjectFields(
                    context,
                    Definition.Fields,
                    inputObjectType.Definition.Fields);

                TypeExtensionHelper.MergeConfigurations(
                    Definition.Configurations,
                    inputObjectType.Definition.Configurations);
            }
            else
            {
                throw new ArgumentException(
                          TypeResources.InputObjectTypeExtension_CannotMerge,
                          nameof(type));
            }
        }
コード例 #17
0
ファイル: TypeLookup.cs プロジェクト: eximarus/hotchocolate
        public Type GetSerializationType(IType type)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            INamedType namedType = type.NamedType();

            if (!namedType.IsLeafType())
            {
                throw new ArgumentException("Only leaf types are allowed.", nameof(type));
            }

            if (namedType.IsEnumType())
            {
                return(typeof(string));
            }

            if (!_leafTypes.TryGetValue(namedType.Name, out LeafTypeInfo? typeInfo))
            {
                throw new NotSupportedException(
                          $"Leaf type `{namedType.Name}` is not supported.");
            }

            return(typeInfo.SerializationType);
        }
コード例 #18
0
 private static NamedTypeNode SerializeNamedType(
     INamedType namedType,
     ReferencedTypes referenced)
 {
     referenced.TypeNames.Add(namedType.Name);
     return(new NamedTypeNode(null, new NameNode(namedType.Name)));
 }
コード例 #19
0
        private bool InferNamedType(INamedType decl)
        {
            if (!TryInferParam(decl))
            {
                var named = ParamType as NamedType;
                if (named == null)
                {
                    mFailed = true;
                    return(false);
                }

                if (named.TypeArgs.Length != decl.TypeArguments.Length)
                {
                    mFailed = true;
                    return(false);
                }

                for (int i = 0; i < named.TypeArgs.Length; i++)
                {
                    mParamTypes.Push(named.TypeArgs[i]);
                    decl.TypeArguments[i].Accept(this);
                    mParamTypes.Pop();
                }
            }

            return(false);
        }
コード例 #20
0
        public bool TryGetProperty <T>(
            INamedType namedType, NameString fieldName, out T member)
            where T : MemberInfo
        {
            if (namedType is ObjectType &&
                _schemaContext.Types.TryGetTypeBinding(namedType,
                                                       out ObjectTypeBinding binding) &&
                binding.Fields.TryGetValue(fieldName,
                                           out FieldBinding fieldBinding) &&
                fieldBinding.Member is T m)
            {
                member = m;
                return(true);
            }


            if (namedType is InputObjectType &&
                _schemaContext.Types.TryGetTypeBinding(namedType,
                                                       out InputObjectTypeBinding inputBinding) &&
                inputBinding.Fields.TryGetValue(fieldName,
                                                out InputFieldBinding inputFieldBinding) &&
                inputFieldBinding.Property is T p)
            {
                member = p;
                return(true);
            }

            member = null;
            return(false);
        }
コード例 #21
0
        private static ITypeDefinitionNode SerializeNonScalarTypeDefinition(
            INamedType namedType,
            ReferencedTypes referenced)
        {
            switch (namedType)
            {
            case ObjectType type:
                return(SerializeObjectType(type, referenced));

            case InterfaceType type:
                return(SerializeInterfaceType(type, referenced));

            case InputObjectType type:
                return(SerializeInputObjectType(type, referenced));

            case UnionType type:
                return(SerializeUnionType(type, referenced));

            case EnumType type:
                return(SerializeEnumType(type, referenced));

            default:
                throw new NotSupportedException();
            }
        }
コード例 #22
0
        public ITypeInfo GetTypeInfo(IType fieldType, bool readOnly)
        {
            INamedType namedType = fieldType.NamedType();

            if (namedType.IsLeafType())
            {
                if (!_leafTypes.TryGetValue(namedType.Name, out LeafTypeInfo type))
                {
                    throw new NotSupportedException(
                              $"Leaf type `{namedType.Name}` is not supported.");
                }

                var typeInfo = new TypeInfo();
                typeInfo.Type              = fieldType;
                typeInfo.SchemaTypeName    = namedType.Name;
                typeInfo.SerializationType = type.SerializationType;

                BuildTypeInfo(type.ClrType, fieldType, readOnly, typeInfo);

                return(typeInfo);
            }

            throw new NotSupportedException(
                      "Type infos are only supported for leaf types.");
        }
コード例 #23
0
        /// <summary>
        /// Creates a new the MVC controllers info
        /// </summary>
        /// <param name="namedType">The named type for the controller</param>
        /// <param name="actionFilter">The parse filter to use for the MVC action attribute</param>
        /// <param name="typeFactory">The type table</param>
        internal MvcController(INamedType namedType, TypeFilter actionFilter, TypeFactory typeFactory)
        {
            NamedType    = namedType;
            _typeFactory = typeFactory;

            foreach (IMethod method in namedType.Methods)
            {
                if (method.Attributes.Any(attrData => actionFilter.Matches(attrData.AttributeType)))
                {
                    MvcAction action = new MvcAction(this, method, typeFactory);
                    _actions.Add(action);
                }
            }

            var baseType = NamedType.BaseType;

            while (baseType != null)
            {
                if (baseType.FullName == MvcConstants.ControllerBaseFullName_AspNetCore)
                {
                    IsAspNetCore = true;
                    break;
                }
                baseType = baseType.BaseType;
            }
        }
コード例 #24
0
        internal override void Merge(
            ICompletionContext context,
            INamedType type)
        {
            if (type is UnionType unionType)
            {
                TypeExtensionHelper.MergeContextData(
                    Definition,
                    unionType.Definition);

                TypeExtensionHelper.MergeDirectives(
                    context,
                    Definition.Directives,
                    unionType.Definition.Directives);

                TypeExtensionHelper.MergeTypes(
                    Definition.Types,
                    unionType.Definition.Types);
            }
            else
            {
                throw new ArgumentException(
                          TypeResources.UnionTypeExtension_CannotMerge);
            }
        }
コード例 #25
0
        internal override void Merge(
            ITypeCompletionContext context,
            INamedType type)
        {
            if (type is EnumType enumType)
            {
                TypeExtensionHelper.MergeContextData(
                    Definition,
                    enumType.Definition);

                TypeExtensionHelper.MergeDirectives(
                    context,
                    Definition.Directives,
                    enumType.Definition.Directives);

                TypeExtensionHelper.MergeConfigurations(
                    Definition.Configurations,
                    enumType.Definition.Configurations);

                MergeValues(context, Definition, enumType.Definition);
            }
            else
            {
                throw new ArgumentException(
                          TypeResources.EnumTypeExtension_CannotMerge,
                          nameof(type));
            }
        }
コード例 #26
0
ファイル: NamedType.cs プロジェクト: someguy20336/TypeRight
 // TODO: factory for specifc types
 public NamedType(string name,
                  string fullName,
                  TypeFlags flags = null,
                  IReadOnlyList <IProperty> properties = null,
                  IReadOnlyList <IMethod> methods      = null,
                  INamedType baseType = null,
                  IReadOnlyList <INamedType> interfaces = null,
                  string filePath = null,
                  INamedType constructedFromType            = null,
                  IReadOnlyList <IType> typeArguments       = null,
                  IReadOnlyList <IField> fields             = null,
                  IReadOnlyList <IAttributeData> attributes = null,
                  string comments = "")
 {
     Name                = name;
     FullName            = fullName;
     Properties          = properties ?? new List <Property>();
     BaseType            = baseType;
     ConstructedFromType = constructedFromType ?? this;
     Interfaces          = interfaces ?? new List <NamedType>();
     FilePath            = filePath;
     TypeArguments       = typeArguments ?? new List <TypeBase>();
     Fields              = fields ?? new List <IField>();
     Methods             = methods ?? new List <IMethod>();
     Attributes          = attributes ?? new List <IAttributeData>();
     Flags               = flags ?? new TypeFlags();
     Comments            = comments;
 }
コード例 #27
0
        internal override void Merge(
            ITypeCompletionContext context,
            INamedType type)
        {
            if (type is ObjectType objectType)
            {
                TypeExtensionHelper.MergeContextData(
                    Definition,
                    objectType.Definition);

                TypeExtensionHelper.MergeDirectives(
                    context,
                    Definition.Directives,
                    objectType.Definition.Directives);

                TypeExtensionHelper.MergeInterfaces(
                    Definition,
                    objectType.Definition);

                TypeExtensionHelper.MergeObjectFields(
                    context,
                    objectType.Definition.RuntimeType,
                    Definition.Fields,
                    objectType.Definition.Fields);

                TypeExtensionHelper.MergeConfigurations(
                    Definition.Configurations,
                    objectType.Definition.Configurations);
            }
            else
            {
                throw new ArgumentException(
                          TypeResources.ObjectTypeExtension_CannotMerge);
            }
        }
コード例 #28
0
        internal override void Merge(
            ICompletionContext context,
            INamedType type)
        {
            if (type is ObjectType objectType)
            {
                TypeExtensionHelper.MergeContextData(
                    Definition,
                    objectType.Definition);

                TypeExtensionHelper.MergeDirectives(
                    context,
                    Definition.Directives,
                    objectType.Definition.Directives);

                TypeExtensionHelper.MergeObjectFields(
                    context,
                    Definition.Fields,
                    objectType.Definition.Fields);
            }
            else
            {
                // TODO : resources
                throw new ArgumentException("CANNOT MERGE");
            }
        }
コード例 #29
0
 public bool TryGetTypeBinding <T>(
     INamedType namedType,
     out T typeBinding)
     where T : ITypeBinding
 {
     return(TryGetTypeBinding(namedType.Name, out typeBinding));
 }
コード例 #30
0
        private static IReadOnlyList <INamedType> CollectLeafTypes(
            IEnumerable <IResultParserMethodDescriptor> parseMethods)
        {
            var leafTypes = new Dictionary <string, INamedType>();

            foreach (IResultParserMethodDescriptor method in parseMethods)
            {
                foreach (IClassDescriptor possibleType in
                         method.PossibleTypes.Select(t => t.ResultDescriptor))
                {
                    foreach (IOutputField field in
                             possibleType.Fields.Select(t => t.Field))
                    {
                        if (field.Type.IsLeafType())
                        {
                            INamedType namedType = field.Type.NamedType();
                            if (!leafTypes.ContainsKey(namedType.Name))
                            {
                                leafTypes.Add(namedType.Name, namedType);
                            }
                        }
                    }
                }
            }

            return(leafTypes.Values.ToList());
        }
コード例 #31
0
ファイル: TypeTable.cs プロジェクト: rwaldron/magpie
        public void Add(INamedType type, IEnumerable<IBoundDecl> typeArgs)
        {
            var uniqueName = GetUniqueName(type.Name, typeArgs);

            if (mTypes.ContainsKey(uniqueName)) throw new CompileException(type.Position, "There is already a type named " + uniqueName + ".");

            mTypes[uniqueName] = type;

            /*
            // if we are in the middle of binding, make sure we bind this type too
            if (mToBind != null)
            {
                mToBind.Enqueue(type);
            }*/
        }
コード例 #32
0
ファイル: TypeTable.cs プロジェクト: rwaldron/magpie
 public void Add(INamedType type)
 {
     Add(type, null);
 }