I have been going round and round with GraphQL. I am using Flask and need to create a mutation to a GraphQL api. I have been trying to use Graphene but I am having trouble forming my schema correctly.
I am trying to follow Eradash's response but not sure how to form the schema. My original working (hard coded) schema is in mutation.py
and my updated is named updated_mutation.py
. Thank you for any guidance.
mutation.py
def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
request = requests.post('https:/xxxxx.com/api/', json={'query': query}, headers=headers)
if request.status_code == 200:
print(request.json())
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
# The GraphQL query (with a few aditional bits included) itself defined as a multi-line string.
parameter = "Meeting Test"
query = """mutation {
createMeeting(
input: {
meetingName: "Meeting on Calendar - API"
scheduledDateTime: "Thu Jul 30 2020 14:00:00 -0500"
scheduledTimeZone: "CDT/UTC-05:00"
endDateTime: "Thu Jul 30 2020 15:00:00 -0500"
enableTwilio: 1
}
) {
message
result
status
}
}
"""
result = run_query(query) # Execute the query
update_mutation.py
import requests
headers = {"apiToken": "your_token"}
def make_query(self, query, variables, url, headers):
"""
Make query response
"""
request = requests.post(url, json={'query': query, 'variables': variables}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
query = """
mutation createMeeting{($meetingName:MeetingInput,
$scheduledDateTime: starttime,
$scheduledTimeZone: timezone,
$endDateTime: endtime)
}
{
message
result
status
}
"""
variables = {
'meetingName': name,
'scheduledDateTime': starttime,
'scheduledTimeZone': timezone,
'endDateTime': endtime
}