query
reading data from apis is the most common operation we do, that's why ql
makes it
easy to query data from your graphql endpoint and provide variety of query methods.
ql.query
takes python ql query structure and returns a valid graphql query string.
def query(
*query_models: _QueryModelType,
fragments: Optional[_QueryFragmentType] = None,
include_typename: bool = True,
) -> str:
Name | Type | Description |
---|---|---|
query_models |
*_QueryModelType |
python ql structured query |
fragments |
Optional[_QueryFragmentType] |
dict mapping between ql.fragment to the python ql structured query |
include_typename |
bool |
if include __typename field when querying sub types |
example.py
import ql
from pydantic import BaseModel
@ql.model
class Point(BaseModel):
x: int
y: int
query_str = ql.query(
(Point, (
ql._(Point).x,
ql._(Point).y
))
)
ql.query_response
serializes the ql query structure, send it via http and returns the response as dict
.
def query_response(
*query_models: _QueryModelType,
fragments: Optional[_QueryFragmentType] = {},
include_typename: bool = True,
) -> QueryResponseDict:
http request function must be set to make this function work, click here to view.
ql.query_response_scalar
serializes the ql query structure to a valid graphql query, send it via http, takes the response
and returns a scalared dict with the defined models, this function will also raise
graphql errors if the query responsed with errors
field