Skip to content

Modify query programmatically #149

Closed
@abitrolly

Description

@abitrolly

Sent here from graphql-python/gql#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%20ast#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions