Пример #1
0
 private __DirectiveLocation ToInterfaceLocation(DirectiveLocation arg)
 {
     return(arg switch
     {
         DirectiveLocation.Query => __DirectiveLocation.Query,
         DirectiveLocation.Mutation => __DirectiveLocation.Mutation,
         DirectiveLocation.Subscription => __DirectiveLocation.Subscription,
         DirectiveLocation.Field => __DirectiveLocation.Field,
         DirectiveLocation.FragmentDefinition => __DirectiveLocation.FragmentDefinition,
         DirectiveLocation.FragmentSpread => __DirectiveLocation.FragmentSpread,
         DirectiveLocation.InlineFragment => __DirectiveLocation.InlineFragment,
         DirectiveLocation.VariableDefinition => __DirectiveLocation.VariableDefinition,
         DirectiveLocation.Schema => __DirectiveLocation.Schema,
         DirectiveLocation.Scalar => __DirectiveLocation.Scalar,
         DirectiveLocation.Object => __DirectiveLocation.Object,
         DirectiveLocation.FieldDefinition => __DirectiveLocation.FieldDefinition,
         DirectiveLocation.ArgumentDefinition => __DirectiveLocation.ArgumentDefinition,
         DirectiveLocation.Interface => __DirectiveLocation.Interface,
         DirectiveLocation.Union => __DirectiveLocation.Union,
         DirectiveLocation.Enum => __DirectiveLocation.Enum,
         DirectiveLocation.EnumValue => __DirectiveLocation.EnumValue,
         DirectiveLocation.InputObject => __DirectiveLocation.InputObject,
         DirectiveLocation.InputFieldDefinition => __DirectiveLocation.InputFieldDefinition,
         _ => throw new NotSupportedException()
     });
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QueryDirective" /> class.
 /// </summary>
 /// <param name="node">The node denoting the directive.</param>
 /// <param name="directive">The directive pulled from the target schema.</param>
 /// <param name="location">The location in the source document where this directive instance was seen.</param>
 public QueryDirective(DirectiveNode node, IDirectiveGraphType directive, DirectiveLocation location)
 {
     this.Node      = Validation.ThrowIfNullOrReturn(node, nameof(node));
     this.Directive = Validation.ThrowIfNullOrReturn(directive, nameof(directive));
     this.Location  = location;
     this.Arguments = new QueryInputArgumentCollection();
 }
Пример #3
0
        private RequestDirective BuildDirective(Node dirNode, DirectiveLocation atLocation, RequestObjectBase parent)
        {
            var dirName = dirNode.ChildNodes[0].ChildNodes[1].GetText(); // child0 is dirName-> @+name

            _path.Push(dirName);
            try {
                var dirDef = LookupDirective(dirName, dirNode);
                if (dirDef == null)
                {
                    return(null); // error is already logged
                }
                if (!dirDef.Registration.Locations.IsSet(atLocation))
                {
                    AddError($"Directive @{dirName} may not be placed at this location ({atLocation}). Valid locations: [{dirDef.Registration.Locations}].", dirNode);
                    return(null);
                }
                var dir = new RequestDirective()
                {
                    Def = dirDef, Location = atLocation, Name = dirName, SourceLocation = dirNode.GetLocation(), Parent = parent
                };
                var argListNode = dirNode.FindChild(TermNames.ArgListOpt);
                dir.Args = BuildArguments(argListNode.ChildNodes, dir);
                return(dir);
            } finally {
                _path.Pop();
            }
        }
Пример #4
0
        private IList <ModelDirective> BuildDirectivesFromAttributes(ICustomAttributeProvider clrObjectInfo,
                                                                     DirectiveLocation location, GraphQLModelObject owner)
        {
            var attrList = clrObjectInfo.GetCustomAttributes(inherit: true);

            if (attrList.Length == 0)
            {
                return(ModelDirective.EmptyList);
            }

            var dirList = new List <ModelDirective>();

            foreach (var attr in attrList)
            {
                if (!(attr is BaseDirectiveAttribute dirAttr))
                {
                    continue;
                }
                var dirAttrType = dirAttr.GetType();
                var dirDef      = _model.Directives.Values.FirstOrDefault(def => def.DirInfo.AttributeType == dirAttrType);
                if (dirDef == null)
                {
                    AddError($"{clrObjectInfo}: directive attribute {dirAttrType} not registered.");
                    continue;
                }
                var dir = new ModelDirective()
                {
                    Def = dirDef, ModelAttribute = dirAttr, Location = location
                };
                dirList.Add(dir);
            }
            return(dirList);
        } //method
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphDirectiveExecutionContext"/> class.
 /// </summary>
 /// <param name="seenAtLocation">The seen at location.</param>
 /// <param name="graphType">Type of the graph.</param>
 /// <param name="origin">The origin.</param>
 public GraphDirectiveExecutionContext(DirectiveLocation seenAtLocation, IDirectiveGraphType graphType, SourceOrigin origin)
 {
     this.Location  = seenAtLocation;
     this.Directive = graphType;
     this.Origin    = origin;
     this.Arguments = new InputArgumentCollection();
 }
Пример #6
0
        protected FieldBase(
            FieldDescriptionBase description,
            DirectiveLocation location)
        {
            if (description == null)
            {
                throw new ArgumentNullException(nameof(description));
            }

            if (string.IsNullOrEmpty(description.Name))
            {
                throw new ArgumentException(
                          "The name of a field mustn't be null or empty.",
                          nameof(description));
            }

            Name          = description.Name;
            Description   = description.Description;
            TypeReference = description.TypeReference;

            var directives = new DirectiveCollection(
                this,
                location,
                description.Directives);

            RegisterForInitialization(directives);

            Directives = directives;
        }
Пример #7
0
 public RuntimeDirective(ModelDirective modelDir, int index)
 {
     Source          = modelDir;
     Index           = index;
     Def             = modelDir.Def;
     Location        = modelDir.Location;
     StaticArgValues = modelDir.ArgValues;
 }
Пример #8
0
 private InputField(
     ArgumentDescription argumentDescription,
     DirectiveLocation location)
     : base(argumentDescription, location)
 {
     _nativeDefaultValue = argumentDescription.NativeDefaultValue;
     SyntaxNode          = argumentDescription.SyntaxNode;
     DefaultValue        = argumentDescription.DefaultValue;
 }
Пример #9
0
        public object[] StaticArgValues; // dirs that do not use variables, or model directives

        public RuntimeDirective(RequestDirective reqDir, int index)
        {
            Source          = reqDir;
            Index           = index;
            Owner           = reqDir.Parent;
            Def             = reqDir.Def;
            Location        = reqDir.Location;
            MappedArgs      = reqDir.MappedArgs;
            StaticArgValues = reqDir.MappedArgs.TryEvaluateStaticArgValues();
        }
 internal DirectiveCollection(
     TypeSystemBase source,
     DirectiveLocation location,
     IReadOnlyCollection <DirectiveDescription> directiveDescriptions)
 {
     _source       = source;
     _location     = location;
     _descriptions = directiveDescriptions
                     ?? throw new ArgumentNullException(
                               nameof(directiveDescriptions));
 }
 public static Language.DirectiveLocation MapDirectiveLocation(this DirectiveLocation location)
 {
     if (!_locs.TryGetValue(location, out Language.DirectiveLocation l))
     {
         throw new NotSupportedException(string.Format(
                                             CultureInfo.InvariantCulture,
                                             TypeResources.DirectiveTypeFactory_LocationNotSupported,
                                             location));
     }
     return(l);
 }
Пример #12
0
        protected void Location(DirectiveLocation location)
        {
            var values = Enum.GetValues(typeof(DirectiveLocation));

            foreach (DirectiveLocation value in values)
            {
                if (location.HasFlag(value))
                {
                    DirectiveDescription.Locations.Add(value);
                }
            }
        }
        public static void RegisterDirective(this GraphQLModule module, string name, string signatureMethodName,
                                             DirectiveLocation locations, string description = null, IDirectiveHandler handler = null, bool listInSchema = true)
        {
            var method = module.GetType().GetMethod(signatureMethodName,
                                                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

            if (method == null)
            {
                throw new ArgumentException($"RegisterDirective, name={name}: method {signatureMethodName} not found in module {module.Name}");
            }
            RegisterDirective(module, name, method, locations, description, handler, listInSchema);
        }
Пример #14
0
        internal DirectiveCollection(
            object source,
            IEnumerable <DirectiveDefinition> directiveDefinitions)
        {
            if (directiveDefinitions is null)
            {
                throw new ArgumentNullException(nameof(directiveDefinitions));
            }

            _source      = source ?? throw new ArgumentNullException(nameof(source));
            _definitions = directiveDefinitions.ToList();
            _location    = DirectiveHelper.InferDirectiveLocation(source);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DirectiveGraphType" /> class.
 /// </summary>
 /// <param name="name">The name of the directive as it appears in the schema.</param>
 /// <param name="locations">The locations where this directive is valid.</param>
 /// <param name="directiveType">The concrete type of the directive.</param>
 /// <param name="resolver">The resolver used to process this instance.</param>
 public DirectiveGraphType(
     string name,
     DirectiveLocation locations,
     Type directiveType,
     IGraphDirectiveResolver resolver = null)
 {
     this.Name      = Validation.ThrowIfNullOrReturn(name, nameof(name));
     this.Arguments = new GraphFieldArgumentCollection();
     this.Locations = locations;
     this.Resolver  = resolver;
     this.Publish   = true;
     _directiveType = Validation.ThrowIfNullOrReturn(directiveType, nameof(directiveType));
 }
Пример #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphDirectiveRequest" /> class.
 /// </summary>
 /// <param name="targetDirective">The target directive.</param>
 /// <param name="location">The location.</param>
 /// <param name="origin">The origin.</param>
 /// <param name="requestMetaData">The request meta data.</param>
 public GraphDirectiveRequest(
     IDirectiveGraphType targetDirective,
     DirectiveLocation location,
     SourceOrigin origin,
     MetaDataCollection requestMetaData = null)
 {
     this.Id                = Guid.NewGuid().ToString("N");
     this.Directive         = Validation.ThrowIfNullOrReturn(targetDirective, nameof(targetDirective));
     this.LifeCycle         = DirectiveLifeCycle.BeforeResolution;
     this.DirectiveLocation = location;
     this.Origin            = origin ?? SourceOrigin.None;
     this.Items             = requestMetaData ?? new MetaDataCollection();
 }
Пример #17
0
        public IDirectiveTypeDescriptor Location(DirectiveLocation value)
        {
            Array values = Enum.GetValues(typeof(DirectiveLocation));

            foreach (DirectiveLocation item in values)
            {
                if (value.HasFlag(item))
                {
                    Definition.Locations.Add(item);
                }
            }
            return(this);
        }
        public static void RegisterDirective(this GraphQLModule module, string name, MethodInfo signature,
                                             DirectiveLocation locations, string description = null, IDirectiveHandler handler = null, bool listInSchema = true)
        {
            if (signature == null)
            {
                throw new ArgumentException("RegisterDirective method: signature parameter may not be null.");
            }
            var reg = new DirectiveRegistration()
            {
                Name         = name, Signature = signature, Locations = locations, Description = description,
                ListInSchema = listInSchema, Handler = handler
            };

            module.RegisteredDirectives.Add(reg);
        }
Пример #19
0
        public DirectiveCollection(
            object source,
            IEnumerable <DirectiveDefinition> directiveDefinitions)
        {
            if (directiveDefinitions is null)
            {
                throw new ArgumentNullException(nameof(directiveDefinitions));
            }

            _source      = source ?? throw new ArgumentNullException(nameof(source));
            _definitions = directiveDefinitions.Any()
                ? directiveDefinitions.ToArray()
                : Array.Empty <DirectiveDefinition>();
            _location = DirectiveHelper.InferDirectiveLocation(source);
        }
        public static void RegisterDirective(this GraphQLModule module, string name, Type directiveAttributeType,
                                             DirectiveLocation locations, string description = null, IDirectiveHandler handler = null, bool listInSchema = true)
        {
            if (!typeof(BaseDirectiveAttribute).IsAssignableFrom(directiveAttributeType))
            {
                throw new ArgumentException(
                          $"RegisterDirective method: directive attribute must be subclass of {nameof(directiveAttributeType)}");
            }
            var reg = new DirectiveRegistration()
            {
                Name         = name, AttributeType = directiveAttributeType, Locations = locations, Description = description,
                ListInSchema = listInSchema, Handler = handler
            };

            module.RegisteredDirectives.Add(reg);
        }
Пример #21
0
        private List <RequestDirective> BuildDirectives(Node dirListNode, DirectiveLocation atLocation, RequestObjectBase parent)
        {
            var dirList = new List <RequestDirective>();

            if (dirListNode == null)
            {
                return(dirList);
            }
            foreach (var dirNode in dirListNode.ChildNodes)
            {
                var dir = BuildDirective(dirNode, atLocation, parent);
                if (dir == null)
                {
                    continue;
                }
                dirList.Add(dir);
            }
            return(dirList);
        }
Пример #22
0
        private bool ShouldIncludeNode(
            IEnumerable <GraphQLDirective> directives,
            DirectiveLocation location)
        {
            foreach (var directive in directives)
            {
                var directiveType = this.schemaRepository.GetDirective(directive.Name.Value);
                if (directiveType != null)
                {
                    if (!directiveType.Locations.Any(e => e == location))
                    {
                        continue;
                    }
                    if (!directiveType.PreExecutionIncludeFieldIntoResult(directive, this.schemaRepository))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #23
0
        private void BuildTypesInternalsFromClrType()
        {
            foreach (var td in _model.Types)
            {
                if (td.ClrType == null)
                {
                    continue; // Special types (Query, Mutation etc) do not have Clr types
                }
                DirectiveLocation loc = DirectiveLocation.None;
                switch (td)
                {
                case InterfaceTypeDef intfTypeDef:
                    loc = DirectiveLocation.Interface;
                    BuildObjectTypeFields(intfTypeDef);
                    break;

                case ObjectTypeDef objTypeDef:
                    loc = DirectiveLocation.Object;
                    BuildObjectTypeFields(objTypeDef);
                    break;

                case InputObjectTypeDef inpTypeDef:
                    loc = DirectiveLocation.InputObject;
                    BuildInputObjectFields(inpTypeDef);
                    break;

                case EnumTypeDef etd:
                    loc = DirectiveLocation.Enum;
                    BuildEnumValues(etd);
                    break;

                case UnionTypeDef utd:
                    loc = DirectiveLocation.Union;
                    // we build union types in a separate loop after building other types
                    break;
                } //switch
                td.Directives = BuildDirectivesFromAttributes(td.ClrType, loc, td);
            }     //foreach td
        }
Пример #24
0
 IDirectiveTypeDescriptor IDirectiveTypeDescriptor.Location(
     DirectiveLocation location)
 {
     Location(location);
     return(this);
 }
Пример #25
0
 /// <summary>
 /// Initializes a new instance with the specified properties.
 /// </summary>
 public KnownDirectivesError(ValidationContext context, Directive node, DirectiveLocation candidateLocation)
     : base(context.OriginalQuery, NUMBER, MisplacedDirectiveMessage(node.Name, candidateLocation.ToString()), node)
 {
 }
 /// <summary>
 /// Initializes a new instance with the specified properties.
 /// </summary>
 public DirectivesInAllowedLocationsError(ValidationContext context, Directive node, DirectiveLocation candidateLocation)
     : base(context.Document.OriginalQuery, "5.7.2", $"Directive '{node.Name}' may not be used on {candidateLocation}.", node)
 {
 }
Пример #27
0
 public static bool IsSet(this DirectiveLocation locs, DirectiveLocation loc)
 {
     return((locs & loc) != 0);
 }
 public new IDirectiveTypeDescriptor <T> Location(
     DirectiveLocation value)
 {
     base.Location(value);
     return(this);
 }
Пример #29
0
 public RuntimeDirectiveBase(DirectiveDef dirDef, DirectiveLocation location)
 {
     Def      = dirDef;
     Location = location;
 }
Пример #30
0
 protected void Location(DirectiveLocation location)
 {
     DirectiveDescription.Locations.Add(location);
 }