graphql-python/gql

Generate domain specific language query from string query

aychang95 opened this issue · 1 comments

Hi, first of all thanks for providing an open source tool like gql! It's been really useful.

I might have missed this, but is there a way to generate a DSL query from a string query without manually building it?

I.E.

ds = DSLSchema(StarWarsSchema)

# Outputs DSLQuery object provided the string query and potentially some fragments
query = gql.dsl_query_gen(
  schema = ds,
  query ="""
   query {
      hero {
        id
        name
        friends {
          name
        }
      }
    }
  """,
  fragments=""
)

result = client.execute(query)

The motivation being that we have a large number of *.graphql files that contain DSL queries, and it would be nice if we could generate GQL queries from these files/queries.

Generating queries directly from text is the default method for gql (like in the basic example), you don't need the DSL module for that.

The DSL module is only useful if you want to programmatically add something to the query (i.e. depending on a condition in the code)

If you have a file with graphql queries, an option is to create a query using the full content of the file and use the operation_name argument of the execute method to select the correct query to execute. This would send the full file though so it might be inefficient if the file is too big.

If you really want to have a DSL query from a string query because you want to add something to that query later depending on a condition, unfortunately this is not supported right now, you need to create the full query from scratch using the DSL module in that case.