Skip to content

Commit

Permalink
Merge pull request #48 from mirumee/fix-47-handle-undefined-vars
Browse files Browse the repository at this point in the history
Handle unset variables in schema proxy
  • Loading branch information
rafalp authored Feb 8, 2024
2 parents 6666667 + 2417cad commit ba59564
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
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

0 comments on commit ba59564

Please sign in to comment.