Exemplo n.º 1
0
        public Utf8GraphQLRequestParser(
            ReadOnlySpan <byte> requestData,
            ParserOptions options,
            IDocumentCache cache,
            IDocumentHashProvider hashProvider)
        {
            _options = options
                       ?? throw new ArgumentNullException(nameof(options));
            _cache        = cache;
            _hashProvider = hashProvider;

            _reader   = new Utf8GraphQLReader(requestData);
            _useCache = cache != null;
        }
Exemplo n.º 2
0
        public void OneGraph_Schema()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes(FileResource.Open("onegraph.graphql"));

            // act
            var reader = new Utf8GraphQLReader(sourceText);

            while (reader.Read())
            {
            }

            // assert
            // the document was lexed without syntax exception
        }
Exemplo n.º 3
0
        internal Utf8GraphQLParser(
            Utf8GraphQLReader reader,
            ParserOptions?options = null)
        {
            if (reader.Kind == TokenKind.EndOfFile)
            {
                throw new ArgumentException(GraphQLData_Empty, nameof(reader));
            }

            options ??= ParserOptions.Default;
            _createLocation    = !options.NoLocations;
            _allowFragmentVars = options.Experimental.AllowFragmentVariables;
            _reader            = reader;
            _description       = null;
        }
Exemplo n.º 4
0
        public Utf8GraphQLParser(
            ReadOnlySpan <byte> graphQLData,
            ParserOptions?options = null)
        {
            if (graphQLData.Length == 0)
            {
                throw new ArgumentException(GraphQLData_Empty, nameof(graphQLData));
            }

            options ??= ParserOptions.Default;
            _createLocation    = !options.NoLocations;
            _allowFragmentVars = options.Experimental.AllowFragmentVariables;
            _reader            = new Utf8GraphQLReader(graphQLData);
            _description       = null;
        }
Exemplo n.º 5
0
        private void ReadToken(string sourceText)
        {
            // arrange
            string nameTokenValue = "helloWorld_123";

            byte[] source = Encoding.UTF8.GetBytes(sourceText);
            var    reader = new Utf8GraphQLReader(source);

            // act
            reader.Read();

            // assert
            Assert.Equal(TokenKind.Name, reader.Kind);
            Assert.Equal(nameTokenValue, reader.GetName());
        }
        private void ReadToken(string sourceBody, TokenKind kind)
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(sourceBody);
            var    reader = new Utf8GraphQLReader(source);

            // act
            reader.Read();

            // assert
            Assert.Equal(kind, reader.Kind);
            Assert.Equal(1, reader.Line);
            Assert.Equal(1, reader.Column);
            Assert.Equal(0, reader.Start);
            Assert.Equal(sourceBody.Length, reader.End);
        }
        private void ParseMessageProperty(ref Message message)
        {
            ReadOnlySpan <byte> fieldName = _reader.Expect(TokenKind.String);

            _reader.Expect(TokenKind.Colon);

            switch (fieldName[0])
            {
            case _t:
                if (fieldName.SequenceEqual(_type))
                {
                    message.Type = ParseStringOrNull();
                    return;
                }
                break;

            case _i:
                if (fieldName.SequenceEqual(_id))
                {
                    message.Id = ParseStringOrNull();
                    return;
                }
                break;

            case _p:
                if (fieldName.SequenceEqual(_payload))
                {
                    int start = _reader.Start;
                    message.HasPayload = !IsNullToken();
                    int end = SkipValue();
                    message.Payload = _reader.GraphQLData.Slice(
                        start, end - start);
                    return;
                }
                break;

            default:
                // TODO : resources
                throw new SyntaxException(
                          _reader,
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Unexpected request property name `{0}` found.",
                              Utf8GraphQLReader.GetString(fieldName, false)));
            }
        }
Exemplo n.º 8
0
        public void Read_NameBraceTokens()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes("{ x { y } }");

            // act
            var tokens = new List<SyntaxTokenInfo>();
            var reader = new Utf8GraphQLReader(sourceText);

            while (reader.Read())
            {
                tokens.Add(SyntaxTokenInfo.FromReader(in reader));
            }

            // assert
            tokens.MatchSnapshot();
        }
Exemplo n.º 9
0
        private void UnescapeEmpty()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes("\"\"");
            var    reader = new Utf8GraphQLReader(source);

            reader.Read();

            // act
            var buffer = new byte[1];
            var span   = buffer.AsSpan();

            reader.UnescapeValue(ref span);

            // assert
            Assert.Equal(0, span.Length);
        }
