예제 #1
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 = this.InstantiateField(formatter, template, securityGroups);

            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);
        }
        /// <summary>
        /// Inspects the given type and, in accordance with the rules of this maker, will
        /// generate a complete set of necessary graph types required to support it.
        /// </summary>
        /// <param name="concreteType">The concrete type to incorporate into the schema.</param>
        /// <returns>GraphTypeCreationResult.</returns>
        public GraphTypeCreationResult CreateGraphType(Type concreteType)
        {
            var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter;
            var template  = GraphQLProviders.TemplateProvider.ParseType(concreteType) as IGraphDirectiveTemplate;

            if (template == null)
            {
                return(null);
            }

            var result = new GraphTypeCreationResult();

            var directive = new DirectiveGraphType(
                formatter.FormatFieldName(template.Name),
                template.Locations,
                template.ObjectType,
                template.CreateResolver())
            {
                Description = template.Description,
                Publish     = template.Publish,
            };

            // all arguments are required to have the same signature via validation
            // can use any method to fill the arg field list
            var argMaker = new GraphArgumentMaker(_schema);

            foreach (var argTemplate in template.Arguments)
            {
                var argumentResult = argMaker.CreateArgument(argTemplate);
                directive.Arguments.AddArgument(argumentResult.Argument);

                result.MergeDependents(argumentResult);
            }

            result.GraphType    = directive;
            result.ConcreteType = concreteType;
            return(result);
        }
예제 #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);
        }