DirectiveDefinitionNode

We found 10 examples in language CSharp for this search. You will see 45 fragments of code.

        public DirectiveDefinitionNode WithLocation(Location? location)
        {
            return new DirectiveDefinitionNode
            (
                location,
                Name,
                Description,
                IsRepeatable,
                Arguments,
                Locations
            );
        }

        public DirectiveDefinitionNode WithName(
            NameNode name)
        {
            return new DirectiveDefinitionNode
            (
                Location,
                name,
                Description,
                IsRepeatable,
                Arguments,
                Locations
            );
        }

        public DirectiveDefinitionNode WithDescription(
            StringValueNode? description)
        {
            return new DirectiveDefinitionNode
            (
                Location,
                Name,
                description,
                IsRepeatable,
                Arguments,
                Locations
            );
        }

        public DirectiveDefinitionNode AsRepeatable()
        {
            return new DirectiveDefinitionNode
            (
                Location,
                Name,
                Description,
                true,
                Arguments,
                Locations
            );
        }

        public DirectiveDefinitionNode AsUnique()
        {
            return new DirectiveDefinitionNode
            (
                Location,
                Name,
                Description,
                false,
                Arguments,
                Locations
            );
        }

        public DirectiveDefinitionNode WithArguments(
            IReadOnlyList<InputValueDefinitionNode> arguments)
        {
            return new DirectiveDefinitionNode
            (
                Location,
                Name,
                Description,
                IsRepeatable,
                arguments,
                Locations
            );
        }


    public DirectiveDefinitionNode WithLocation(Location? location)
    {
        return new DirectiveDefinitionNode
        (
            location,
            Name,
            Description,
            IsRepeatable,
            Arguments,
            Locations
        );
    }

    public DirectiveDefinitionNode WithName(
        NameNode name)
    {
        return new DirectiveDefinitionNode
        (
            Location,
            name,
            Description,
            IsRepeatable,
            Arguments,
            Locations
        );
    }

    public DirectiveDefinitionNode WithDescription(
        StringValueNode? description)
    {
        return new DirectiveDefinitionNode
        (
            Location,
            Name,
            description,
            IsRepeatable,
            Arguments,
            Locations
        );
    }

    public DirectiveDefinitionNode AsRepeatable()
    {
        return new DirectiveDefinitionNode
        (
            Location,
            Name,
            Description,
            true,
            Arguments,
            Locations
        );
    }

    public DirectiveDefinitionNode AsUnique()
    {
        return new DirectiveDefinitionNode
        (
            Location,
            Name,
            Description,
            false,
            Arguments,
            Locations
        );
    }

    public DirectiveDefinitionNode WithArguments(
        IReadOnlyList<InputValueDefinitionNode> arguments)
    {
        return new DirectiveDefinitionNode
        (
            Location,
            Name,
            Description,
            IsRepeatable,
            arguments,
            Locations
        );
    }

        /// <summary>
        /// Associates the specified <paramref name="directiveDefinitionNode"/>
        /// with the <see cref="DirectiveType"/>.
        /// </summary>
        /// <param name="directiveDefinitionNode">
        /// The <see cref="DirectiveDefinitionNode"/> of a parsed schema.
        /// </param>
        IDirectiveTypeDescriptor SyntaxNode(
            DirectiveDefinitionNode directiveDefinitionNode);

        // <summary>
        /// Associates the specified <paramref name="directiveDefinitionNode"/>
        /// with the <see cref="DirectiveType"/>.
        /// </summary>
        /// <param name="syntaxNode">
        /// The <see cref="DirectiveDefinitionNode"/> of a parsed schema.
        /// </param>

        IDirectiveTypeDescriptor<T> SyntaxNode(
            DirectiveDefinitionNode directiveDefinitionNode);

        /// <summary>
        /// Defines the name of the <see cref="DirectiveType"/>.
        /// </summary>
        /// <param name="value">The directive type name.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> is <c>null</c> or
        /// <see cref="string.Empty"/>.
        /// </exception>
        IDirectiveTypeDescriptor<T> Name(NameString value);

        /// <summary>
        /// Adds explanatory text to the <see cref="DirectiveType"/>
        /// that can be accessd via introspection.
        /// </summary>
        /// <param name="value">The object type description.</param>
        IDirectiveTypeDescriptor<T> Description(string value);

        /// <summary>
        /// Defines the argument binding behavior.
        ///
        /// The default binding behavior is set to
        /// <see cref="BindingBehavior.Implicit"/>.
        /// </summary>
        /// <param name="behavior">
        /// The binding behavior.
        ///
        /// Implicit:
        /// The directive type descriptor will try to infer the directive type
        /// arguments from the specified .net directive type representation
        /// (<typeparamref name="T"/>).
        ///
        /// Explicit:
        /// All arguments have to specified explicitly via
        /// <see cref="IDirectiveTypeDescriptor{T}.Field{TValue}(Expression{Func{T, TValue}})"/>.
        /// </param>
        IDirectiveTypeDescriptor<T> BindArguments(
            BindingBehavior behavior);

        /// <summary>
        /// Defines that all arguments have to be specified explicitly.
        /// </summary>
        IDirectiveTypeDescriptor<T> BindArgumentsExplicitly();

        /// <summary>
        /// The directive type will add arguments for all compatible properties.
        /// </summary>
        IDirectiveTypeDescriptor<T> BindArgumentsImplicitly();


        public DocumentNode AddExtensions(
            DocumentNode schema,
            DocumentNode extensions)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (extensions == null)
            {
                throw new ArgumentNullException(nameof(extensions));
            }

            var newTypes = extensions.Definitions
                .OfType<ITypeDefinitionNode>().ToList();
            var newDirectives = extensions.Definitions
                .OfType<DirectiveDefinitionNode>().ToList();

            DocumentNode current = schema;

            if (newTypes.Count > 0 || newDirectives.Count > 0)
            {
                current = RemoveDirectives(current,
                    newDirectives.Select(t => t.Name.Value));
                current = RemoveTypes(current,
                    newTypes.Select(t => t.Name.Value));

                var definitions = schema.Definitions.ToList();
                definitions.AddRange(newTypes);
                definitions.AddRange(newDirectives);
                current = current.WithDefinitions(definitions);
            }

            var context = new MergeContext(current, extensions);
            current = RewriteDocument(current, context);
            return current;
        }

        private static DocumentNode RemoveDirectives(
            DocumentNode document,
            IEnumerable<string> directiveNames)
        {
            return RemoveDefinitions(
                document,
                d => d.Definitions.OfType<DirectiveDefinitionNode>()
                    .ToDictionary(t => t.Name.Value, t => (IDefinitionNode)t),
                directiveNames);
        }

        private static DocumentNode RemoveTypes(
            DocumentNode document,
            IEnumerable<string> directiveNames)
        {
            return RemoveDefinitions(
                document,
                d => d.Definitions.OfType<ITypeDefinitionNode>()
                    .ToDictionary(t => t.Name.Value, t => (IDefinitionNode)t),
                directiveNames);
        }

        private static DocumentNode RemoveDefinitions(
            DocumentNode document,
            Func<DocumentNode, Dictionary<string, IDefinitionNode>> toDict,
            IEnumerable<string> names)
        {
            List<IDefinitionNode> definitions = document.Definitions.ToList();
            Dictionary<string, IDefinitionNode> directives = toDict(document);

            foreach (string name in names)
            {
                if (directives.TryGetValue(name, out IDefinitionNode directive))
                {
                    definitions.Remove(directive);
                }
            }

            return document.WithDefinitions(definitions);
        }

        protected override UnionTypeDefinitionNode RewriteUnionTypeDefinition(
            UnionTypeDefinitionNode node,
            MergeContext context)
        {
            UnionTypeDefinitionNode current = node;

            if (context.Extensions.TryGetValue(
                current.Name.Value,
                out ITypeExtensionNode extension))
            {
                if (extension is UnionTypeExtensionNode unionTypeExtension)
                {
                    current = AddTypes(current, unionTypeExtension);
                    current = AddDirectives(current, unionTypeExtension,
                        d => current.WithDirectives(d), context);
                }
                else
                {
                    throw new SchemaMergeException(
                        current,
                        extension,
                        string.Format(
                            CultureInfo.InvariantCulture,
                            StitchingResources
                                .AddSchemaExtensionRewriter_TypeMismatch,
                            node.Name.Value,
                            node.Kind,
                            extension.Kind));
                }
            }

            return base.RewriteUnionTypeDefinition(current, context);
        }

        private static UnionTypeDefinitionNode AddTypes(
            UnionTypeDefinitionNode typeDefinition,
            UnionTypeExtensionNode typeExtension)
        {
            if (typeExtension.Types.Count == 0)
            {
                return typeDefinition;
            }

            var types =
                new OrderedDictionary<string, NamedTypeNode>();

            foreach (NamedTypeNode type in typeDefinition.Types)
            {
                types[type.Name.Value] = type;
            }

            foreach (NamedTypeNode type in typeExtension.Types)
            {
                types[type.Name.Value] = type;
            }

            return typeDefinition.WithTypes(types.Values.ToList());
        }


        public void AddType(ITypeDefinitionNode type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (_types.ContainsKey(type.Name.Value))
            {
                throw new ArgumentException(
                    "A type with that name was already added.");
            }

            _types.Add(type.Name.Value, type);
        }

        public void AddDirective(DirectiveDefinitionNode directive)
        {
            if (directive == null)
            {
                throw new ArgumentNullException(nameof(directive));
            }

            if (_dirs.ContainsKey(directive.Name.Value))
            {
                throw new ArgumentException(
                    "A type with that name was already added.");
            }

            _dirs.Add(directive.Name.Value, directive);
        }

        public bool ContainsType(NameString typeName)
        {
            typeName.EnsureNotEmpty(nameof(typeName));
            return _types.ContainsKey(typeName);
        }

        public bool ContainsDirective(NameString directiveName)
        {
            directiveName.EnsureNotEmpty(nameof(directiveName));
            return _dirs.ContainsKey(directiveName);
        }

        public DocumentNode CreateSchema()
        {
            var definitions = new List<IDefinitionNode>();
            definitions.AddRange(_types.Values);
            definitions.AddRange(_dirs.Values);
            return new DocumentNode(definitions);
        }

    /// <summary>
    /// Associates the specified <paramref name="directiveDefinitionNode"/>
    /// with the <see cref="DirectiveType"/>.
    /// </summary>
    /// <param name="directiveDefinitionNode">
    /// The <see cref="DirectiveDefinitionNode"/> of a parsed schema.
    /// </param>
    IDirectiveTypeDescriptor SyntaxNode(
        DirectiveDefinitionNode directiveDefinitionNode);

    /// <summary>
    /// Defines the name of the <see cref="DirectiveType"/>.
    /// </summary>
    /// <param name="value">The directive type name.</param>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="value"/> is <c>null</c> or
    /// <see cref="string.Empty"/>.
    /// </exception>
    IDirectiveTypeDescriptor Name(NameString value);

    /// <summary>
    /// Adds explanatory text to the <see cref="DirectiveType"/>
    /// that can be accessed via introspection.
    /// </summary>
    /// <param name="value">The object type description.</param>
    IDirectiveTypeDescriptor Description(string value);

    /// <summary>
    /// Specifies a directive argument.
    /// </summary>
    /// <param name="name">The name of the argument.</param>
    IDirectiveArgumentDescriptor Argument(NameString name);

    /// <summary>
    /// Specifies in which location the directive belongs in.
    /// </summary>
    /// <param name="value">The directive location.</param>
    IDirectiveTypeDescriptor Location(DirectiveLocation value);

    [Obsolete("Replace Middleware with `Use`.")]
    IDirectiveTypeDescriptor Middleware(
        DirectiveMiddleware middleware);

    [Fact]
    public void ParseUniqueDirective()
    {
        // arrange
        var text = "directive @skip(if: Boolean!) " +
            "on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT";
        var parser = new Utf8GraphQLParser(Encoding.UTF8.GetBytes(text));

        // assert
        DocumentNode document = parser.Parse();

        // assert
        DirectiveDefinitionNode directiveDefinition = document.Definitions
            .OfType<DirectiveDefinitionNode>().FirstOrDefault();
        Assert.NotNull(directiveDefinition);
        Assert.False(directiveDefinition.IsRepeatable);
    }

    [Fact]
    public void ParseRepeatableDirective()
    {
        // arrange
        var text = "directive @skip(if: Boolean!) repeatable " +
            "on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT";
        var parser = new Utf8GraphQLParser(Encoding.UTF8.GetBytes(text));

        // assert
        DocumentNode document = parser.Parse();

        // assert
        DirectiveDefinitionNode directiveDefinition = document.Definitions
            .OfType<DirectiveDefinitionNode>().FirstOrDefault();
        Assert.NotNull(directiveDefinition);
        Assert.True(directiveDefinition.IsRepeatable);
    }

    [Fact]
    public void DirectiveOrderIsSignificant()
    {
        // arrange
        var text = "type Query { field: String @a @b @c }";
        var parser = new Utf8GraphQLParser(Encoding.UTF8.GetBytes(text));

        // assert
        DocumentNode document = parser.Parse();

        // assert
        ObjectTypeDefinitionNode type = document.Definitions
            .OfType<ObjectTypeDefinitionNode>().FirstOrDefault();
        Assert.Collection(type.Fields.Single().Directives,
            t => Assert.Equal("a", t.Name.Value),
            t => Assert.Equal("b", t.Name.Value),
            t => Assert.Equal("c", t.Name.Value));
    }

    [Fact]
    public void ParseQueryDirective()
    {
        // arrange
        var text = @"
                query ($var: Boolean) @onQuery {
                    field
                }
            ";

        // act
        DocumentNode document = Utf8GraphQLParser.Parse(
            Encoding.UTF8.GetBytes(text));

        // assert
        document.MatchSnapshot();
    }


        public void Merge(
            ISchemaMergeContext context,
            IReadOnlyList<IDirectiveTypeInfo> types)
        {
            var notMerged = types.ToList();

            while (notMerged.Count > 0)
            {
                MergeNextType(context, notMerged);
            }
        }

        private void MergeNextType(
           ISchemaMergeContext context,
           List<IDirectiveTypeInfo> notMerged)
        {
            IDirectiveTypeInfo left = notMerged[0];

            var readyToMerge = new List<IDirectiveTypeInfo>();
            readyToMerge.Add(left);

            for (int i = 1; i < notMerged.Count; i++)
            {
                if (CanBeMerged(left.Definition, notMerged[i].Definition))
                {
                    readyToMerge.Add(notMerged[i]);
                }
            }

            NameString name = readyToMerge[0].Definition.Name.Value;

            if (context.ContainsDirective(name))
            {
                throw new InvalidOperationException($"Unable to merge {name}, directive with this name already exists.");
            }

            MergeTypes(context, readyToMerge, name);
            notMerged.RemoveAll(readyToMerge.Contains);
        }

        protected void MergeTypes(
            ISchemaMergeContext context,
            IReadOnlyList<IDirectiveTypeInfo> types,
            NameString newTypeName)
        {
            var definitions = types
                .Select(t => t.Definition)
                .ToList();

            DirectiveDefinitionNode definition =
                definitions[0].Rename(
                    newTypeName,
                    types.Select(t => t.Schema.Name));

            context.AddDirective(definition);
        }

        private static bool CanBeMerged(DirectiveDefinitionNode left, DirectiveDefinitionNode right)
        {
            if (!left.Name.Value.Equals(right.Name.Value, StringComparison.Ordinal))
            {
                return false;
            }

            if (left.Locations.Count != right.Locations.Count)
            {
                return false;
            }

            var leftLocations = left.Locations.Select(l => l.Value).OrderBy(l => l).ToList();
            var rightLocations = right.Locations.Select(l => l.Value).OrderBy(l => l).ToList();

            if (!leftLocations.SequenceEqual(rightLocations))
            {
                return false;
            }

            if (left.IsRepeatable != right.IsRepeatable)
            {
                return false;
            }

            if (left.Arguments.Count != right.Arguments.Count)
            {
                return false;
            }

            return ComplexTypeMergeHelpers.HasSameArguments(left.Arguments, right.Arguments);
        }

