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

AST visitor should allow to transform the AST #214

Closed
OlegIlyenko opened this issue Feb 15, 2017 · 1 comment
Closed

AST visitor should allow to transform the AST #214

OlegIlyenko opened this issue Feb 15, 2017 · 1 comment
Assignees
Labels
Milestone

Comments

@OlegIlyenko
Copy link
Member

Similar to the reference implementation: https://github.com/graphql/graphql-js/blob/c23f5782dee81b0a59a6a6bb2bcca9da247570e7/src/language/visitor.js#L72-L94

@OlegIlyenko OlegIlyenko added this to the Backlog milestone Feb 15, 2017
@OlegIlyenko OlegIlyenko modified the milestones: v1.1.0, Backlog Mar 6, 2017
@OlegIlyenko OlegIlyenko self-assigned this Mar 6, 2017
@OlegIlyenko
Copy link
Member Author

Here is an example of this feature. Here I just replace all variables with a constant:

import sangria.ast._
import sangria.visitor._

val query =
  gql"""
    query Foo($$aa: String = "some default", $$lang: String) {
      a: aaa(e: one)
      b: aaa(e: $$aa) {
        comments {
          # deeply nested
          name(lang: $$lang)
          ...Bar
        }
      }
    }

    fragment Bar on Comment {
      description(lang: $$lang)
    }
  """

val result =
  AstVisitor.visit(query, AstVisitor {
    case _: VariableDefinition 
      VisitorCommand.Delete
    case _: VariableValue 
      VisitorCommand.Transform(StringValue("some constant"))
  })

println(result.renderPretty)

result will look like this:

query Foo {
  a: aaa(e: one)
  b: aaa(e: "some constant") {
    comments {
      # deeply nested
      name(lang: "some constant")
      ...Bar
    }
  }
}

fragment Bar on Comment {
  description(lang: "some constant")
}

/cc @timsuchanek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant