Пример #1
0
        /// <summary>
        /// Instantiates the graph field according to the data provided.
        /// </summary>
        /// <param name="formatter">The formatter.</param>
        /// <param name="template">The template.</param>
        /// <param name="securityGroups">The security groups.</param>
        /// <returns>MethodGraphField.</returns>
        protected virtual MethodGraphField InstantiateField(
            GraphNameFormatter formatter,
            IGraphTypeFieldTemplate template,
            List <FieldSecurityGroup> securityGroups)
        {
            switch (template.FieldSource)
            {
            case GraphFieldTemplateSource.Method:
            case GraphFieldTemplateSource.Action:
                return(new MethodGraphField(
                           formatter.FormatFieldName(template.Name),
                           template.TypeExpression.CloneTo(formatter.FormatGraphTypeName(template.TypeExpression.TypeName)),
                           template.Route,
                           template.Mode,
                           template.CreateResolver(),
                           securityGroups));

            case GraphFieldTemplateSource.Property:
                return(new PropertyGraphField(
                           formatter.FormatFieldName(template.Name),
                           template.TypeExpression.CloneTo(formatter.FormatGraphTypeName(template.TypeExpression.TypeName)),
                           template.Route,
                           template.DeclaredReturnType,
                           template.DeclaredName,
                           template.Mode,
                           template.CreateResolver(),
                           securityGroups));

            default:
                throw new ArgumentOutOfRangeException($"Template field source of {template.FieldSource.ToString()} is not supported by {this.GetType().FriendlyName()}.");
            }
        }
        /// <summary>
        /// Instantiates the graph field according to the data provided.
        /// </summary>
        /// <param name="formatter">The formatter.</param>
        /// <param name="template">The template.</param>
        /// <param name="securityGroups">The security groups.</param>
        /// <returns>MethodGraphField.</returns>
        protected override MethodGraphField InstantiateField(
            GraphNameFormatter formatter,
            IGraphTypeFieldTemplate template,
            List <FieldSecurityGroup> securityGroups)
        {
            var subTemplate = template as ControllerSubscriptionActionGraphFieldTemplate;

            if (subTemplate != null &&
                subTemplate.FieldSource == GraphFieldTemplateSource.Action &&
                subTemplate.Route.RootCollection == GraphCollection.Subscription)
            {
                return(new SubscriptionMethodGraphField(
                           formatter.FormatFieldName(template.Name),
                           template.TypeExpression.CloneTo(formatter.FormatGraphTypeName(template.TypeExpression.TypeName)),
                           template.Route,
                           template.Mode,
                           template.CreateResolver(),
                           securityGroups,
                           subTemplate.EventName));
            }

            return(base.InstantiateField(formatter, template, securityGroups));
        }
Пример #3
0
        /// <summary>
        /// Creates a single graph field from the provided template using hte rules of this maker and the contained schema.
        /// </summary>
        /// <param name="template">The template to generate a field from.</param>
        /// <returns>IGraphField.</returns>
        public GraphFieldCreationResult CreateField(IGraphTypeFieldTemplate template)
        {
            var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter;
            var result    = new GraphFieldCreationResult();

            // if the owner of this field declared top level objects append them to the
            // field for evaluation
            var securityGroups = new List <FieldSecurityGroup>();

            if (template.Parent?.SecurityPolicies?.Count > 0)
            {
                securityGroups.Add(template.Parent.SecurityPolicies);
            }

            if (template.SecurityPolicies?.Count > 0)
            {
                securityGroups.Add(template.SecurityPolicies);
            }

            MethodGraphField field = null;

            switch (template.FieldSource)
            {
            case GraphFieldTemplateSource.Method:
            case GraphFieldTemplateSource.Action:
                field = new MethodGraphField(
                    formatter.FormatFieldName(template.Name),
                    template.TypeExpression.CloneTo(formatter.FormatGraphTypeName(template.TypeExpression.TypeName)),
                    template.Route,
                    template.Mode,
                    template.CreateResolver(),
                    securityGroups);
                break;

            case GraphFieldTemplateSource.Property:
                field = new PropertyGraphField(
                    formatter.FormatFieldName(template.Name),
                    template.TypeExpression.CloneTo(formatter.FormatGraphTypeName(template.TypeExpression.TypeName)),
                    template.Route,
                    template.DeclaredReturnType,
                    template.DeclaredName,
                    template.Mode,
                    template.CreateResolver(),
                    securityGroups);
                break;
            }

            field.Description       = template.Description;
            field.IsDeprecated      = template.IsDeprecated;
            field.DeprecationReason = template.DeprecationReason;
            field.Complexity        = template.Complexity;
            field.FieldSource       = template.FieldSource;

            if (template.Arguments != null)
            {
                var argumentMaker = new GraphArgumentMaker(_schema);
                foreach (var argTemplate in template.Arguments)
                {
                    var argumentResult = argumentMaker.CreateArgument(argTemplate);
                    field.Arguments.AddArgument(argumentResult.Argument);

                    result.MergeDependents(argumentResult);
                }
            }

            result.AddDependentRange(template.RetrieveRequiredTypes());

            if (template.UnionProxy != null)
            {
                var unionMaker = new UnionGraphTypeMaker(_schema);
                result.AddDependent(unionMaker.CreateGraphType(template.UnionProxy, template.Kind));
            }

            result.Field = field;
            return(result);
        }