Ejemplo n.º 1
0
        private void CompleteDirective(
            ITypeInitializationContext context,
            DirectiveDescription description,
            HashSet <string> processed)
        {
            DirectiveReference reference =
                DirectiveReference.FromDescription(description);
            DirectiveType directiveType = context.GetDirectiveType(reference);

            if (directiveType != null)
            {
                if (!processed.Add(directiveType.Name) &&
                    !directiveType.IsRepeatable)
                {
                    context.ReportError(new SchemaError(
                                            $"The specified directive `@{directiveType.Name}` " +
                                            "is unique and cannot be added twice.",
                                            context.Type as INamedType));
                }
                else if (directiveType.Locations.Contains(_location))
                {
                    _directives.Add(Directive.FromDescription(
                                        directiveType, description, _source));
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            $"The specified directive `@{directiveType.Name}` " +
                                            "is not allowed on the current location " +
                                            $"`{_location}`.",
                                            context.Type as INamedType));
                }
            }
        }
        public void MapCustomDirectiveToDifferentType()
        {
            // arrange
            ISchema       schema        = CreateSchema();
            DirectiveType directiveType = schema.GetDirectiveType("Foo");
            var           fooDirective  = new FooDirective
            {
                Bar   = "123",
                Child = new FooChild
                {
                    Bar = "456"
                }
            };

            // act
            var directive = Directive.FromDescription(
                directiveType,
                new DirectiveDefinition(
                    fooDirective,
                    _typeInspector.GetTypeRef(fooDirective.GetType())),
                new object());
            FooChild mappedObject = directive.ToObject <FooChild>();

            // assert
            Assert.Equal("123", mappedObject.Bar);
        }
        public void GetArgumentFromCustomDirective()
        {
            // arrange
            ISchema       schema        = CreateSchema();
            DirectiveType directiveType = schema.GetDirectiveType("Foo");
            var           fooDirective  = new FooDirective
            {
                Bar   = "123",
                Child = new FooChild
                {
                    Bar = "456"
                }
            };

            // act
            var directive = Directive.FromDescription(
                directiveType,
                new DirectiveDefinition(
                    fooDirective,
                    _typeInspector.GetTypeRef(fooDirective.GetType())),
                new object());
            var barValue = directive.GetArgument <string>("bar");

            // assert
            Assert.Equal("123", barValue);
        }
Ejemplo n.º 4
0
        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 ConvertCustomDirectiveToDirectiveNode()
        {
            // arrange
            ISchema       schema        = CreateSchema();
            DirectiveType directiveType = schema.GetDirectiveType("Foo");
            var           fooDirective  = new FooDirective
            {
                Bar   = "123",
                Child = new FooChild
                {
                    Bar = "456"
                }
            };

            // act
            var directive = Directive.FromDescription(
                directiveType,
                new DirectiveDefinition(
                    fooDirective,
                    _typeInspector.GetTypeRef(fooDirective.GetType())),
                new object());
            DirectiveNode directiveNode = directive.ToNode();

            // assert
            Assert.Equal(directiveType.Name, directiveNode.Name.Value);
            Assert.Collection(directiveNode.Arguments,
                              t =>
            {
                Assert.Equal("bar", t.Name.Value);
                Assert.Equal("123", ((StringValueNode)t.Value).Value);
            },
                              t =>
            {
                Assert.Equal("child", t.Name.Value);
                Assert.Collection(((ObjectValueNode)t.Value).Fields,
                                  x =>
                {
                    Assert.Equal("bar", x.Name.Value);
                    Assert.Equal("456",
                                 ((StringValueNode)x.Value).Value);
                });
            });
        }
Ejemplo n.º 6
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());
                }
            }
        }
Ejemplo n.º 7
0
        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 CompleteDirecive(
            ITypeInitializationContext context,
            DirectiveDescription description)
        {
            DirectiveReference reference =
                DirectiveReference.FromDescription(description);
            DirectiveType type = context.GetDirectiveType(reference);

            if (type != null)
            {
                if (type.Locations.Contains(_location))
                {
                    _directives.Add(
                        Directive.FromDescription(type, description, _source));
                }
                else
                {
                    context.ReportError(new SchemaError(
                                            $"The specified directive `{type.Name}` is not " +
                                            $"allowed on the current location `{_location}`.",
                                            context.Type));
                }
            }
        }