|
1 | 1 | import json |
2 | 2 | from unittest.mock import Mock, patch |
3 | 3 |
|
4 | | -from ariadne import ObjectType, make_executable_schema |
| 4 | +from ariadne import ObjectType, gql, make_executable_schema |
5 | 5 | from ariadne.validation import cost_directive |
6 | 6 | from django.test import RequestFactory, TestCase, override_settings |
7 | 7 | from django.urls import ResolverMatch |
@@ -39,11 +39,34 @@ def generate_cost_test_schema(): |
39 | 39 | return make_executable_schema([types, cost_directive], query_bindable) |
40 | 40 |
|
41 | 41 |
|
| 42 | +def generate_schema_with_required_variables(): |
| 43 | + types = gql( |
| 44 | + """ |
| 45 | + type Query { |
| 46 | + person_exists(name: String!): Boolean |
| 47 | + stuff: String |
| 48 | + } |
| 49 | + """ |
| 50 | + ) |
| 51 | + query_bindable = ObjectType("Query") |
| 52 | + |
| 53 | + # Add a resolver for the `person_exists` field |
| 54 | + @query_bindable.field("person_exists") |
| 55 | + def resolve_person_exists(_, info, name): |
| 56 | + return name is not None # Example resolver logic |
| 57 | + |
| 58 | + return make_executable_schema(types, query_bindable) |
| 59 | + |
| 60 | + |
42 | 61 | class AriadneViewTestCase(GraphQLTestHelper, TestCase): |
43 | | - async def do_query(self, schema, query="{ failing }"): |
| 62 | + async def do_query(self, schema, query="{ failing }", variables=None): |
44 | 63 | view = AsyncGraphqlView.as_view(schema=schema) |
| 64 | + data = {"query": query} |
| 65 | + if variables is not None: |
| 66 | + data["variables"] = variables |
| 67 | + |
45 | 68 | request = RequestFactory().post( |
46 | | - "/graphql/gh", {"query": query}, content_type="application/json" |
| 69 | + "/graphql/gh", data, content_type="application/json" |
47 | 70 | ) |
48 | 71 | match = ResolverMatch(func=lambda: None, args=(), kwargs={"service": "github"}) |
49 | 72 |
|
@@ -256,3 +279,33 @@ def test_client_ip_from_remote_addr(self): |
256 | 279 |
|
257 | 280 | result = view.get_client_ip(request) |
258 | 281 | assert result == "lol" |
| 282 | + |
| 283 | + async def test_required_variable_present(self): |
| 284 | + schema = generate_schema_with_required_variables() |
| 285 | + |
| 286 | + query = """ |
| 287 | + query ($name: String!) { |
| 288 | + person_exists(name: $name) |
| 289 | + } |
| 290 | + """ |
| 291 | + |
| 292 | + # Provide the variable |
| 293 | + data = await self.do_query(schema, query=query, variables={"name": "Bob"}) |
| 294 | + |
| 295 | + assert data is not None |
| 296 | + assert "data" in data |
| 297 | + assert data["data"]["person_exists"] is True |
| 298 | + |
| 299 | + async def test_required_variable_missing(self): |
| 300 | + schema = generate_schema_with_required_variables() |
| 301 | + |
| 302 | + query = """ |
| 303 | + query ($name: String!) { |
| 304 | + person_exists(name: $name) |
| 305 | + } |
| 306 | + """ |
| 307 | + |
| 308 | + # Don't provide the variable |
| 309 | + data = await self.do_query(schema, query=query, variables={}) |
| 310 | + |
| 311 | + assert data == {"detail": "Missing required variables: name", "status": 400} |
0 commit comments