Overview
The parser converts GraphQL document strings into structured arrays that the framework uses for execution. While you typically won’t use the parser directly, it’s available for testing and validation.Basic Usage
Parse a GraphQL execution document:Check Spec Version
View the supported GraphQL specification version:Performance
The C-based implementation provides significant performance benefits:- Native Speed - Compiled C code runs faster than pure Ruby
- Memory Efficient - Minimal object allocation during parsing
- Zero Dependencies - No external parser libraries required
Parser Concepts
The parser converts documents into arrays of predefined sizes where information appears in consistent positions. This structure enables fast, predictable access during execution.Token System
Parsed elements are wrapped inGQLParser::Token, a SimpleDelegator that enhances arrays with metadata:
Token Methods
The semantic type of this token (e.g.,
:query, :field, :directive)Starting line number in the source document (1-indexed)
Starting column number in the source document (1-indexed)
Ending line number in the source document
Ending column number in the source document
Check if the token matches a specific type
Data Structures
The parser returns structured arrays for different GraphQL elements:Execution Document
Operation
type- Operation type:"query","mutation", or"subscription"(ornilfor shorthand)name- Optional operation name[*variable]- Variable definitions[*directive]- Directives applied to the operation[*field]- Root fields
Fragment
name- Fragment nametype- Type condition[*directive]- Directives[*field]- Fragment fields
Field
name- Field namealias- Optional alias[*argument]- Field arguments[*directive]- Directives[*field]- Nested selection set
Variable
name- Variable name (without$)type- Type tokenvalue- Default value (if provided)[*directive]- Directives
Directive
name- Directive name (without@)[*argument]- Directive arguments
Argument
name- Argument namevalue- Literal value (if provided)variable_name- Variable reference (if provided)
Type
name- Type namearray_dimensions- Number of list wrappersbitwise_nullability- Encoded nullability information
Spread
name- Fragment name (for fragment spreads)type- Type condition (for inline fragments)[*directive]- Directives[*field]- Fields (for inline fragments)
Type Encoding
Types are encoded efficiently using two numeric fields:Array Dimensions
Integer representing list nesting depth:Bitwise Nullability
Integer encoding non-null markers at each level:Implementation
The parser is implemented in C atext/gql_parser.c. Key parsing functions include:
ext/gql_parser.c
View the complete implementation on GitHub.
Use Cases
Validating Syntax
Test if a document is syntactically valid:Inspecting Structure
Examine parsed document structure for debugging:Testing Parser Behavior
GraphQL Specification
The parser implements the October 2021 GraphQL specification with full support for:- Queries, mutations, and subscriptions
- Variables and default values
- Fragments (named and inline)
- Directives
- List and non-null type modifiers
- Comments and string literals
- Aliases and arguments
The parser is continuously updated to maintain spec compliance as the GraphQL specification evolves.
Related Tools
Testing
Use
valid? to validate documents without parsing manuallyIntrospection
Query schema structure programmatically