2
2
3
3
from django .test import TestCase , Client
4
4
5
+ DEFAULT_GRAPHQL_URL = "/graphql/"
6
+
7
+
8
+ def graphql_query (
9
+ query ,
10
+ op_name = None ,
11
+ input_data = None ,
12
+ variables = None ,
13
+ headers = None ,
14
+ client = None ,
15
+ graphql_url = None ,
16
+ ):
17
+ """
18
+ Args:
19
+ query (string) - GraphQL query to run
20
+ op_name (string) - If the query is a mutation or named query, you must
21
+ supply the op_name. For annon queries ("{ ... }"),
22
+ should be None (default).
23
+ input_data (dict) - If provided, the $input variable in GraphQL will be set
24
+ to this value. If both ``input_data`` and ``variables``,
25
+ are provided, the ``input`` field in the ``variables``
26
+ dict will be overwritten with this value.
27
+ variables (dict) - If provided, the "variables" field in GraphQL will be
28
+ set to this value.
29
+ headers (dict) - If provided, the headers in POST request to GRAPHQL_URL
30
+ will be set to this value.
31
+ client (django.test.Client) - Test client. Defaults to django.test.Client.
32
+ graphql_url (string) - URL to graphql endpoint. Defaults to "/graphql".
33
+
34
+ Returns:
35
+ Response object from client
36
+ """
37
+ if client is None :
38
+ client = Client ()
39
+ if not graphql_url :
40
+ graphql_url = DEFAULT_GRAPHQL_URL
41
+
42
+ body = {"query" : query }
43
+ if op_name :
44
+ body ["operationName" ] = op_name
45
+ if variables :
46
+ body ["variables" ] = variables
47
+ if input_data :
48
+ if variables in body :
49
+ body ["variables" ]["input" ] = input_data
50
+ else :
51
+ body ["variables" ] = {"input" : input_data }
52
+ if headers :
53
+ resp = client .post (
54
+ graphql_url , json .dumps (body ), content_type = "application/json" , ** headers
55
+ )
56
+ else :
57
+ resp = client .post (
58
+ graphql_url , json .dumps (body ), content_type = "application/json"
59
+ )
60
+ return resp
61
+
5
62
6
63
class GraphQLTestCase (TestCase ):
7
64
"""
8
65
Based on: https://www.sam.today/blog/testing-graphql-with-graphene-django/
9
66
"""
10
67
11
68
# URL to graphql endpoint
12
- GRAPHQL_URL = "/graphql/"
13
- # Here you need to set your graphql schema for the tests
14
- GRAPHQL_SCHEMA = None
69
+ GRAPHQL_URL = DEFAULT_GRAPHQL_URL
15
70
16
71
@classmethod
17
72
def setUpClass (cls ):
18
73
super (GraphQLTestCase , cls ).setUpClass ()
19
74
20
- if not cls .GRAPHQL_SCHEMA :
21
- raise AttributeError (
22
- "Variable GRAPHQL_SCHEMA not defined in GraphQLTestCase."
23
- )
24
-
25
75
cls ._client = Client ()
26
76
27
77
def query (self , query , op_name = None , input_data = None , variables = None , headers = None ):
@@ -43,28 +93,15 @@ def query(self, query, op_name=None, input_data=None, variables=None, headers=No
43
93
Returns:
44
94
Response object from client
45
95
"""
46
- body = {"query" : query }
47
- if op_name :
48
- body ["operationName" ] = op_name
49
- if variables :
50
- body ["variables" ] = variables
51
- if input_data :
52
- if variables in body :
53
- body ["variables" ]["input" ] = input_data
54
- else :
55
- body ["variables" ] = {"input" : input_data }
56
- if headers :
57
- resp = self ._client .post (
58
- self .GRAPHQL_URL ,
59
- json .dumps (body ),
60
- content_type = "application/json" ,
61
- ** headers
62
- )
63
- else :
64
- resp = self ._client .post (
65
- self .GRAPHQL_URL , json .dumps (body ), content_type = "application/json"
66
- )
67
- return resp
96
+ return graphql_query (
97
+ query ,
98
+ op_name = op_name ,
99
+ input_data = input_data ,
100
+ variables = variables ,
101
+ headers = headers ,
102
+ client = self ._client ,
103
+ graphql_url = self .GRAPHQL_URL ,
104
+ )
68
105
69
106
def assertResponseNoErrors (self , resp ):
70
107
"""
0 commit comments