Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manipulate AST with visitor #1740

Merged
merged 10 commits into from
Aug 14, 2018
Merged

Manipulate AST with visitor #1740

merged 10 commits into from
Aug 14, 2018

Conversation

rmosolgo
Copy link
Owner

@rmosolgo rmosolgo commented Aug 6, 2018

It would be nice to have a way to programmaticaly modify GraphQL ASTs, but we don't have one yet. I propose a way to do it using the class-based visitor on the 1.9-dev branch.

Goals:

  • Efficient
  • Easy-to-use
  • Respects immutability of nodes

So, this implementation will treat the AST as a persistent immutable data structure:

  • By default, the tree is only traversed; no modification or duplication
  • When a hook returns a new node, then:
    • the node's parent is shallow-copied; the copy is modified to include the new child
    • the copied parent is returned so that its parent may also be modified

TODO:

  • Lots of nodes need reorganization to support persistent copying, for example:
    • Implementing .children_method_name (I sure hope that each node type has only one possible name!)
    • Refactor initialize_node to call set_attributes?
    • But, instance_variable_set is slow, consider another metaprogramming approach?
    • Maybe generate classes from configuration? There's a lot of duplication and I'm about to add a lot more.
  • Implement more modifications:
    • modify node & replace child
    • delete node & remove child
    • create node & insert child
    • others?
  • Add a guide

Fixes #455

@rmosolgo rmosolgo added this to the 1.9.0 milestone Aug 6, 2018
@rmosolgo rmosolgo mentioned this pull request Aug 6, 2018
7 tasks
@rmosolgo rmosolgo merged commit 3df83dc into 1.9-dev Aug 14, 2018
@rmosolgo rmosolgo deleted the ast-maniputation branch August 14, 2018 14:33
rmosolgo pushed a commit that referenced this pull request Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant