Skip to content

Using gql for query editing #272

Closed
Closed

Description

@abitrolly

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.

Activity

  1. leszekhanusz commented on Dec 2, 2021

    @leszekhanusz
    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.

  2. added
    type: question or discussion
    on Dec 2, 2021
  3. abitrolly commented on Dec 3, 2021

    @abitrolly
    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.

  4. abitrolly commented on Dec 3, 2021

    @abitrolly
    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?
  5. leszekhanusz commented on Dec 3, 2021

    @leszekhanusz
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: question or discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions