示例#1
0
 private void CheckIfAllInterfaceFieldsAreImplemented(
     ITypeInitializationContext context)
 {
     foreach (InterfaceType interfaceType in _interfaceMap.Values)
     {
         foreach (InterfaceField interfaceField in interfaceType.Fields)
         {
             if (Fields.TryGetField(interfaceField.Name, out ObjectField field))
             {
                 foreach (InputField interfaceArgument in interfaceField.Arguments)
                 {
                     if (!field.Arguments.ContainsField(
                             interfaceArgument.Name))
                     {
                         context.ReportError(new SchemaError(
                                                 $"Object type {Name} does not implement " +
                                                 $"all arguments of field {interfaceField.Name} " +
                                                 $"from interface {interfaceType.Name}.",
                                                 this));
                     }
                 }
             }
             else
             {
                 context.ReportError(new SchemaError(
                                         $"Object type {Name} does not implement the " +
                                         $"field {interfaceField.Name} " +
                                         $"from interface {interfaceType.Name}.",
                                         this));
             }
         }
     }
 }
示例#2
0
        private void ValidateObjectField(
            ITypeInitializationContext context,
            InterfaceField first)
        {
            if (Fields.TryGetField(first.Name, out ObjectField field))
            {
                if (!field.Type.IsEqualTo(first.Type))
                {
                    context.ReportError(new SchemaError(
                                            "The return type of the interface field " +
                                            $"{first.Name} does not match the field declared " +
                                            $"by object type {Name}.",
                                            this));
                }

                if (!ArgumentsAreEqual(field.Arguments, first.Arguments))
                {
                    context.ReportError(new SchemaError(
                                            $"Object type {Name} does not implement " +
                                            $"all arguments of field {first.Name} " +
                                            $"from interface {first.DeclaringType.Name}.",
                                            this));
                }
            }
            else
            {
                context.ReportError(new SchemaError(
                                        $"Object type {Name} does not implement the " +
                                        $"field {first.Name} " +
                                        $"from interface {first.DeclaringType.Name}.",
                                        this));
            }
        }
示例#3
0
        private void CompleteDirective(
            ITypeInitializationContext context,
            DirectiveDescription description,
            HashSet <string> processed)
        {
            DirectiveReference reference =
                DirectiveReference.FromDescription(description);
            DirectiveType directiveType = context.GetDirectiveType(reference);

            if (directiveType != null)
            {
                if (!processed.Add(directiveType.Name) &&
                    !directiveType.IsRepeatable)
                {
                    context.ReportError(new SchemaError(
                                            $"The specified directive `@{directiveType.Name}` " +
                                            "is unique and cannot be added twice.",
                                            context.Type as INamedType));
                }
                else if (directiveType.Locations.Contains(_location))
                {
                    _directives.Add(Directive.FromDescription(
                                        directiveType, description, _source));
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            $"The specified directive `@{directiveType.Name}` " +
                                            "is not allowed on the current location " +
                                            $"`{_location}`.",
                                            context.Type as INamedType));
                }
            }
        }
示例#4
0
        public static T ResolveFieldType <T>(
            this ITypeInitializationContext context,
            IField field,
            TypeReference typeReference)
            where T : IType
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            T type = default(T);

            if (typeReference != null)
            {
                type = context.GetType <T>(typeReference);
            }

            if (ReferenceEquals(type, default(T)))
            {
                context.ReportError(new SchemaError(
                                        $"The type `{typeReference}` of field " +
                                        $"`{field.DeclaringType.Name}.{field.Name}` " +
                                        "could not be resolved to a valid schema type.",
                                        field.DeclaringType));
            }

            return(type);
        }
示例#5
0
        private void CompleteResolver(
            ITypeInitializationContext context)
        {
            if (Resolver == null)
            {
                Resolver = context.GetResolver(Name);
            }

            Middleware = context.CreateMiddleware(
                _middlewareComponents, Resolver,
                IsIntrospectionField ||
                DeclaringType.IsIntrospectionType());

            if (Resolver == null && Middleware == null)
            {
                if (_executableDirectives.Any())
                {
                    Middleware = ctx => Task.CompletedTask;
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            $"The field `{context.Type.Name}.{Name}` " +
                                            "has no resolver.", (INamedType)context.Type));
                }
            }

            _middlewareComponents = null;
        }
