Exemplo n.º 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>
        /// Inspects the root and ensures that any intermediate, virtual fields
        /// are accounted for and returns a reference to the immediate parent this action should be added to.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <returns>IGraphField.</returns>
        private IObjectGraphType AddOrRetrieveControllerRoutePath(IGraphTypeFieldTemplate action)
        {
            var pathSegments = action.Route.GenerateParentPathSegments();

            // loop through all parent path parts of this action
            // creating virtual fields as necessary or using existing ones and adding on to them
            IObjectGraphType parentType = this.Schema.OperationTypes[action.Route.RootCollection];

            for (var i = 0; i < pathSegments.Count; i++)
            {
                var segment       = pathSegments[i];
                var formattedName = _formatter.FormatFieldName(segment.Name);
                if (parentType.Fields.ContainsKey(formattedName))
                {
                    var field     = parentType[formattedName];
                    var foundType = Schema.KnownTypes.FindGraphType(field.TypeExpression.TypeName);

                    var ogt = foundType as IObjectGraphType;
                    if (ogt != null)
                    {
                        if (ogt.IsVirtual)
                        {
                            parentType = ogt;
                            continue;
                        }

                        throw new GraphTypeDeclarationException(
                                  $"The action '{action.Route}' attempted to nest itself under the {foundType.Kind} graph type '{foundType.Name}', which is returned by " +
                                  $"the route '{field.Route}'.  Actions can only be added to virtual graph types created by their parent controller.");
                    }

                    if (foundType != null)
                    {
                        throw new GraphTypeDeclarationException(
                                  $"The action '{action.Route.Path}' attempted to nest itself under the graph type '{foundType.Name}'. {foundType.Kind} graph types cannot " +
                                  "accept fields.");
                    }
                    else
                    {
                        throw new GraphTypeDeclarationException(
                                  $"The action '{action.Route.Path}' attempted to nest itself under the field '{field.Route}' but no graph type was found " +
                                  "that matches its type.");
                    }
                }

                parentType = this.CreateVirtualFieldOnParent(
                    parentType,
                    formattedName,
                    segment,
                    i == 0 ? action.Parent : null);
            }

            return(parentType);
        }
        /// <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));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Inspects the root and ensures that any intermediate, virtual fields
        /// are accounted for and returns a reference to the immediate parent this action should be added to.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <returns>IGraphField.</returns>
        private IObjectGraphType AddOrRetrieveRoutePath(IGraphTypeFieldTemplate action)
        {
            var pathSegments = action.Route.GenerateParentPathSegments();

            // loop through all parent path parts of this action
            // creating virtual fields as necessary or using existing ones and adding on to them
            IObjectGraphType parentType = this.Schema.OperationTypes[action.Route.RootCollection];

            for (var i = 0; i < pathSegments.Count; i++)
            {
                var segment       = pathSegments[i];
                var formattedName = _formatter.FormatFieldName(segment.Name);
                if (parentType.Fields.ContainsKey(formattedName))
                {
                    var field     = parentType[formattedName];
                    var foundType = Schema.KnownTypes.FindGraphType(field.TypeExpression.TypeName);
                    if (foundType is IObjectGraphType ogt)
                    {
                        parentType = ogt;
                        continue;
                    }

                    throw new GraphTypeDeclarationException(
                              $"The action '{action.Name}' attempted to nest itself under the grpah type '{foundType?.Name}' but the graph type " +
                              "does not exist or does not accept fields.");
                }

                var fieldType = this.CreateVirtualFieldOnParent(
                    parentType,
                    formattedName,
                    segment,
                    i == 0 ? action.Parent : null);
                parentType = fieldType;
            }

            return(parentType);
        }