-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnobj_viewchange_test.py
116 lines (91 loc) · 3.18 KB
/
connobj_viewchange_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
USE CASE DESCRIPTION:
This test shows how to create a GraphQL Query to fetch a GraphQL connection type
and build the python class instance containing the data from the response.
In addition it shows how to hide fields of a type in a query.
PREREQUISITES:
A mapped connection Query (see _gdbc_connobjTest.py STEP 1):
GraphQL version
query MyQuery {
currencies {
totalCount (*)
edges {
cursor
node {
countryCodes (*)
code
symbol (*)
}
}
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
}
}
Relating python classes
class currency(GQLObject):
countryCodes: list
code: str
symbol: str
def __init__(self):
self.countryCodes = []
self.code = ''
self.symbol = ''
super().__init__()
class currencies(GQLConnection):
def __init__(self):
super().__init__(GQLEdges(currency()))
OBJECTIVE: Hiding fields from the original python mapped class (above with (*))
STEP 1: Instantiate python class representing the GraphQL query
STEP 2: Call set_show function of the object to set the visibility of field (path to declare with dot notation)
STEP 3: Query the GraphQL server
STEP 4: Pass the response received to the GQLResponse constructor
STEP 5: Call map_gqldata_to_obj() function to obtain the python class with data from GraphQL server
RESULT:
a) The request toward the GraphQL server will not have the hidden fields
b) The python class instance obtained from the response will not have the hidden fields
"""
import requests
from ..consts import GDBC_URL, GDBC_HEADERS
from ..output.gdbc.queries import currencies
import logging as logger
from ..utils import stringifyresult
def run_gdbc_connobj_viewchange():
logger.debug('\n\nRunning run_gdbc_connobj_viewchange...')
##STEP 1
from pygqlmap.enums import OperationType
from pygqlmap.components import GQLOperation
query = currencies()
query.name = 'myCurrenciesQueryVisibility'
##
##STEP 2
query.set_show('currencies.edges.node.symbol', False)
query.set_show('currencies.edges.node.countryCodes', False)
query.set_show('currencies.totalCount', False)
##
try:
##RESULT a)
logger.debug('Query GQL syntax: ' + query.export_gql_source)
##
##STEP 3
response = requests.request('POST', url=GDBC_URL,
json={ "query": query.export_gql_source },
headers=GDBC_HEADERS)
##
##STEP 4
from pygqlmap.network import GQLResponse
gqlResponse = GQLResponse(response)
##
gqlResponse.print_msg_out()
##STEP 5
gqlResponse.map_gqldata_to_obj(query.type)
##
##RESULT b)
logger.info('result object: ' + stringifyresult(gqlResponse.result_obj))
##
except Exception as ex:
raise ex
logger.debug("End of run_gdbc_connobj_viewchange")