示例#6
0
 private void CompleteDefaultValue(
     ITypeInitializationContext context,
     IInputType type)
 {
     try
     {
         if (DefaultValue == null)
         {
             if (_nativeDefaultValue == null)
             {
                 DefaultValue = NullValueNode.Default;
             }
             else
             {
                 DefaultValue = type.ParseValue(_nativeDefaultValue);
             }
         }
     }
     catch (Exception ex)
     {
         context.ReportError(new SchemaError(
                                 "Could not parse the native value of input field " +
                                 $"`{context.Type.Name}.{Name}`.", context.Type, ex));
     }
 }
示例#7
0
        private static bool TryCreateNativeTypeParserDeserializer(
            ITypeInitializationContext context,
            InputObjectType inputObjectType,
            Type nativeType,
            out Func <ObjectValueNode, object> deserializer)
        {
            if (nativeType.IsDefined(typeof(GraphQLLiteralParserAttribute)))
            {
                Type parserType = nativeType
                                  .GetCustomAttribute <GraphQLLiteralParserAttribute>().Type;
                if (typeof(ILiteralParser).IsAssignableFrom(parserType))
                {
                    var parser = (ILiteralParser)Activator
                                 .CreateInstance(parserType);
                    deserializer = parser.ParseLiteral;
                    return(true);
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            "A literal parser has to implement `ILiteralParser`.",
                                            inputObjectType));
                }
            }

            deserializer = null;
            return(false);
        }
示例#8
0
        public static T ResolveFieldType <T>(
            this ITypeInitializationContext context,
            IField field,
            TypeReference typeReference)
            where T : IType
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            T type = default(T);

            if (typeReference != null)
            {
                type = context.GetType <T>(typeReference);
            }

            if (ReferenceEquals(type, default(T)))
            {
                context.ReportError(new SchemaError(
                                        TypeResources.Field_Cannot_ResolveType(
                                            field.DeclaringType.Name,
                                            field.Name,
                                            typeReference),
                                        field.DeclaringType));
            }

            return(type);
        }
示例#9
0
 protected override void OnCompleteType(ITypeInitializationContext context)
 {
     if (!Values.Any())
     {
         context.ReportError(new SchemaError(
                                 $"The enum type `{Name}` has no values."));
     }
 }
示例#10
0
 private void ValidateFieldsRequirement(
     ITypeInitializationContext context)
 {
     if (Fields.Count == 0)
     {
         context.ReportError(new SchemaError(
                                 $"Interface `{Name}` has no fields declared.",
                                 this));
     }
 }
示例#11
0
 protected override void OnCompleteType(
     ITypeInitializationContext context)
 {
     if (!Values.Any())
     {
         context.ReportError(new SchemaError(
                                 string.Format(
                                     CultureInfo.InvariantCulture,
                                     TypeResources.EnumType_NoValues,
                                     Name)));
     }
 }
示例#12
0
        private bool ValidateInterfaceFieldGroup(
            ITypeInitializationContext context,
            InterfaceField first,
            IGrouping <string, InterfaceField> interfaceField)
        {
            if (interfaceField.Count() == 1)
            {
                return(true);
            }

            foreach (InterfaceField field in interfaceField)
            {
                if (!field.Type.IsEqualTo(first.Type))
                {
                    context.ReportError(new SchemaError(
                                            "The return type of the interface field " +
                                            $"{first.Name} from interface " +
                                            $"{first.DeclaringType.Name} and " +
                                            $"{field.DeclaringType.Name} do not match " +
                                            $"and are implemented by object type {Name}.",
                                            this));
                    return(false);
                }

                if (!ArgumentsAreEqual(field.Arguments, first.Arguments))
                {
                    context.ReportError(new SchemaError(
                                            $"The arguments of the interface field {first.Name} " +
                                            $"from interface {first.DeclaringType.Name} and " +
                                            $"{field.DeclaringType.Name} do not match " +
                                            $"and are implemented by object type {Name}.",
                                            this));
                    return(false);
                }
            }

            return(true);
        }
示例#13
0
        private void CompleteFields(
            ITypeInitializationContext context)
        {
            foreach (INeedsInitialization field in Fields
                     .Cast <INeedsInitialization>())
            {
                field.CompleteType(context);
            }

            if (Fields.IsEmpty)
            {
                context.ReportError(new SchemaError(
                                        $"The input object `{Name}` does not have any fields."));
            }
        }
示例#14
0
 private void CompleteResolver(
     ITypeInitializationContext context)
 {
     if (Resolver == null)
     {
         Resolver = context.GetResolver(Name);
         if (Resolver == null &&
             _executableDirectives.All(
                 t => t.Middleware == null))
         {
             context.ReportError(new SchemaError(
                                     $"The field `{context.Type.Name}.{Name}` " +
                                     "has no resolver.", context.Type));
         }
     }
 }
