Skip to content

Commit 10fb9f7

Browse files
committed
Graphql implementation
1 parent 82a8a30 commit 10fb9f7

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

main.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from fastapi import FastAPI
2+
from typing import List
3+
from pymongo import MongoClient
4+
import strawberry
5+
from strawberry.asgi import GraphQL
6+
7+
app = FastAPI()
8+
9+
# Connect to MongoDB
10+
client = MongoClient("mongodb://localhost:27017/")
11+
db = client["CRUD"]
12+
collection = db["users"]
13+
14+
15+
# Define the User model
16+
@strawberry.type
17+
class User:
18+
id: int
19+
name: str
20+
21+
22+
# Define the root resolver
23+
@strawberry.type
24+
class Query:
25+
@strawberry.field
26+
def users(self) -> List[User]:
27+
users = []
28+
for user_data in collection.find():
29+
user = User(id=user_data["id"], name=user_data["name"])
30+
users.append(user)
31+
return users
32+
33+
@strawberry.field
34+
def user(self, id: int) -> User:
35+
user_data = collection.find_one({"id": id})
36+
if user_data:
37+
return User(id=user_data["id"], name=user_data["name"])
38+
else:
39+
return None
40+
41+
42+
@strawberry.type
43+
class Mutation:
44+
@strawberry.mutation
45+
def create_user(self, name: str) -> User:
46+
user = {"id": get_next_id(), "name": name}
47+
collection.insert_one(user)
48+
return User(id=user["id"], name=user["name"])
49+
50+
@strawberry.mutation
51+
def update_user(self, id: int, name: str) -> User:
52+
collection.update_one({"id": id}, {"$set": {"name": name}})
53+
updated_user = collection.find_one({"id": id})
54+
return User(id=updated_user["id"], name=updated_user["name"])
55+
56+
@strawberry.mutation
57+
def delete_user(self, id: int) -> User:
58+
deleted_user = collection.find_one_and_delete({"id": id})
59+
return User(id=deleted_user["id"], name=deleted_user["name"])
60+
61+
62+
def get_next_id():
63+
last_user = collection.find_one(sort=[("id", -1)])
64+
if last_user:
65+
return last_user["id"] + 1
66+
else:
67+
return 1
68+
69+
70+
schema = strawberry.Schema(query=Query, mutation=Mutation)
71+
app.mount("/graphql", GraphQL(schema, debug=True))

test.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import requests
2+
3+
BASE_URL = "http://127.0.0.1:8000"
4+
5+
6+
def test_create_user():
7+
query = """
8+
mutation {
9+
createUser(name: "Shikha Pandey") {
10+
id
11+
name
12+
}
13+
}
14+
"""
15+
response = requests.post(f"{BASE_URL}/graphql", json={"query": query})
16+
assert response.status_code == 200
17+
data = response.json()
18+
assert "errors" not in data
19+
assert "data" in data
20+
user = data["data"]["createUser"]
21+
assert user["name"] == "Shikha Pandey"
22+
23+
24+
def test_update_user():
25+
query = """
26+
mutation {
27+
updateUser(id: 1, name: "Yogesh Singh") {
28+
id
29+
name
30+
}
31+
}
32+
"""
33+
response = requests.post(f"{BASE_URL}/graphql", json={"query": query})
34+
assert response.status_code == 200
35+
data = response.json()
36+
assert "errors" not in data
37+
assert "data" in data
38+
user = data["data"]["updateUser"]
39+
assert user["name"] == "Yogesh Singh"
40+
41+
42+
def test_delete_user():
43+
query = """
44+
mutation {
45+
deleteUser(id: 1) {
46+
id
47+
name
48+
}
49+
}
50+
"""
51+
response = requests.post(f"{BASE_URL}/graphql", json={"query": query})
52+
assert response.status_code == 200
53+
data = response.json()
54+
assert "errors" not in data
55+
assert "data" in data
56+
user = data["data"]["deleteUser"]
57+
assert user["name"] == "Shikha Pandey"
58+
59+
60+
def test_get_all_users():
61+
query = """
62+
query {
63+
users {
64+
id
65+
name
66+
}
67+
}
68+
"""
69+
response = requests.post(f"{BASE_URL}/graphql", json={"query": query})
70+
assert response.status_code == 200
71+
data = response.json()
72+
assert "errors" not in data
73+
assert "data" in data
74+
users = data["data"]["users"]
75+
print(users)
76+
assert len(users) > 0
77+
78+
79+
def test_get_user_by_id():
80+
query = """
81+
query {
82+
user(id: 1) {
83+
id
84+
name
85+
}
86+
}
87+
"""
88+
response = requests.post(f"{BASE_URL}/graphql", json={"query": query})
89+
assert response.status_code == 200
90+
data = response.json()
91+
assert "errors" not in data
92+
assert "data" in data
93+
user = data["data"]["user"]
94+
assert len(user) > 0
95+
96+
97+
# Run the test cases
98+
test_create_user()
99+
test_update_user()
100+
test_delete_user()
101+
test_get_all_users()
102+
test_get_user_by_id()

0 commit comments

Comments
 (0)