Example #1
0
        internal static Directive FromDescription(
            DirectiveType directiveType,
            DirectiveDefinition definition,
            object source)
        {
            if (directiveType == null)
            {
                throw new ArgumentNullException(nameof(directiveType));
            }

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

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

            if (definition.CustomDirective is null)
            {
                return(new Directive(directiveType,
                                     definition.ParsedDirective,
                                     source));
            }
            else
            {
                return(new Directive(directiveType,
                                     definition.CustomDirective,
                                     source));
            }
        }
    private void VisitDirectiveDefinition(DirectiveDefinition node)
    {
        EnterNode(node);

        Visit(node.Arguments);

        ExitNode(node);
    }
    public void FromString()
    {
        /* Given */
        /* When */
        DirectiveDefinition original = "directive @a(x: Int, y: Float) on FIELD";

        /* Then */
        Assert.Equal("a", original.Name);
    }
    public void FromBytes()
    {
        /* Given */
        /* When */
        DirectiveDefinition original = Encoding.UTF8.GetBytes("directive @a(x: Int, y: Float) on FIELD")
                                       .AsReadOnlySpan();

        /* Then */
        Assert.Equal("a", original.Name);
    }
    public void WithName()
    {
        /* Given */
        DirectiveDefinition original = @"directive @a on SCHEMA";

        /* When */
        var modified = original
                       .WithName("b");

        /* Then */
        Assert.Equal("a", original.Name);
        Assert.Equal("b", modified.Name);
    }
    public void WithDescription()
    {
        /* Given */
        DirectiveDefinition original = @"directive @a on SCHEMA";

        /* When */
        var modified = original
                       .WithDescription("Description");

        /* Then */
        Assert.Null(original.Description);
        Assert.Equal("Description", modified.Description);
    }
Example #7
0
    public static bool TryGetArgument(
        this DirectiveDefinition definition,
        Name argumentName,
        [NotNullWhen(true)] out InputValueDefinition?argument)
    {
        if (definition.Arguments is null)
        {
            argument = null;
            return(false);
        }

        return(definition.Arguments.TryGet(argumentName, out argument));
    }
Example #8
0
    protected override void EnterDirectiveDefinition(PrinterContext context, DirectiveDefinition directiveDefinition)
    {
        context.Append(' ');
        context.AppendDescription(directiveDefinition.Description);
        context.Append("directive");
        context.Append(' ');
        context.Append($"@{directiveDefinition.Name}");

        if (directiveDefinition.IsRepeatable)
        {
            context.Append(' ');
            context.Append("repeatable");
        }
    }
    public void WithArguments()
    {
        /* Given */
        DirectiveDefinition original = @"directive @a on SCHEMA";

        /* When */
        var modified = original
                       .WithArguments(new InputValueDefinition[]
        {
            "x: Int"
        });

        /* Then */
        Assert.Equal(1, modified.Arguments?.Count);
    }
        private bool TryCompleteDirective(
            ICompletionContext context,
            DirectiveDefinition definition,
            ISet <string> processed,
            out Directive directive)
        {
            DirectiveType directiveType = context.GetDirectiveType(definition.Reference);

            directive = null;

            if (directiveType != null)
            {
                if (!processed.Add(directiveType.Name) && !directiveType.IsRepeatable)
                {
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        TypeResources.DirectiveCollection_DirectiveIsUnique,
                                                        directiveType.Name))
                                        .SetCode(ErrorCodes.Schema.MissingType)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(definition.ParsedDirective)
                                        .SetExtension("Source", _source)
                                        .Build());
                }
                else if (directiveType.Locations.Contains(_location))
                {
                    directive = Directive.FromDescription(directiveType, definition, _source);
                }
                else
                {
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        TypeResources.DirectiveCollection_LocationNotAllowed,
                                                        directiveType.Name,
                                                        _location))
                                        .SetCode(ErrorCodes.Schema.MissingType)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(definition.ParsedDirective)
                                        .SetExtension("Source", _source)
                                        .Build());
                }
            }

            return(directive != null);
        }
    public void WithDirectiveLocations()
    {
        /* Given */
        DirectiveDefinition original = @"directive @a on SCHEMA";

        /* When */
        var modified = original
                       .WithDirectiveLocations(new[]
        {
            "FIELD"
        });

        /* Then */
        var location = Assert.Single(modified.DirectiveLocations);

        Assert.Equal("FIELD", location);
    }
Example #12
0
    protected override void ExitDirectiveDefinition(PrinterContext context, DirectiveDefinition directiveDefinition)
    {
        context.Append(" on");
        context.Append(' ');

        var locations = directiveDefinition.DirectiveLocations;

        for (var i = 0; i < locations.Count; i++)
        {
            var location = locations[i];

            context.Append(location);

            if (i != locations.Count - 1 && location.Length > 1)
            {
                context.Append(" | ");
            }
        }
    }
Example #13
0
        private void CompleteDirective(
            ICompletionContext context,
            DirectiveDefinition definition,
            ISet <string> processed)
        {
            DirectiveType directiveType =
                context.GetDirectiveType(definition.Reference);

            if (directiveType != null)
            {
                if (!processed.Add(directiveType.Name) &&
                    !directiveType.IsRepeatable)
                {
                    // TODO : resources
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(
                                            $"The specified directive `@{directiveType.Name}` " +
                                            "is unique and cannot be added twice.")
                                        .SetCode(TypeErrorCodes.MissingType)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(definition.ParsedDirective)
                                        .Build());
                }
                else if (directiveType.Locations.Contains(_location))
                {
                    _directives.Add(Directive.FromDescription(
                                        directiveType, definition, _source));
                }
                else
                {
                    // TODO : resources
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(
                                            $"The specified directive `@{directiveType.Name}` " +
                                            "is not allowed on the current location " +
                                            $"`{_location}`.")
                                        .SetCode(TypeErrorCodes.MissingType)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(definition.ParsedDirective)
                                        .Build());
                }
            }
        }
Example #14
0
        protected DirectiveType DirectiveType(DirectiveDefinition definition)
        {
            var locations = definition
                            .DirectiveLocations
                            .Select(location =>
                                    (DirectiveLocation)System.Enum.Parse(typeof(DirectiveLocation), location))
                            .ToList();

            var args = Args(definition.Arguments).ToList();

            _builder.DirectiveType(
                definition.Name,
                out var directiveType,
                locations,
                null,
                argsBuilder => args.ForEach(a => argsBuilder.Arg(a.Name, a.Type, a.DefaultValue, a.Description)));

            return(directiveType);
        }
        private bool TryCompleteDirective(
            ITypeCompletionContext context,
            DirectiveDefinition definition,
            ISet <string> processed,
            [NotNullWhen(true)] out Directive?directive)
        {
            if (!context.TryGetDirectiveType(
                    definition.Reference,
                    out DirectiveType? directiveType))
            {
                directive = null;
                return(false);
            }

            if (!processed.Add(directiveType.Name) && !directiveType.IsRepeatable)
            {
                context.ReportError(
                    DirectiveCollection_DirectiveIsUnique(
                        directiveType,
                        context.Type,
                        definition.ParsedDirective,
                        _source));
                directive = null;
                return(false);
            }

            if (directiveType.Locations.Contains(_location))
            {
                directive = Directive.FromDescription(directiveType, definition, _source);
                return(true);
            }

            context.ReportError(
                DirectiveCollection_LocationNotAllowed(
                    directiveType,
                    _location,
                    context.Type,
                    definition.ParsedDirective,
                    _source));
            directive = null;
            return(false);
        }
        private void AddDeprecatedDirective(string reason)
        {
            if (_deprecatedDirective != null)
            {
                Definition.Directives.Remove(_deprecatedDirective);
            }

            _deprecatedDirective = new DirectiveDefinition(
                new DeprecatedDirective(reason),
                Context.TypeInspector.GetTypeRef(typeof(DeprecatedDirective)));
            Definition.Directives.Add(_deprecatedDirective);

            if (!_deprecatedDependencySet)
            {
                Definition.Dependencies.Add(new TypeDependency(
                                                Context.TypeInspector.GetTypeRef(typeof(DeprecatedDirectiveType)),
                                                TypeDependencyKind.Completed));
                _deprecatedDependencySet = true;
            }
        }
Example #17
0
        private string PrintDirectiveDefinition(DirectiveDefinition node)
        {
            var name      = PrintName(node.Name);
            var args      = node.Arguments?.Select(Print);
            var locations = node.DirectiveLocations;

            return(Join(new[]
            {
                "directive @",
                name,
                args.All(e => !e.Contains(Environment.NewLine))
                    ? Wrap("(", Join(args, ", "), ")")
                    : Wrap(
                    $"({Environment.NewLine}",
                    Indent(Join(args, Environment.NewLine)),
                    $"{Environment.NewLine})"),
                " on ",
                Join(locations, " | ")
            }));
        }
        private void AddDeprectedDirective(string reason)
        {
            if (_deprecatedDirective != null)
            {
                Definition.Directives.Remove(_deprecatedDirective);
            }

            _deprecatedDirective = new DirectiveDefinition(
                new DeprecatedDirective(reason));
            Definition.Directives.Add(_deprecatedDirective);

            if (!_deprecatedDependecySet)
            {
                Definition.Dependencies.Add(new TypeDependency(
                                                new ClrTypeReference(
                                                    typeof(DeprecatedDirectiveType),
                                                    TypeContext.None),
                                                TypeDependencyKind.Completed));
                _deprecatedDependecySet = true;
            }
        }
Example #19
0
        public static Directive FromDescription(
            DirectiveType directiveType,
            DirectiveDefinition definition,
            object source)
        {
            if (directiveType is null)
            {
                throw new ArgumentNullException(nameof(directiveType));
            }

            if (definition is null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (definition.CustomDirective is null)
            {
                return(new Directive(
                           directiveType,
                           CompleteArguments(directiveType, definition.ParsedDirective),
                           null,
                           source));
            }
            else
            {
                DirectiveNode directiveNode = ParseValue(
                    directiveType, definition.CustomDirective);

                return(new Directive(
                           directiveType,
                           CompleteArguments(directiveType, directiveNode),
                           definition.CustomDirective,
                           source));
            }
        }
Example #20
0
 public DirectiveContext(DirectiveDefinition directive, GraphQLGenerationOptions options, Document document)
 {
     this.directive = directive;
     this.options   = options;
     this.document  = document;
 }
Example #21
0
 public static DirectiveDefinition WithDescription(this DirectiveDefinition definition,
                                                   in StringValue?description)
 protected virtual void ExitDirectiveDefinition(TContext context, DirectiveDefinition directiveDefinition)
 {
 }