GraphQL AST — GraphQLTag — Part Two

Janel John Janson
2 min readFeb 14, 2021

--

In our previous article, we only talked about the basic query and how it is being parsed to build an AST object.

In this article, we are going to understand how variables, directives, and parameters are being parsed.

Consider the following query,

Here we added the variables, $after that is a string and $includeUsername that is a non-nullable Boolean.

Variables

In our previous query, the variableDefinitionswas an empty array. But for this case, it will be not. Variable can only be defined on the operations.

For each variable being defined, there will be a corresponding single entry in the variableDefinitions. Each object has the following properties:

  • kind- which will always be a VariableDefinition
  • variable- This provides the information of the variable, it contains the name of the variable and its type.
  • type- Defines the type of the variable. The interesting part is that, when the variable is not nullable, it’s kindwill be NonNullType($includeUsername: Boolean!), then on the type property where we get the type that is non-nullable, unlike from nullable type, its type will be defined on the property, with kind quals to NamedType.
  • defaultValue- Provides the information of the default value.
  • directive- Provides the information of the directives.

Directives

In the query above we included the directive if on the Field id that has a parameter and a variable as the parameter value.

Directives can be present in the operation, field, and variables. In the query, the directive is being added to the field id.

For each entry in the directives the property they all have the same fields, namely the kind, name, and arguments.

  • kind- This will always be a Directive.
  • name- Holds the information of the name of the field.
  • arguments- Holds the information of the arguments being passed on the directive

Arguments

Based on the query above, there are 2 parts that the argument was present, the first one is from the directives and the second one was on the field.

These arguments are on the addresses field selection. We have declared 2 kinds of arguments the one was a value and the other one was the variable.

Stay tuned for more updates as we continue to understand how fragments are being parsed.

--

--

No responses yet