Support RSocket
Skip to main content

Mutation

In this section we will add an example for Mutation.

Scheme

Update the schema to include a mutation, and a method which will be used to verify the mutation:

type Query {
greeting: Greeting
getMessage: String
}

type Subscription {
greetings: Greeting
}

type Greeting {
message: String
}

type Mutation {
setMessage(message: String): Greeting
}

Server

First we will add the python functionality:

stored_message = ""

async def get_message(*args):
global stored_message
return stored_message


async def set_message(root, _info, message):
global stored_message
stored_message = message
return {
"message": message
}

And connect it to the schema:

schema.query_type.fields['getMessage'].resolve = get_message
schema.mutation_type.fields['setMessage'].resolve = set_message

This is all that is required on the server side

Client

Here is an example of using the mutation from the client:

async def verify_message(graphql: Client, expected_message: str):
response = await graphql.execute_async(
gql("""query { getMessage }""")
)

assert response['getMessage'] == expected_message

print(response)


async def set_message(graphql: Client, message: str):
response = await graphql.execute_async(
gql("""mutation SetMessage($message:String) { setMessage (message: $message ) {message}}"""),
variable_values={"message": message}
)

print(response)