Exemplo n.º 10
0
        public void Read_KitchenSinkQuery()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes(
                FileResource.Open("kitchen-sink.graphql"));

            // act
            var tokens = new List<SyntaxTokenInfo>();
            var reader = new Utf8GraphQLReader(sourceText);

            while (reader.Read())
            {
                tokens.Add(SyntaxTokenInfo.FromReader(in reader));
            }

            // assert
            tokens.MatchSnapshot();
        }
Exemplo n.º 11
0
        public void Read_String_SkipEscapes()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes(
                "abc \"def\\\"\" ghi");

            // act
            var tokens = new List<SyntaxTokenInfo>();
            var reader = new Utf8GraphQLReader(sourceText);

            while (reader.Read())
            {
                tokens.Add(SyntaxTokenInfo.FromReader(in reader));
            }

            // assert
            tokens.MatchSnapshot();
        }
        private void ReadToken_SingleLine_ParsesCorrectly()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                "\"\"\"helloWorld_123\"\"\"");
            var reader = new Utf8GraphQLReader(source);

            // act
            reader.Read();

            // assert
            Assert.Equal("helloWorld_123", reader.GetString());
            Assert.Equal(TokenKind.BlockString, reader.Kind);
            Assert.Equal(1, reader.Line);
            Assert.Equal(1, reader.Column);
            Assert.Equal(0, reader.Start);
            Assert.Equal(19, reader.End);
        }
Exemplo n.º 13
0
        public void Read_BlockStringValue()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes(
                "{ me(a: \"\"\"\n     Abcdef\n\"\"\") }");

            // act
            var tokens = new List<SyntaxTokenInfo>();
            var reader = new Utf8GraphQLReader(sourceText);

            while (reader.Read())
            {
                tokens.Add(SyntaxTokenInfo.FromReader(in reader));
            }

            // assert
            tokens.MatchSnapshot();
        }
Exemplo n.º 14
0
        private void UnescapeString()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes("\"abc\"");
            var    reader = new Utf8GraphQLReader(source);

            reader.Read();

            // act
            var buffer = new byte[3 * 4];
            var span   = buffer.AsSpan();

            reader.UnescapeValue(ref span);

            // assert
            Assert.Equal(3, span.Length);
            Assert.Equal("abc", Utf8GraphQLReader.GetString(span));
        }
        public static void ParseTypeReference_Reader()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes("[[String!]]");
            var    reader     = new Utf8GraphQLReader(sourceText);

            reader.MoveNext();

            // act
            ITypeNode type = Utf8GraphQLParser.Syntax.ParseTypeReference(reader);

            // assert
            Assert.Equal(
                "String",
                Assert.IsType <NamedTypeNode>(
                    Assert.IsType <NonNullTypeNode>(
                        Assert.IsType <ListTypeNode>(
                            Assert.IsType <ListTypeNode>(type).Type).Type).Type).Name.Value);
        }
Exemplo n.º 16
0
        private void ReadToken(string sourceBody, bool isFloat)
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(sourceBody);
            var    reader = new Utf8GraphQLReader(source);

            // act
            reader.Read();

            // assert
            Assert.Equal(sourceBody, reader.GetScalarValue());
            Assert.Equal(
                isFloat ? TokenKind.Float : TokenKind.Integer,
                reader.Kind);
            Assert.Equal(1, reader.Line);
            Assert.Equal(1, reader.Column);
            Assert.Equal(0, reader.Start);
            Assert.Equal(sourceBody.Length, reader.End);
        }
Exemplo n.º 17
0
        private void InvalidDigit()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes("123.F");
            var    reader = new Utf8GraphQLReader(source);
            bool   raised = false;

            // act
            try
            {
                reader.Read();
            }
            catch (SyntaxException ex)
            {
                raised = true;
                ex.Message.MatchSnapshot();
            }

            // assert
            Assert.True(raised);
        }
Exemplo n.º 18
0
        private void UnexpectedSyntaxException()
        {
            // arrange
            byte[] source = new byte[] { 187 };
            var    reader = new Utf8GraphQLReader(source);
            bool   raised = false;

            // act
            try
            {
                reader.Read();
            }
            catch (SyntaxException ex)
            {
                raised = true;
                ex.Message.MatchSnapshot();
            }

            // assert
            Assert.True(raised);
        }
Exemplo n.º 19
0
        public void Read_Two_NameTokens()
        {
            var source = new ReadOnlySpan<byte>(
                Encoding.UTF8.GetBytes("type foo"));
            var lexer = new Utf8GraphQLReader(source);

            Assert.Equal(TokenKind.StartOfFile, lexer.Kind);

            Assert.True(lexer.Read());
            Assert.Equal(TokenKind.Name, lexer.Kind);
            Assert.Equal("type",
                Encoding.UTF8.GetString(lexer.Value.ToArray()));

            Assert.True(lexer.Read());
            Assert.Equal(TokenKind.Name, lexer.Kind);
            Assert.Equal("foo",
                Encoding.UTF8.GetString(lexer.Value.ToArray()));

            Assert.False(lexer.Read());
            Assert.Equal(TokenKind.EndOfFile, lexer.Kind);
        }
        private void ReadToken_WithEscapedTrippleQuote2_EscapeIsReplacedWithActualQuotes()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                "\"\"\"hello\\\"\"\"World_123\r\n\t\tfoo\r\n\tbar\"\"\"");
            var reader = new Utf8GraphQLReader(source);

            // act
            reader.Read();

            // assert
            Assert.Equal(
                "hello\"\"\"World_123\n\tfoo\nbar",
                reader.GetString());

            Assert.Equal(TokenKind.BlockString, reader.Kind);
            Assert.Equal(1, reader.Line);
            Assert.Equal(1, reader.Column);
            Assert.Equal(0, reader.Start);
            Assert.Equal(36, reader.End);
        }
Exemplo n.º 21
0
        internal Utf8GraphQLParser(
            Utf8GraphQLReader reader,
            ParserOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (reader.Kind == TokenKind.EndOfFile)
            {
                throw new ArgumentException(
                          LangResources.GraphQLData_Empty,
                          nameof(reader));
            }

            _options           = options;
            _createLocation    = !options.NoLocations;
            _allowFragmentVars = options.Experimental.AllowFragmentVariables;
            _reader            = reader;
            _description       = null;
        }
Exemplo n.º 22
0
        public Utf8GraphQLParser(
            ReadOnlySpan <byte> graphQLData,
            ParserOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (graphQLData.Length == 0)
            {
                throw new ArgumentException(
                          LangResources.GraphQLData_Empty,
                          nameof(graphQLData));
            }

            _options           = options;
            _createLocation    = !options.NoLocations;
            _allowFragmentVars = options.Experimental.AllowFragmentVariables;
            _reader            = new Utf8GraphQLReader(graphQLData);
            _description       = null;
        }
        private void ReadToken_WithLeadingBlanks_BlanksAreRemoved()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                "\"\"\"\r\n\t\r\n\t\r\n\thelloWorld_123" +
                "\r\n\t\tfoo\r\n\tbar\"\"\"");
            var reader = new Utf8GraphQLReader(source);

            // act
            reader.Read();

            // assert
            Assert.Equal(
                "helloWorld_123\n\tfoo\nbar",
                reader.GetString());

            Assert.Equal(TokenKind.BlockString, reader.Kind);
            Assert.Equal(1, reader.Line);
            Assert.Equal(1, reader.Column);
            Assert.Equal(0, reader.Start);
            Assert.Equal(41, reader.End);
        }
Exemplo n.º 24
0
 // TODO : resources
 public static void QueryMustBeStringOrNull(Utf8GraphQLReader reader) =>
 throw new SyntaxException(
           reader,
           "The query field must be a string or null.");
 public static ObjectValueNode ParseObjectLiteral(
     Utf8GraphQLReader reader,
     bool constant = true) =>
 new Utf8GraphQLParser(reader).ParseObject(constant);
 public static IValueNode ParseValueLiteral(
     Utf8GraphQLReader reader,
     bool constant = true) =>
 new Utf8GraphQLParser(reader).ParseValueLiteral(constant);
 /// <summary>
 /// Parses a GraphQL selection set string e.g. { field(arg: "abc") }
 /// </summary>
 public static SelectionSetNode ParseSelectionSet(
     Utf8GraphQLReader reader) =>
 new Utf8GraphQLParser(reader).ParseSelectionSet();
 /// <summary>
 /// Parses a GraphQL field selection string e.g. field(arg: "abc")
 /// </summary>
 public static FieldNode ParseField(
     Utf8GraphQLReader reader) =>
 new Utf8GraphQLParser(reader).ParseField();
 /// <summary>
 /// Parses a GraphQL type reference e.g. [String!]
 /// </summary>
 public static ITypeNode ParseTypeReference(
     Utf8GraphQLReader reader) =>
 new Utf8GraphQLParser(reader).ParseTypeReference();
Exemplo n.º 30
0
 // TODO : resources
 public static void InvalidRequestStructure(Utf8GraphQLReader reader) =>
 throw new SyntaxException(
           reader,
           "Expected `{` or `[` as first syntax token.");