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

Handle unset variables in schema proxy #48

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## UNRELEASED

- Added `CacheSerializer`, `NoopCacheSerializer` and `JSONCacheSerializer`. Changed `CacheBackend`, `InMemoryCache`, `CloudflareCacheBackend` and `DynamoDBCacheBackend` to accept `serializer` initialization option.
- Fixed schema proxy returning an error when variable defined in an operation is missing from its variables.


## 0.2.0 (2023-09-25)
Expand Down
10 changes: 7 additions & 3 deletions ariadne_graphql_proxy/proxy_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,13 @@ async def root_resolver(
"operationName": operation_name,
"query": print_ast(query_document),
"variables": (
variables
if not variables
else {key: variables[key] for key in query_variables}
{
key: variables[key]
for key in query_variables
if key in variables
}
if variables
else None
),
},
)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_proxy_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,44 @@ async def test_proxy_schema_splits_variables_from_fragments_between_schemas(
}


@pytest.mark.asyncio
async def test_proxy_schema_handles_omitted_optional_variables(
httpx_mock,
schema_json,
):
httpx_mock.add_response(url="http://graphql.example.com/", json=schema_json)

proxy_schema = ProxySchema()
proxy_schema.add_remote_schema("http://graphql.example.com/")
proxy_schema.get_final_schema()

await proxy_schema.root_resolver(
{},
"TestQuery",
{"arg": "test"},
parse(
"""
query TestQuery($arg: Generic, $other: Generic) {
basic(arg: $arg, other: $other)
}
"""
),
)

request = httpx_mock.get_requests(url="http://graphql.example.com/")[-1]
assert json.loads(request.content) == {
"operationName": "TestQuery",
"variables": {"arg": "test"},
"query": dedent(
"""
query TestQuery($arg: Generic, $other: Generic) {
basic(arg: $arg, other: $other)
}
"""
).strip(),
}


def test_insert_field_adds_field_into_local_schemas_with_given_type(
schema, complex_schema
):
Expand Down
Loading