Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds GraphQL data client #81

Merged
merged 20 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Empty file added data_access_layer/__init__.py
Empty file.
69 changes: 69 additions & 0 deletions data_access_layer/client.py
jrwils marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import Any, cast

from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport

from settings import DAL_GRAPHQL_AUTH_KEY, DAL_GRAPHQL_ENDPOINT


class _CanvasGQLClient:
"""
This is a GraphQL client that can be used to query home-app in order to fetch data for use in plugins.

Usage Examples:

A query with no parameters:

TEST_QUERY_NO_PARAMS = '''
query PatientsAll {
patients {
edges {
node {
firstName
lastName
birthDate
}
}
}
}
'''

client = _CanvasGQLClient()
result = client.query(TEST_QUERY_NO_PARAMS)
print(result) # returns dictionary

A query with parameters:

TEST_QUERY_WITH_PARAMS = '''
query PatientGet($patientKey: String!) {
patient(patientKey: $patientKey) {
firstName
lastName
birthDate
}
}
'''

client = _CanvasGQLClient()
result = client.query(TEST_QUERY_NO_PARAMS)
print(result)
"""

def __init__(self) -> None:
transport = AIOHTTPTransport(
url=cast(str, DAL_GRAPHQL_ENDPOINT),
headers={"Authorization": f"{cast(str, DAL_GRAPHQL_AUTH_KEY)}"},
)
self.client = Client(transport=transport, fetch_schema_from_transport=True)
csande marked this conversation as resolved.
Show resolved Hide resolved

def query(self, gql_query: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
jrwils marked this conversation as resolved.
Show resolved Hide resolved
if params is None:
query_params = {}
else:
query_params = params

result = self.client.execute(gql(gql_query), variable_values=query_params)
jrwils marked this conversation as resolved.
Show resolved Hide resolved
return result


GQL_CLIENT = _CanvasGQLClient()
3 changes: 3 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ INTEGRATION_TEST_CLIENT_ID=
INTEGRATION_TEST_CLIENT_SECRET=

PLUGIN_RUNNER_DAL_TARGET=localhost:50052

DAL_GRAPHQL_ENDPOINT=http://localhost:8000/graphql
DAL_GRAPHQL_AUTH_KEY=abc123
1 change: 1 addition & 0 deletions plugin_runner/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"canvas_sdk.utils",
"canvas_sdk.views",
"contextlib",
"data_access_layer.client",
"datetime",
"dateutil",
"enum",
Expand Down
681 changes: 680 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ version = "0.1.15"
[tool.poetry.dependencies]
cookiecutter = "*"
cron-converter = "^1.2.1"
gql = {extras = ["all"], version = "^3.5.0"}
grpcio = "^1.60.1"
ipython = "^8.21.0"
jsonschema = "^4.21.1"
Expand Down
3 changes: 3 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
INTEGRATION_TEST_CLIENT_SECRET = os.getenv("INTEGRATION_TEST_CLIENT_SECRET")

PLUGIN_RUNNER_DAL_TARGET = os.getenv("PLUGIN_RUNNER_DAL_TARGET")

DAL_GRAPHQL_ENDPOINT = os.getenv("DAL_GRAPHQL_ENDPOINT")
DAL_GRAPHQL_AUTH_KEY = os.getenv("DAL_GRAPHQL_AUTH_KEY")
jrwils marked this conversation as resolved.
Show resolved Hide resolved
Loading