graphql-python / gql

A GraphQL client in Python
https://gql.readthedocs.io
MIT License
1.54k stars 179 forks source link

examples how to use "gql" method with mutations and variables ? #85

Closed arielvol closed 4 years ago

arielvol commented 4 years ago

Can someone show me an example how to use execute method with the "gql" option and with a mutation example which also needs variables ?

KingDarBoja commented 4 years ago

Hi @arielvol

You can check out the readme and perform the same logic using the query example, like this (taken from test_query):

def test_mutation_result(client):
    query = gql(
        """
        mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
          createReview(episode: $ep, review: $review) {
            stars
            commentary
          }
        }
    """
    )
    params = {
        "ep": "JEDI",
        "review": {"stars": 5, "commentary": "This is a great movie!"},
    }
    result = client.execute(query, variable_values=params)
arielvol commented 4 years ago

Great!!! thank you @KingDarBoja.

Sultan91 commented 3 years ago

Still can't understand it(. Episode is it a type?, why do we need "ep": "JEDI",? An what if a mutation has no name, should it work like this: mutation ($ep: Episode!, $review: ReviewInput!)

KingDarBoja commented 3 years ago

Episode is a enum type, that's why you need to specify the value between NEWHOPE, EMPIRE or JEDI as you can seen at: https://github.com/graphql-python/gql/blob/master/tests/starwars/schema.py#L32