示例#15
0
        protected override void OnCompleteType(
            ITypeInitializationContext context)
        {
            base.OnCompleteType(context);

            if (Resolver == null)
            {
                Resolver = context.GetResolver(Name);
                if (Resolver == null)
                {
                    context.ReportError(new SchemaError(
                                            $"The field `{context.Type.Name}.{Name}` " +
                                            "has no resolver.", context.Type));
                }
            }
        }
示例#16
0
        private void CompleteFields(
            ITypeInitializationContext context)
        {
            foreach (INeedsInitialization field in Fields
                     .Cast <INeedsInitialization>())
            {
                field.CompleteType(context);
            }

            if (Fields.IsEmpty)
            {
                context.ReportError(new SchemaError(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        TypeResources.InputObjectType_NoFields,
                                                        Name)));
            }
        }
示例#17
0
        private void CompleteTypes(
            ITypeInitializationContext context)
        {
            if (_types != null)
            {
                foreach (ObjectType memberType in CreateUnionTypeSet(context))
                {
                    _typeMap[memberType.Name] = memberType;
                }
            }

            if (_typeMap.Count == 0)
            {
                context.ReportError(new SchemaError(
                                        "A Union type must define one or more unique member types.",
                                        this));
            }
        }
        private void CompleteNativeType(
            ITypeInitializationContext context)
        {
            if (ClrType == null &&
                context.TryGetNativeType(this, out Type nativeType))
            {
                ClrType = nativeType;
            }

            if (ClrType == null)
            {
                // TODO :resources
                context.ReportError(new SchemaError(
                                        "Could not resolve the native type associated with " +
                                        $"input object type `{Name}`.",
                                        this));
            }
        }
示例#19
0
        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);
        }
示例#20
0
        private void CompleteNativeType(
            ITypeInitializationContext context)
        {
            if (ClrType == null &&
                context.TryGetNativeType(this, out Type nativeType))
            {
                ClrType = nativeType;
            }

            if (ClrType == null)
            {
                context.ReportError(new SchemaError(
                                        "Could not resolve the native type associated with " +
                                        $"input object type `{Name}`.",
                                        this));
            }

            _deserialize = InputObjectDeserializerFactory.Create(
                context, this, ClrType);
        }
示例#21
0
        private void CompleteTypes(
            ITypeInitializationContext context)
        {
            if (_types != null)
            {
                foreach (ObjectType memberType in _types
                         .Select(t => context.GetType <ObjectType>(t))
                         .Where(t => t != null))
                {
                    _typeMap[memberType.Name] = memberType;
                }
            }

            if (_typeMap.Count == 0)
            {
                context.ReportError(new SchemaError(
                                        "A Union type must define one or more unique member types.",
                                        this));
            }
        }
示例#22
0
        private void CompleteResolver(
            ITypeInitializationContext context)
        {
            if (Resolver == null)
            {
                Resolver = context.GetResolver(Name);
            }

            // TODO : review if that is what we want. Sometimes a middleware can replace a resolver ... but not all middleware components are resolver replacements.
            Resolver = context.CreateFieldMiddleware(
                _middlewareComponents, Resolver);

            // TODO : All executable middlewars should have a middleware so could we rewrite this?
            if (Resolver == null && _executableDirectives.All(
                    t => t.Middleware == null))
            {
                context.ReportError(new SchemaError(
                                        $"The field `{context.Type.Name}.{Name}` " +
                                        "has no resolver.", context.Type));
            }

            _middlewareComponents = null;
        }
示例#23
0
        protected override void OnRegisterDependencies(
            ITypeInitializationContext context)
        {
            base.OnRegisterDependencies(context);

            if (Locations.Count == 0)
            {
                context.ReportError(new SchemaError(
                                        $"The `{Name}` directive does not declare any " +
                                        "location on which it is valid."));
            }

            foreach (INeedsInitialization argument in Arguments
                     .Cast <INeedsInitialization>())
            {
                argument.RegisterDependencies(context);
            }

            if (_middleware != null)
            {
                context.RegisterMiddleware(_middleware);
            }
        }
        private void CompleteDirecive(
            ITypeInitializationContext context,
            DirectiveDescription description)
        {
            DirectiveReference reference =
                DirectiveReference.FromDescription(description);
            DirectiveType type = context.GetDirectiveType(reference);

            if (type != null)
            {
                if (type.Locations.Contains(_location))
                {
                    _directives.Add(
                        Directive.FromDescription(type, description, _source));
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            $"The specified directive `{type.Name}` is not " +
                                            $"allowed on the current location `{_location}`.",
                                            context.Type));
                }
            }
        }