question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modify query programmatically

See original GitHub issue

Sent here from https://github.com/graphql-python/gql/issues/272

There is a way in gql to construct queries programmatically with DSL module or by parsing a string into AST with gql.gql() and then using print_ast from graphql to get back the string.

import gql

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

from graphql import print_ast
print(print_ast(dg))

What is not clear is how to actually find nodes in AST and edit or expand them. For example, finding a parent of node in the query below (continents) and adding an attribute to it ((code:"AF")).

query getContinents {
  continents {
    code
    name
  }
}

So that the query becomes.

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

I looked into the docs, but it doesn’t actually explain.

  • How to find AST node that needs modification?
  • How to modify it (upsert attributes)?

The documentation container chapter about schemas https://graphql-core-3.readthedocs.io/en/latest/usage/extension.html?highlight=modify ast#extending-a-schema and as I am new to GraphQL I am not yet sure if schema and query are the same things.

Feature requests

I am not sure GraphQL.js includes this too and not sure that fundamentally changes the way GraphQL works.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Citocommented, Dec 27, 2021

@abitrolly I’m currently going through the old tickets before creating the next release. Feel free to reopen or open a new issue if you have concrete questions.

1reaction
Citocommented, Dec 5, 2021

@abitrolly You can use a Visitor to traverse the AST. This will also give you access to the immediate parent and full list of ancestors of the current node.

Here is an example of a visitor that searches for a field node with the name ‘code’, and then adds another field node with the name ‘newField’ to its parent.

from graphql import FieldNode, NameNode, Visitor, parse, print_ast, visit
from graphql.pyutils import FrozenList

query = """
query getContinents {
  continents {
    code
    name
  }
}"""

doc = parse(query)

class ExampleVisitor(Visitor):

    def enter_name(self, node, key, parent, path, ancestors):
        if node.value == 'code' and parent.kind == 'field':
            parent = ancestors[-2]  # selection set
            parent.selections = FrozenList(
                (*parent.selections,
                 FieldNode(name=NameNode(value='newField'))))
            return True  # stop visiting

visitor = ExampleVisitor()
visit(doc, visitor)

new_query = print_ast(doc)

print(new_query)

Note: It is recommended to use a FrozenList instead of an ordinary list to make the nodes hashable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

So how does one modify a query programmatically? - Drupal
I have created a view from indexed nodes to show products in drupal commerce with faced search blocks. It is a multilanguage site...
Read more >
How to Create and Modify QueryDefs Programmatically in ...
In this episode, we'll look at how to programmatically create and modify query definitions in Microsoft Access. Being able to create and ...
Read more >
How to Programmatically Modify a TFS Query with C# | Jon ...
I had to change TFS Iterations and didn't want to break all existing queries. I also didn't want to update them all manually....
Read more >
how to modify query parameters with programmatically in ...
I wanna be modified query parameters with change body in onProxyRes method cause I use query parameter for request body signed hash string....
Read more >
How to change querydef sql programmatically in MS Access
Changing the query SQL is extremely useful for graphs and output of formatted HTML, especially if this involves complicated crosstabs with user input....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found