Running GraphQL using Python
Python
In this lesson, we’ll set up Docker Job for two Python libraries for GraphQL.
- Graphene
- Strawberry
Graphene Code
import grapheneclass Query(graphene.ObjectType):hello = graphene.String(name=graphene.String(default_value="World"))def resolve_hello(self, info, name):return 'Hello ' + nameschema = graphene.Schema(query=Query)result = schema.execute('{ hello }')print(result.data['hello']) # "Hello World"
Strawberry Code
import strawberry@strawberry.typeclass Query:@strawberry.fielddef hello(self, name: str = "World") -> str:return f"Hello {name}"schema = strawberry.Schema(query=Query)
Docker Job
Graphene Docker Job
Let’s see what each field in the above job means:
Select Docker Job Type
This is Docker Job Type selection in which we have to select what kind of docker job we are creating.
Default
Job Name
This is just a job name for reference. You can use any name you want to specify for this job.
GrapheneDockerJob
Input File Name
Name of the input file you want to run in code widget. In our case, it is main.py
.
main.py
Input File Name is the name of the file that will compile or run. The one the user will write their code in. Once its name is set that file shows up in the code widget and is used by default.
Run Script
This script runs when we execute the code in the code widget. It is mandatory.
python3 main.py
Now that our Graphene environment is all setup, we make a code widget, and select the job name set for GrapheneDockerJob as illustrated below:
Go ahead and Run the below code. You should see a file named as main.py
in the output window, available for download.
import grapheneclass Query(graphene.ObjectType):hello = graphene.String(name=graphene.String(default_value="World"))def resolve_hello(self, info, name):return 'Hello ' + nameschema = graphene.Schema(query=Query)result = schema.execute('{ hello }')print(result.data['hello']) # "Hello World"
Graphene Code Breakdown
On Line 1 we are importing the required library to run GraphQL.
From Lines 3-7 we are writing a query against which we will fetch data.
On Line9 we are building schema.
On Line 10 we are fetching data within that code.
On Line 11 we are printing the output of that code.
Strawberry Docker Job
Let’s see what each field in the above job means:
Select Docker Job Type
This is Docker Job Type selection in which we have to select what kind of docker job we are creating.
Live
Job Name
This is just a job name for reference. You can use any name you want to specify for this job.
StrawberryDockerJob
Input File Name
Name of the input file you want to run in code widget. In our case, it is main.py
.
main.py
Run Script
This script runs when we execute the code in the code widget. It is mandatory.
strawberry server app
Application Port
We have to specify the port on which we want to run it.
8000
Start Script
This script runs when we execute the Live Widget for the very first time. It is mandatory.
cp app.py /usercode && strawberry server app
Now that our Strawberry environment is all setup, we make a SPA widget, and select the job name set for StrawberryDockerJob as illustrated below:
Select a Docker Job
Go ahead and Run the below code.
import strawberry @strawberry.type class Query: @strawberry.field def hello(self, name: str = "World") -> str: return f"Hello {name}" schema = strawberry.Schema(query=Query)
Strawberry Code Breakdown
On Line 1 we are importing the required library to run GraphQL server.
From Lines 3-7 we are writing a query against which we will fetch data.
On Line9 we are building schema.
Note:
We are running this application through strawberry server.