Reputation: 13
I have a mysql table, which i want to represent using graphQL, I have tried something using ariandne, and i am able to get the rows. Now, I want to introduce functionality of where clause as well, One way could be to have a resolver for each column, but that has its limitations, and can not scale with columns increasing, can you please suggest something.
My code:
class SSREvent(TypedDict):
event_id: int
start_date_time: datetime
start_date_time_UTC: datetime
epg_num: str
duration: timedelta
end_date_time: datetime
end_date_time_UTC: datetime
prog_name: str
def all_ssr_events() -> List[SSREvent]:
connection = mysql.connector.connect(**DB_CONFIG)
try:
with connection.cursor(dictionary=True) as cursor:
query = f"SELECT * FROM SSR_EVENTS"
cursor.execute(query)
data = cursor.fetchall()
result = [SSREvent(**i) for i in data]
return result
except Exception as e:
return e
finally:
connection.close()
EVENT_TYPEDEF = """
scalar GraphQLDateTime
scalar GraphQLDate
scalar GraphQLTime
type SSREvent {
event_id : Int!
start_date_time : GraphQLDateTime!
start_date_time_UTC : GraphQLDateTime!
epg_num : String!
duration : String!
end_date_time : GraphQLDateTime!
end_date_time_UTC : GraphQLDateTime!
prog_name : String!
}
"""
ssr_event_query = ObjectType("SSREvent")
@query.field("ssr_events")
def resolve_ssr_events(_, info: GraphQLResolveInfo) -> list[SSREvent]:
data: list[SSREvent] = all_ssr_events()
return data
this is ariande library, Do I need to change it or something? I need to filter the results based on one or more columns, a feature similar to where clause in sql
Upvotes: 0
Views: 112
Reputation: 20227
The way to do this is in the query resolver, not the field-level resolvers. The query resolver will be responsible for handling the where
clause and selecting the appropriate records from the database. If the query returns all the fields then you won't even need field-level resolvers because all fields will be resolved by the query resolver.
Upvotes: 0