-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from lonelyteapot/rework-parameters
Support non-nullable parameters in queries
- Loading branch information
Showing
13 changed files
with
120 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from __future__ import annotations | ||
|
||
from pydantic import Field | ||
|
||
from pygraphic import GQLParameters, GQLQuery, GQLType | ||
|
||
|
||
class Parameters(GQLParameters): | ||
userId: int | ||
|
||
|
||
class User(GQLType): | ||
id: int | ||
username: str | ||
|
||
|
||
class GetUser(GQLQuery, parameters=Parameters): | ||
user: User = Field(id=Parameters.userId) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
|
||
from examples.server import server_schema | ||
|
||
from .get_user import GetUser, Parameters | ||
|
||
|
||
# Generate query string | ||
gql = GetUser.get_query_string() | ||
variables = Parameters(userId=1) | ||
|
||
# Typically you would send an HTTP Post request to a remote server. | ||
# For simplicity, this query is processed locally. | ||
# We dump and load json here to convert parameters from python types to strings. | ||
response = server_schema.execute_sync(gql, json.loads(variables.json())) | ||
|
||
# Handle errors | ||
if response.data is None: | ||
raise Exception("Query failed", response.errors) | ||
|
||
# Parse the data | ||
result = GetUser.parse_obj(response.data) | ||
|
||
# Print validated data | ||
print(result.user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
query GetUser($userId: Int!) { | ||
user(id: $userId) { | ||
id | ||
username | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ scalar Date | |
|
||
type Query { | ||
users(bornAfter: Date = null): [User!]! | ||
user(id: Int!): User! | ||
} | ||
|
||
type User { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from examples.required_parametrized_query.get_user import GetUser, Parameters | ||
from examples.server import server_schema | ||
|
||
|
||
def test_query_string_generation(): | ||
expected = Path("golden_files", "query_parametrized_required.gql").read_text( | ||
"utf-8" | ||
) | ||
assert GetUser.get_query_string() == expected | ||
|
||
|
||
def test_local_query_execution(): | ||
query = GetUser.get_query_string() | ||
variables = Parameters(userId=1) | ||
result = server_schema.execute_sync(query, json.loads(variables.json())) | ||
assert result.errors is None | ||
assert result.data is not None | ||
|
||
|
||
def test_pydantic_object_parsing(): | ||
query = GetUser.get_query_string() | ||
variables = Parameters(userId=1) | ||
result = server_schema.execute_sync(query, json.loads(variables.json())) | ||
assert type(result.data) is dict | ||
result = GetUser.parse_obj(result.data) | ||
|
||
|
||
def test_example(): | ||
from examples.required_parametrized_query.main import result | ||
|
||
assert type(result) is GetUser | ||
assert result.user.id == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters