private static void CollectComputeDependencies(
            Context context,
            IComplexOutputType type,
            IOutputField field)
        {
            IDirective directive = field.Directives[DirectiveNames.Computed]
                                   .FirstOrDefault();

            if (directive != null)
            {
                NameString[] dependantOn = directive
                                           .ToObject <ComputedDirective>()
                                           .DependantOn;

                if (dependantOn != null)
                {
                    foreach (string fieldName in dependantOn)
                    {
                        if (type.Fields.TryGetField(
                                fieldName,
                                out IOutputField dependency))
                        {
                            context.Dependencies.Add(new FieldDependency(
                                                         type.Name, dependency.Name));
                        }
                    }
                }
            }
        }
Exemple #2
0
    public static void EnsureFieldNamesAreValid(
        IComplexOutputType type,
        ICollection <ISchemaError> errors)
    {
        for (var i = 0; i < type.Fields.Count; i++)
        {
            IOutputField field = type.Fields[i];

            if (!field.IsIntrospectionField)
            {
                if (field.Name.Value.StartsWith(_twoUnderscores))
                {
                    errors.Add(TwoUnderscoresNotAllowedField(type, field));
                }

                for (var j = 0; j < field.Arguments.Count; j++)
                {
                    IInputField argument = field.Arguments[j];

                    if (argument.Name.Value.StartsWith(_twoUnderscores))
                    {
                        errors.Add(TwoUnderscoresNotAllowedOnArgument(
                                       type, field, argument));
                    }
                }
            }
        }
    }
Exemple #3
0
    private static void ValidateImplementation(
        IComplexOutputType type,
        IInterfaceType implementedType,
        ICollection <ISchemaError> errors)
    {
        if (!IsFullyImplementingInterface(type, implementedType))
        {
            errors.Add(NotTransitivelyImplemented(type, implementedType));
        }

        foreach (IOutputField implementedField in implementedType.Fields)
        {
            if (type.Fields.TryGetField(implementedField.Name, out IOutputField? field))
            {
                ValidateArguments(field, implementedField, errors);

                if (!IsValidImplementationFieldType(field.Type, implementedField.Type))
                {
                    errors.Add(InvalidFieldType(type, field, implementedField));
                }
            }
            else
            {
                errors.Add(FieldNotImplemented(type, implementedField));
            }
        }
    }
        private IReadOnlyList <IFieldDescriptor> CreateFields(
            IComplexOutputType type,
            IEnumerable <ISelectionNode> selections,
            Func <string, bool> addField,
            Path path)
        {
            var fields = new Dictionary <string, FieldSelection>();

            foreach (FieldNode selection in selections.OfType <FieldNode>())
            {
                NameString responseName = selection.Alias == null
                    ? selection.Name.Value
                    : selection.Alias.Value;

                if (addField(responseName))
                {
                    FieldCollector.ResolveFieldSelection(
                        type,
                        selection,
                        path,
                        fields);
                }
            }

            return(fields.Values.Select(t =>
            {
                string responseName = (t.Selection.Alias ?? t.Selection.Name).Value;
                return new FieldDescriptor(
                    t.Field,
                    t.Selection,
                    t.Field.Type,
                    path.Append(responseName));
            }).ToList());
        }
Exemple #5
0
        private static IReadOnlyList <VariableValue> ResolveScopedVariables(
            IResolverContext context,
            NameString schemaName,
            OperationType operationType,
            IEnumerable <SelectionPathComponent> components)
        {
            IStitchingContext stitchingContext =
                context.Service <IStitchingContext>();

            ISchema remoteSchema =
                stitchingContext.GetRemoteSchema(schemaName);

            IComplexOutputType type =
                remoteSchema.GetOperationType(operationType);

            var variables = new List <VariableValue>();

            SelectionPathComponent[] comps = components.Reverse().ToArray();

            for (int i = 0; i < comps.Length; i++)
            {
                SelectionPathComponent component = comps[i];

                if (!type.Fields.TryGetField(component.Name.Value,
                                             out IOutputField field))
                {
                    throw new QueryException(new Error
                    {
                        Message = string.Format(
                            CultureInfo.InvariantCulture,
                            StitchingResources
                            .DelegationMiddleware_PathElementInvalid,
                            component.Name.Value,
                            type.Name)
                    });
                }

                ResolveScopedVariableArguments(
                    context, component, field, variables);

                if (i + 1 < comps.Length)
                {
                    if (!field.Type.IsComplexType())
                    {
                        throw new QueryException(new Error
                        {
                            Message = StitchingResources
                                      .DelegationMiddleware_PathElementTypeUnexpected
                        });
                    }
                    type = (IComplexOutputType)field.Type.NamedType();
                }
            }

            return(variables);
        }
Exemple #6
0
 public static void EnsureTypeHasFields(
     IComplexOutputType type,
     ICollection <ISchemaError> errors)
 {
     if (type.Fields.Count == 0 ||
         type.Fields.All(t => t.IsIntrospectionField))
     {
         errors.Add(NeedsOneAtLeastField(type));
     }
 }
 private static bool FieldExists(
     IComplexOutputType type,
     NameString fieldName)
 {
     if (IsTypeNameField(fieldName))
     {
         return(true);
     }
     return(type.Fields.ContainsField(fieldName));
 }
Exemple #8
0
 public static void EnsureInterfacesAreCorrectlyImplemented(
     IComplexOutputType type,
     ICollection <ISchemaError> errors)
 {
     if (type.Implements.Count > 0)
     {
         foreach (IInterfaceType implementedType in type.Implements)
         {
             ValidateImplementation(type, implementedType, errors);
         }
     }
 }
Exemple #9
0
 private static bool IsFullyImplementingInterface(
     IComplexOutputType type,
     IInterfaceType implementedType)
 {
     foreach (IInterfaceType?interfaceType in implementedType.Implements)
     {
         if (!type.IsImplementing(interfaceType))
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #10
0
        public static ISchemaError NeedsOneAtLeastField(IComplexOutputType type)
        {
            bool isInterface = type.Kind == TypeKind.Interface;

            return(SchemaErrorBuilder.New()
                   .SetMessage(
                       "The {0} type `{1}` has to at least define one field in " +
                       "order to be valid.",
                       isInterface ? _interface : _object,
                       type.Name)
                   .SetType(type)
                   .SetSpecifiedBy(isInterface)
                   .Build());
        }
Exemple #11
0
        public static ISchemaError TwoUnderscoresNotAllowedField(
            IComplexOutputType type,
            IOutputField field)
        {
            bool isInterface = type.Kind == TypeKind.Interface;

            return(SchemaErrorBuilder.New()
                   .SetMessage(
                       "Field names starting with `__` are reserved for " +
                       "the GraphQL specification.")
                   .SetType(type)
                   .SetField(field)
                   .SetSpecifiedBy(isInterface)
                   .Build());
        }
Exemple #12
0
        /// <summary>
        /// Initializes a new instance of <see cref="EntityModel" />.
        /// </summary>
        /// <param name="type">
        /// The entity type.
        /// </param>
        public EntityModel(IComplexOutputType type)
        {
            Name       = type.Name;
            Type       = type;
            Definition = type.GetEntityDefinition();

            var fields = new Dictionary <string, IOutputField>();

            foreach (var fieldSyntax in Definition.Selections.OfType <FieldNode>())
            {
                fields.Add(fieldSyntax.Name.Value, type.Fields[fieldSyntax.Name.Value]);
            }

            Fields = fields.Values.ToList();
        }
Exemple #13
0
        public static ISchemaError NotTransitivelyImplemented(
            IComplexOutputType type,
            IComplexOutputType implementedType)
        {
            bool isInterface = type.Kind == TypeKind.Interface;

            return(SchemaErrorBuilder.New()
                   .SetMessage(
                       "The {0} type must also declare all interfaces " +
                       "declared by implemented interfaces.",
                       isInterface ? _interface : _object)
                   .SetType(type)
                   .SetImplementedType(implementedType)
                   .SetSpecifiedBy(isInterface)
                   .Build());
        }
Exemple #14
0
 public static IError FieldDoesNotExist(
     this IDocumentValidatorContext context,
     FieldNode node,
     IComplexOutputType outputType)
 {
     return(ErrorBuilder.New()
            .SetMessage(
                Resources.ErrorHelper_FieldDoesNotExist,
                node.Name.Value, outputType.Name)
            .AddLocation(node)
            .SetPath(context.CreateErrorPath())
            .SetExtension("type", outputType.Name)
            .SetExtension("field", node.Name.Value)
            .SetExtension("responseName", (node.Alias ?? node.Name).Value)
            .SpecifiedBy("sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types")
            .Build());
 }
Exemple #15
0
        public static ISchemaError FieldNotImplemented(
            IComplexOutputType type,
            IOutputField implementedField)
        {
            bool isInterface = type.Kind == TypeKind.Interface;

            return(SchemaErrorBuilder.New()
                   .SetMessage(
                       "The field `{0}` must be implement by {1} type `{2}`.",
                       implementedField.Name,
                       isInterface ? _interface : _object,
                       type.Print())
                   .SetType(type)
                   .SetImplementedField(implementedField)
                   .SetSpecifiedBy(isInterface)
                   .Build());
        }
Exemple #16
0
 public static IError NoSelectionOnCompositeField(
     this IDocumentValidatorContext context,
     FieldNode node,
     IComplexOutputType declaringType,
     IType fieldType)
 {
     return(ErrorBuilder.New()
            .SetMessage(
                Resources.ErrorHelper_NoSelectionOnCompositeField,
                node.Name.Value)
            .AddLocation(node)
            .SetPath(context.CreateErrorPath())
            .SetExtension("declaringType", declaringType.Name)
            .SetExtension("field", node.Name.Value)
            .SetExtension("type", fieldType.Print())
            .SetExtension("responseName", (node.Alias ?? node.Name).Value)
            .SpecifiedBy("sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types")
            .Build());
 }
Exemple #17
0
 public static IError LeafFieldsCannotHaveSelections(
     this IDocumentValidatorContext context,
     FieldNode node,
     IComplexOutputType declaringType,
     IType fieldType)
 {
     return(ErrorBuilder.New()
            .SetMessage(
                Resources.ErrorHelper_LeafFieldsCannotHaveSelections,
                node.Name.Value, fieldType.IsScalarType() ? "a scalar" : "an enum")
            .AddLocation(node)
            .SetPath(context.CreateErrorPath())
            .SetExtension("declaringType", declaringType.Name)
            .SetExtension("field", node.Name.Value)
            .SetExtension("type", fieldType.Print())
            .SetExtension("responseName", (node.Alias ?? node.Name).Value)
            .SpecifiedBy("sec-Leaf-Field-Selections")
            .Build());
 }
Exemple #18
0
        public static ISchemaError InvalidFieldType(
            IComplexOutputType type,
            IOutputField field,
            IOutputField implementedField)
        {
            bool isInterface = type.Kind == TypeKind.Interface;

            return(SchemaErrorBuilder.New()
                   .SetMessage(
                       "Field `{0}` must return a type which is equal to " +
                       "or a sub‐type of (covariant) the return type `{1}` " +
                       "of the interface field.",
                       field.Name,
                       implementedField.Type.Print())
                   .SetType(type)
                   .SetField(field)
                   .SetImplementedField(implementedField)
                   .SetSpecifiedBy(isInterface)
                   .Build());
        }
Exemple #19
0
 private static ISchemaErrorBuilder SetType(
     this ISchemaErrorBuilder errorBuilder,
     IComplexOutputType type) =>
 errorBuilder
 .AddSyntaxNode(type.SyntaxNode)
 .SetTypeSystemObject((TypeSystemObjectBase)type);
Exemple #20
0
 private static ISchemaErrorBuilder SetImplementedType(
     this ISchemaErrorBuilder errorBuilder,
     IComplexOutputType type) =>
 errorBuilder
 .AddSyntaxNode(type.SyntaxNode)
 .SetExtension("implementedType", type);
Exemple #21
0
 public TypeInfo(IComplexOutputType type, IDictionary <string, object?> contextData)
 {
     Type        = type;
     ContextData = contextData;
 }