Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
pip-wheel-metadata/
share/python-wheels/

# Virtual environments
venv/
ENV/
env/
.venv/
.ENV/

# IDEs and editors
.vscode/
.idea/

# Unit test / coverage
.coverage
.coverage.*
.pytest_cache/
htmlcov/
.tox/
.nox/
.cache/
nosetests.xml
coverage.xml

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Pyre
.pyre/

# MyPy
.mypy_cache/

# Jupyter
.ipynb_checkpoints/

# SQLite
*.sqlite3
*.db

# macOS
.DS_Store

# Logs
*.log

# Editor temp files
*~
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fastapi
uvicorn
strawberry-graphql
starlette
32 changes: 20 additions & 12 deletions tutorial1/intro.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from fastapi import FastAPI
import graphene
from starlette.graphql import GraphQLApp
import strawberry
from strawberry.fastapi import GraphQLRouter


class calculator(graphene.ObjectType):
concat=graphene.String(a=graphene.String(),b=graphene.String())
add=graphene.String(a=graphene.Int(),b=graphene.Int())
def resolve_concat(self,info,a,b):
return a+" "+b
def resolve_add(self,info,a,b):
return a+b
@strawberry.type
class calculator:

app=FastAPI()
app.add_route("/",GraphQLApp(schema=graphene.Schema(query=calculator)))

@strawberry.field
def concat(self, a: str, b: str) -> str:
return a + " " + b

@strawberry.field
def add(self, a: int, b: int) -> int:
return a + b


schema = strawberry.Schema(query=calculator)

graphql_app = GraphQLRouter(schema)

app = FastAPI()

app.include_router(graphql_app, prefix="/graphql")
35 changes: 23 additions & 12 deletions tutorial2/usecase1.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import graphene
import strawberry
from fastapi import FastAPI
from starlette.graphql import GraphQLApp
course_name="computer sience"
course_time_year=1
class course(graphene.ObjectType):
name= graphene.String()
duration=graphene.Int()
def resolve_name(self,info):
from strawberry.fastapi import GraphQLRouter

course_name = "computer science"
course_time_year = 1


@strawberry.type
class Query:
@strawberry.field
def name(self) -> str:
return course_name
def resolve_duration(self,into):

@strawberry.field
def duration(self) -> int:
return course_time_year
app=FastAPI()
app.add_route("/graphql",GraphQLApp(schema=graphene.Schema(query=course)))
print(graphene.Schema(query=course))


schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")

print(schema)
46 changes: 25 additions & 21 deletions tutorial2/usecase2.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import graphene
import strawberry
from fastapi import FastAPI
from graphene.types.objecttype import ObjectType
from starlette.graphql import GraphQLApp
from graphene import ObjectType as ot
from graphene import String as st
from graphene import Int as int
from graphene import List as li

data=[
from strawberry.fastapi import GraphQLRouter

data = [
{
"name": "Roni",
"city": "Cologne",
Expand All @@ -27,15 +22,24 @@
}
]

class students(ot):
name=st()
city=st()
country=st()

class person(ot):
student=li(students)
def resolve_student(self,info):
return data
app=FastAPI()
app.add_route("/graphql",GraphQLApp(schema=graphene.Schema(query=person)))
print(graphene.Schema(query=person))
@strawberry.type
class Student:
name: str
city: str
country: str


@strawberry.type
class Query:
@strawberry.field
def student(self) -> list[Student]:
return [Student(**d) for d in data]


schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")

print(schema)
16 changes: 10 additions & 6 deletions tutorial3/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import graphene
import strawberry
from fastapi import FastAPI
from starlette.graphql import GraphQLApp
from mapping import query
from strawberry.fastapi import GraphQLRouter
from .mapping import Query

app=FastAPI()
app.add_route("/graphql",GraphQLApp(schema=graphene.Schema(query=query)))

print(graphene.Schema(query=query))
schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")

print(schema)
16 changes: 9 additions & 7 deletions tutorial3/mapping.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import graphene
from schema import courses
from data import read_data
import strawberry
from .schema import Course
from .data import read_data

class query(graphene.ObjectType):
course=graphene.List(courses)
def resolve_course(self,info):
return read_data()

@strawberry.type
class Query:
@strawberry.field
def course(self) -> list[Course]:
return [Course(**c) for c in read_data()]
12 changes: 7 additions & 5 deletions tutorial3/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import graphene
import strawberry

class courses(graphene.ObjectType):
name=graphene.String()
level=graphene.String()
duration_in_year=graphene.Int()

@strawberry.type
class Course:
name: str
level: str
duration_in_year: int
27 changes: 18 additions & 9 deletions tutorial4/data.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import csv
from pathlib import Path


def read_file():
with open("data.csv")as f1:
data=csv.reader(f1,delimiter=",")
li=[]
i=0
file_path = Path(__file__).resolve().parent / "data.csv"
with open(file_path) as f1:
data = csv.reader(f1, delimiter=",")
li = []
i = 0
for row in data:
if(i>0):
li.append({"name":row[0],"city":row[1],"designation":row[2],"experience_in_year":row[3]})
i=i+1
f1.close()
return(li)
if i > 0:
li.append({
"name": row[0],
"city": row[1],
"designation": row[2],
"experience_in_year": row[3],
})
i += 1

return li
17 changes: 11 additions & 6 deletions tutorial4/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import graphene
import strawberry
from fastapi import FastAPI
from starlette.graphql import GraphQLApp
from mapping import query
from strawberry.fastapi import GraphQLRouter
from .mapping import Query

app=FastAPI()
app.add_route("/graphql",GraphQLApp(schema=graphene.Schema(query=query)))
print(graphene.Schema(query=query))

schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")

print(schema)
16 changes: 9 additions & 7 deletions tutorial4/mapping.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import graphene
from schema import emp
from data import read_file
import strawberry
from .schema import Employee
from .data import read_file

class query(graphene.ObjectType):
employee=graphene.List(emp)
def resolve_employee(self,info):
return read_file()

@strawberry.type
class Query:
@strawberry.field
def employee(self) -> list[Employee]:
return [Employee(**e) for e in read_file()]
14 changes: 8 additions & 6 deletions tutorial4/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import graphene
import strawberry

class emp(graphene.ObjectType):
name=graphene.String()
city=graphene.String()
designation=graphene.String()
experience_in_year=graphene.String()

@strawberry.type
class Employee:
name: str
city: str
designation: str
experience_in_year: str
Loading