pythongql

How to get a string of a query created using Python's gql.dsl module


fairly new to gql in Python, and I've been working on creating some queries using the dsl module. I am currently running into an issue with a query I've created, and I was wondering if there were any easy way to view the string of the dsl query the gql library is sending to the specified server? I am unable to use any external tools to view the communication between my client and the server, so the solution would have to be in Python. Thanks!


Solution

  • I had the same issue, I found a following solution, you can use print_ast:

    from graphql import print_ast
    from gql.dsl import DSLSchema, dsl_gql
    
    query = dsl_gql(
           DSLQuery(
               ds.Query.hero.select(
                  ds.Character.id,
                  ds.Character.name,
                  ds.Character.friends.select(ds.Character.name),
                  )
             )
          )
    
    raw_query_string = print_ast(query)
    
    # Print the GraphQL query
    
    print(raw_query_string)