Add utility for building GQL queries from Python dicts/sequences #342
+252
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Building GQL as strings is fragile and difficult to trap errors.
In lieu of building a tool that validates GQL against a schema, I made a simple helper for building up a GQL query from simple Python objects. The query can be parsed into a GQL string, which itself can be validated against a schema using one of many tools.
This tools expects a combination of
dicts
andlists
(or other sequences) and stitches them together into a query. The parser is useable with strings, but also has a more advancedGQLObject
If you use the
GQLObject
, you get three advantages:GQLObjects
can be treated as "lite" schemas, with class attributes corresponding to available fieldsGQLObjects
can change their string representation without affecting the object itself, allowing schemas to change without code changingGQLObjects
can be called, allowing a nice syntax for creating arguments.For example, using strings: