Best way to construct a GraphQL query string in Python

Viewed 10673

I'm trying to do this (see title), but it's a bit complicated since the string I'm trying to build has to have the following properties:

  • mulitiline
  • contains curly braces
  • I want to inject variables into it

Using a normal '''''' multiline string makes injecting variables difficult. Using multiple f-strings makes injecting variables easy, but every curly brace, of which there are a lot, has to be doubled. And an f has to be prepended to each line. On the other hand, if I try using format, it also gets confused by all the curly braces.

Is there a better way that I haven't considered yet?

2 Answers

You can use the """ multiline string method. For injecting variables, make sure to use the $ sign while defining the string and use the variables object in the JSON parameter of the requests.post method.

Here is an example. ContactInput is one of the types I defined in my GraphQL schema.

query = """
  mutation ($input:[ContactInput!]!) {
    AddContacts(contacts: $input) {
      user_id
    }
  }
"""
variables = {'input': my_arrofcontacts}
r = requests.post(url, json={'query': query , 'variables': variables})

There is no need to construct graphQL strings to inject variables.

This is even abusing graphQL as it was designed to separate query definition and variables (arguments). It's safer, easier to validate etc.

Just learn how to use query variables to pass arguments. This is described in docs and many tutorials. You should know and use in practice this technique. Trying to use string injections can at least prove lack of your knowledge.

If you aren't writting a graphql editor or other tool then you shouldn't use string operations at all. BTW even editor shouldn't operate on strings but on AST.

It's rarely required to operate on strings in graphql, f.e. to let user choose required answer (response part of query) elements/fragments.

Update

Operating using objects is more elastic/usable - e.g. easily solves conditional problems: How to conditionally include an argument in a GraphQL query?

Related