using HotChocolate.Language;

namespace HotChocolate.Stitching.Merge
{
    public class DirectiveTypeInfo : IDirectiveTypeInfo
    {
        public DirectiveTypeInfo(
            DirectiveDefinitionNode definition,
            ISchemaInfo schema)
        {
            Definition = definition;
            Schema = schema;
        }

        public DirectiveDefinitionNode Definition { get; }

        public ISchemaInfo Schema { get; }
    }
}

HotChocolate.Language.DirectiveDefinitionNode : ITypeSystemDefinitionNode, IDefinitionNode, ISyntaxNode

Constructors :

public DirectiveDefinitionNode(Location location = , NameNode name = , StringValueNode description = , Boolean isRepeatable = , IReadOnlyList<InputValueDefinitionNode> arguments = , IReadOnlyList<NameNode> locations = )

Methods :

public NodeKind get_Kind()
public Location get_Location()
public NameNode get_Name()
public StringValueNode get_Description()
public Boolean get_IsRepeatable()
public Boolean get_IsUnique()
public IReadOnlyList<InputValueDefinitionNode> get_Arguments()
public IReadOnlyList<NameNode> get_Locations()
public DirectiveDefinitionNode WithLocation(Location location = )
public DirectiveDefinitionNode WithName(NameNode name = )
public DirectiveDefinitionNode WithDescription(StringValueNode description = )
public DirectiveDefinitionNode AsRepeatable()
public DirectiveDefinitionNode AsUnique()
public DirectiveDefinitionNode WithArguments(IReadOnlyList<InputValueDefinitionNode> arguments = )
public DirectiveDefinitionNode WithLocations(IReadOnlyList<NameNode> locations = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()