Skip to content

Using gql for query editing #272

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

Closed
abitrolly opened this issue Dec 2, 2021 · 4 comments
Closed

Using gql for query editing #272

abitrolly opened this issue Dec 2, 2021 · 4 comments
Labels
type: question or discussion Issue discussing or asking a question about gql

Comments

@abitrolly
Copy link

Can qgl be used to programmatically modify queries?

For example to transform this request.

    query getContinents {
      continents {
        code
        name
      }
    }

To this one.

    query getContinents {
      continents (code:"AF") {
        code
        name
      }
    }

gql.gql() parses the input into graphql AST DocumentNode, but it is not clear which methods are available to modify it, and how to write the query back as a string.

@leszekhanusz
Copy link
Collaborator

You can use the DSL module to create requests programmatically.

To convert a DocumentNode to string again, you can use the print_ast method of graphql.

@leszekhanusz leszekhanusz added the type: question or discussion Issue discussing or asking a question about gql label Dec 2, 2021
@abitrolly
Copy link
Author

I was able to convert a query to DocumentNode and back to string.

import gql

dg = gql.gql("""
    query getContinents {
      continents {
        code
        name
      }
    }
""")

from graphql import print_ast
print(print_ast(dg))
query getContinents {
  continents {
    code
    name
  }
}

However, I don't see any methods that allow to traverse the tree to find continents node and add a parameter to it.

@abitrolly
Copy link
Author

It is not enough for editing to just use DSL to create queries. This one is addressed.

  • How to parse query into "edit mode" and get back the string

However, two more questions are still left.

  • How to find AST node that needs modification?
  • How to modify it?

@leszekhanusz
Copy link
Collaborator

This not really a gql question but instead a graphql-core question.

You might want to look at the dsl module source code which does exactly that.
For example: to add an argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about gql
Projects
None yet
Development

No branches or pull requests

2 participants