Ejemplo n.º 1
0
        public void ParseLiteral_StringValueNode()
        {
            // arrange
            DateType        dateType         = new DateType();
            StringValueNode literal          = new StringValueNode("2018-06-29");
            DateTime        expectedDateTime = new DateTime(2018, 6, 29);

            // act
            DateTime dateTime = (DateTime)dateType
                                .ParseLiteral(literal);

            // assert
            Assert.Equal(expectedDateTime, dateTime);
        }
Ejemplo n.º 2
0
        public void ParseLiteral_StringValueNode()
        {
            // arrange
            var uuidType = new UuidType();
            var expected = Guid.NewGuid();
            var literal  = new StringValueNode(expected.ToString("N"));

            // act
            var actual = (Guid)uuidType
                         .ParseLiteral(literal);

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 3
0
        public void ParseValue_Guid()
        {
            // arrange
            UuidType uuidType             = new UuidType();
            Guid     expected             = Guid.NewGuid();
            string   expectedLiteralValue = expected.ToString("N");

            // act
            StringValueNode stringLiteral =
                (StringValueNode)uuidType.ParseValue(expected);

            // assert
            Assert.Equal(expectedLiteralValue, stringLiteral.Value);
        }
Ejemplo n.º 4
0
        public void ParseLiteral_StringValueNode()
        {
            // arrange
            UuidType        uuidType = new UuidType();
            Guid            expected = Guid.NewGuid();
            StringValueNode literal  = new StringValueNode(expected.ToString());

            // act
            Guid actual = (Guid)uuidType
                          .ParseLiteral(literal);

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
        public void ParseLiteral_StringLiteral_To_ListString()
        {
            // arrange
            var listType = (IInputType) new ListType(new StringType());
            var literal  = new StringValueNode("abc");

            // act
            var serializedList = listType.ParseLiteral(literal);

            // assert
            Assert.Collection(
                Assert.IsType <List <string> >(serializedList),
                t => Assert.Equal("abc", t));
        }
        public void ParseLiteral_StringValueNode()
        {
            // arrange
            var byteArrayType = new ByteArrayType();

            byte[] expected = Encoding.ASCII.GetBytes("value");
            var    literal  = new StringValueNode(Convert.ToBase64String(expected));

            // act
            var actual = (byte[])byteArrayType
                         .ParseLiteral(literal);

            // assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 7
0
        public void ParseValue_Null()
        {
            // arrange
            DateType dateType = new DateType();
            DateTime dateTime =
                new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Utc);
            string expectedLiteralValue = "2018-06-11";

            // act
            StringValueNode stringLiteral =
                (StringValueNode)dateType.ParseValue(dateTime);

            // assert
            Assert.Equal(expectedLiteralValue, stringLiteral.Value);
        }
Ejemplo n.º 8
0
        protected override bool TryParseLiteral(
            StringValueNode literal, out object obj)
        {
            if (DateTime.TryParse(
                    literal.Value,
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.AssumeLocal,
                    out DateTime dateTime))
            {
                obj = dateTime.Date;
                return(true);
            }

            obj = null;
            return(false);
        }
Ejemplo n.º 9
0
        public void ParseLiteral_StringValueNode()
        {
            // arrange
            DateTimeType    dateTimeType     = new DateTimeType();
            StringValueNode literal          = new StringValueNode("2018-06-11T08:46:14+04:00");
            DateTimeOffset  expectedDateTime = new DateTimeOffset(
                new DateTime(2018, 6, 11, 8, 46, 14),
                new TimeSpan(4, 0, 0));

            // act
            DateTimeOffset dateTime = (DateTimeOffset)dateTimeType
                                      .ParseLiteral(literal);

            // assert
            Assert.Equal(expectedDateTime, dateTime);
        }
Ejemplo n.º 10
0
        public void ParseValue_DateTimeOffset()
        {
            // arrange
            DateType       dateType = new DateType();
            DateTimeOffset dateTime = new DateTimeOffset(
                new DateTime(2018, 6, 11, 8, 46, 14),
                new TimeSpan(4, 0, 0));
            string expectedLiteralValue = "2018-06-11";

            // act
            StringValueNode stringLiteral =
                (StringValueNode)dateType.ParseValue(dateTime);

            // assert
            Assert.Equal(expectedLiteralValue, stringLiteral.Value);
        }
Ejemplo n.º 11
0
        public void ParseLiteral_StringValueNode()
        {
            // arrange
            var uuidType = new UuidType();
            var expected = Guid.NewGuid();
            var literalA = new StringValueNode(expected.ToString("N"));
            var literalB = new StringValueNode(expected.ToString("P"));

            // act
            var runtimeValueA = (Guid)uuidType.ParseLiteral(literalA) !;
            var runtimeValueB = (Guid)uuidType.ParseLiteral(literalB) !;

            // assert
            Assert.Equal(expected, runtimeValueA);
            Assert.Equal(expected, runtimeValueB);
        }
Ejemplo n.º 12
0
        public void ParseLiteral_StringValueNode_DifferentCulture(
            string cultureName)
        {
            // arrange
            Thread.CurrentThread.CurrentCulture =
                CultureInfo.GetCultureInfo(cultureName);

            var dateType         = new DateType();
            var literal          = new StringValueNode("2018-06-29");
            var expectedDateTime = new DateTime(2018, 6, 29);

            // act
            var dateTime = (DateTime)dateType.ParseLiteral(literal) !;

            // assert
            Assert.Equal(expectedDateTime, dateTime);
        }
Ejemplo n.º 13
0
        public MemberSymbol(string label, List <ValueNode> values)
            : base(label)
        {
            acceptedValues = values;

            // Check if there is any values, then use the first as current
            if (acceptedValues.Count > 0)
            {
                value = acceptedValues[0] switch
                {
                    IntValueNode t => t.Value,
                    ArrowValueNode t => t.LeftValue,
                    StringValueNode t => t.Value,
                                 _ => throw new Exception($"Unknown value type \'{ acceptedValues[0].GetType().Name }\' in member .{ Label }")
                };
            }
        }
Ejemplo n.º 14
0
        protected bool TryParseValue(object value, out IValueNode valueNode)
        {
            if (value == null)
            {
                valueNode = new NullValueNode(null);
                return(true);
            }

            if (TrySerialize(value, out string serializedValue))
            {
                valueNode = new StringValueNode(serializedValue);
                return(true);
            }

            valueNode = null;
            return(false);
        }
Ejemplo n.º 15
0
        public void LocalTime_ParseLiteralStringValueDifferentCulture(string cultureName)
        {
            // arrange
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);

            ScalarType scalar           = new LocalTimeType();
            var        valueSyntax      = new StringValueNode("2018-06-29T08:46:14+04:00");
            var        expectedDateTime = new DateTimeOffset(
                new DateTime(2018, 6, 29, 8, 46, 14),
                new TimeSpan(4, 0, 0));

            // act
            var dateTime = (DateTime)scalar.ParseLiteral(valueSyntax) !;

            // assert
            Assert.Equal(expectedDateTime, dateTime);
        }
Ejemplo n.º 16
0
        public override void OnBeforeCompleteType(
            ITypeCompletionContext completionContext,
            DefinitionBase?definition,
            IDictionary <string, object?> contextData)
        {
            if ((completionContext.IsQueryType ?? false) &&
                definition is ObjectTypeDefinition objectTypeDefinition)
            {
                ObjectFieldDefinition typeNameField = objectTypeDefinition.Fields.First(
                    t => t.Name.Equals(IntrospectionFields.TypeName) && t.IsIntrospectionField);
                var index = objectTypeDefinition.Fields.IndexOf(typeNameField) + 1;

                var descriptor = ObjectFieldDescriptor.New(
                    completionContext.DescriptorContext,
                    Node);

                IIdSerializer serializer =
                    completionContext.Services.GetService <IIdSerializer>() ??
                    new IdSerializer();

                descriptor
                .Argument(Id, a => a.Type <NonNullType <IdType> >().ID())
                .Type <NodeType>()
                .Resolve(async ctx =>
                {
                    StringValueNode id     = ctx.ArgumentLiteral <StringValueNode>(Id);
                    IdValue deserializedId = serializer.Deserialize(id.Value);

                    ctx.SetLocalValue(NodeId, id.Value);
                    ctx.SetLocalValue(InternalId, deserializedId.Value);
                    ctx.SetLocalValue(InternalType, deserializedId.TypeName);
                    ctx.SetLocalValue(WellKnownContextData.IdValue, deserializedId);

                    if (ctx.Schema.TryGetType(deserializedId.TypeName, out ObjectType type) &&
                        type.ContextData.TryGetValue(NodeResolver, out object?o) &&
                        o is FieldResolverDelegate resolver)
                    {
                        return(await resolver.Invoke(ctx).ConfigureAwait(false));
                    }

                    return(null);
                });

                objectTypeDefinition.Fields.Insert(index, descriptor.CreateDefinition());
            }
        }
Ejemplo n.º 17
0
        public void AddLocation_From_SyntaxNode()
        {
            // arrange
            var syntaxNode = new StringValueNode(
                new HotChocolate.Language.Location(1, 2, 3, 4),
                "abc",
                false);

            // act
            IError error = ErrorBuilder.New()
                           .SetMessage("bar")
                           .AddLocation(syntaxNode)
                           .Build();

            // assert
            Assert.Collection(error.Locations,
                              t => Assert.Equal(3, t.Line));
        }
Ejemplo n.º 18
0
        public void IsInstanceOfType()
        {
            // arrange
            BooleanValueNode boolLiteral   = new BooleanValueNode(null, true);
            StringValueNode  stringLiteral = new StringValueNode(null, "12345", false);
            NullValueNode    nullLiteral   = NullValueNode.Default;

            // act
            BooleanType booleanType               = new BooleanType();
            bool        isIntLiteralInstanceOf    = booleanType.IsInstanceOfType(boolLiteral);
            bool        isStringLiteralInstanceOf = booleanType.IsInstanceOfType(stringLiteral);
            bool        isNullLiteralInstanceOf   = booleanType.IsInstanceOfType(nullLiteral);

            // assert
            Assert.True(isIntLiteralInstanceOf);
            Assert.False(isStringLiteralInstanceOf);
            Assert.True(isNullLiteralInstanceOf);
        }
Ejemplo n.º 19
0
        public void IsInstanceOfType()
        {
            // arrange
            IntValueNode    intLiteral    = new IntValueNode(null, "12345");
            StringValueNode stringLiteral = new StringValueNode(null, "12345", false);
            NullValueNode   nullLiteral   = new NullValueNode(null);

            // act
            IntType integerType               = new IntType();
            bool    isIntLiteralInstanceOf    = integerType.IsInstanceOfType(intLiteral);
            bool    isStringLiteralInstanceOf = integerType.IsInstanceOfType(stringLiteral);
            bool    isNullLiteralInstanceOf   = integerType.IsInstanceOfType(nullLiteral);

            // assert
            Assert.True(isIntLiteralInstanceOf);
            Assert.False(isStringLiteralInstanceOf);
            Assert.True(isNullLiteralInstanceOf);
        }
Ejemplo n.º 20
0
        public void ParseValue_Local_DateTime()
        {
            // arrange
            DateTimeType dateTimeType = new DateTimeType();
            DateTime     dateTime     =
                new DateTime(2018, 6, 11, 8, 46, 14, DateTimeKind.Local);
            DateTimeOffset offset         = dateTime;
            DateTime       offsetDateTime = offset.DateTime;

            // act
            StringValueNode stringLiteral =
                (StringValueNode)dateTimeType.ParseValue(dateTime);
            StringValueNode stringLiteralOffset =
                (StringValueNode)dateTimeType.ParseValue(offsetDateTime);

            // assert
            Assert.Equal(stringLiteral, stringLiteralOffset);
        }
        internal static DeferDirective?GetDeferDirective(
            this IReadOnlyList <DirectiveNode> directives,
            IVariableValueCollection variables)
        {
            DirectiveNode?directiveNode =
                GetDirective(directives, WellKnownDirectives.Defer);

            if (directiveNode is not null)
            {
                var    @if   = false;
                string?label = null;

                foreach (ArgumentNode argument in directiveNode.Arguments)
                {
                    switch (argument.Name.Value)
                    {
                    case WellKnownDirectives.IfArgument:
                        @if = argument.Value switch
                        {
                            VariableNode variable =>
                            variables.GetVariable <BooleanValueNode>(
                                variable.Name.Value).Value,
                            BooleanValueNode b => b.Value,
                            _ => @if
                        };
                        break;

                    case WellKnownDirectives.LabelArgument:
                        label = argument.Value switch
                        {
                            VariableNode variable =>
                            variables.GetVariable <string?>(variable.Name.Value),
                            StringValueNode b => b.Value,
                            _ => label
                        };
                        break;
                    }
                }

                return(new DeferDirective(@if, label));
            }

            return(null);
        }
Ejemplo n.º 22
0
        public void ParseLiteral_StringValueNode_DifferentCulture(string cultureName)
        {
            // arrange
            Thread.CurrentThread.CurrentCulture =
                CultureInfo.GetCultureInfo(cultureName);

            var dateTimeType = new DateTimeType();
            var literal      = new StringValueNode(
                "2018-06-29T08:46:14+04:00");
            var expectedDateTime = new DateTimeOffset(
                new DateTime(2018, 6, 29, 8, 46, 14),
                new TimeSpan(4, 0, 0));

            // act
            var dateTime = (DateTimeOffset)dateTimeType
                           .ParseLiteral(literal);

            // assert
            Assert.Equal(expectedDateTime, dateTime);
        }
Ejemplo n.º 23
0
        public void NonNullEnumsSerializeCorrectlyFromVariables()
        {
            // arrange
            Schema schema = CreateSchema();
            string query  = @"
                query getHero($episode: Episode!) {
                    hero(episode: $episode) {
                        name
                    }
                }";

            var variables = new Dictionary <string, object>();

            variables["episode"] = new StringValueNode("NEWHOPE");

            // act
            IExecutionResult result = schema.Execute(query, variables);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
Ejemplo n.º 24
0
    private static async ValueTask <object?> ResolveSingleNode(
        IResolverContext context,
        IIdSerializer serializer,
        NameString argumentName)
    {
        StringValueNode nodeId         = context.ArgumentLiteral <StringValueNode>(argumentName);
        IdValue         deserializedId = serializer.Deserialize(nodeId.Value);
        NameString      typeName       = deserializedId.TypeName;

        context.SetLocalValue(NodeId, nodeId.Value);
        context.SetLocalValue(InternalId, deserializedId.Value);
        context.SetLocalValue(InternalType, typeName);
        context.SetLocalValue(WellKnownContextData.IdValue, deserializedId);

        if (context.Schema.TryGetType <ObjectType>(typeName, out ObjectType? type) &&
            type.ContextData.TryGetValue(NodeResolver, out var o) &&
            o is FieldResolverDelegate resolver)
        {
            return(await resolver.Invoke(context).ConfigureAwait(false));
        }

        return(null);
    }
Ejemplo n.º 25
0
        public override StatementNode VisitIdentifierAssignStatement([NotNull] CoronaParser.IdentifierAssignStatementContext context)
        {
            BuildValueAst      valueVisitor = new BuildValueAst();
            BuildExpressionAst exprVisitor  = new BuildExpressionAst();

            try
            {
                IdentifierValueNode id = valueVisitor.Visit(context.identifierValue()) as IdentifierValueNode;

                // Get value
                ValueNode value;
                if (context.expr() != null)
                {
                    value = exprVisitor.Visit(context.expr());
                }
                else
                {
                    value = new StringValueNode(context.STRING().GetText());
                }

                return(new IdentifierAssignmentStatementNode(id, value));
            }
            catch (TheLanguageErrorException e) { throw new TheLanguageErrorException("Identifier assignment statement", e); }
        }
Ejemplo n.º 26
0
    private static async ValueTask <object?> ResolveManyNode(
        IResolverContext context,
        IIdSerializer serializer)
    {
        if (context.ArgumentKind(Ids) == ValueKind.List)
        {
            ListValueNode    list  = context.ArgumentLiteral <ListValueNode>(Ids);
            Task <object?>[] tasks = ArrayPool <Task <object?> > .Shared.Rent(list.Items.Count);

            var result = new object?[list.Items.Count];

            try
            {
                for (var i = 0; i < list.Items.Count; i++)
                {
                    context.RequestAborted.ThrowIfCancellationRequested();

                    // it is guaranteed that this is always a string literal.
                    StringValueNode nodeId         = (StringValueNode)list.Items[i];
                    IdValue         deserializedId = serializer.Deserialize(nodeId.Value);
                    NameString      typeName       = deserializedId.TypeName;

                    context.SetLocalValue(NodeId, nodeId.Value);
                    context.SetLocalValue(InternalId, deserializedId.Value);
                    context.SetLocalValue(InternalType, typeName);
                    context.SetLocalValue(WellKnownContextData.IdValue, deserializedId);

                    tasks[i] =
                        context.Schema.TryGetType <ObjectType>(typeName, out ObjectType? type) &&
                        type.ContextData.TryGetValue(NodeResolver, out var o) &&
                        o is FieldResolverDelegate resolver
                            ? resolver.Invoke(context).AsTask()
                            : _nullTask;
                }

                for (var i = 0; i < list.Items.Count; i++)
                {
                    context.RequestAborted.ThrowIfCancellationRequested();

                    Task <object?> task = tasks[i];
                    if (task.IsCompleted)
                    {
                        if (task.Exception is null)
                        {
                            result[i] = task.Result;
                        }
                        else
                        {
                            result[i] = null;
                            ReportError(context, i, task.Exception);
                        }
                    }
                    else
                    {
                        try
                        {
                            result[i] = await task;
                        }
                        catch (Exception ex)
                        {
                            result[i] = null;
                            ReportError(context, i, ex);
                        }
                    }
                }

                return(result);
            }
            finally
            {
                ArrayPool <Task <object?> > .Shared.Return(tasks);
            }
        }
        else
        {
            var result = new object?[1];
            result[0] = await ResolveSingleNode(context, serializer, Ids);

            return(result);
        }
    }
Ejemplo n.º 27
0
 public override dynamic Visit(StringValueNode node)
 {
     return(PrimitiveType.String);
 }
Ejemplo n.º 28
0
 public override void ProcessNode(StringValueNode node)
 {
 }
Ejemplo n.º 29
0
 /// <inheritdoc />
 protected override bool IsInstanceOfType(StringValueNode valueSyntax)
 {
     return(ValidatePostCode(valueSyntax.Value));
 }
Ejemplo n.º 30
0
 public string Visit(StringValueNode node)
 {
     return(node.Value);
 }