|
33 | 33 | """The purpuse of this module is batch and execute a graphql transaction, such |
34 | 34 | as a query or mutation. |
35 | 35 | """ |
| 36 | + |
| 37 | + |
36 | 38 | class InvalidMutationException(Exception): |
37 | | - """This class is to define the InvalidMutationException |
38 | | - """ |
39 | | - pass |
| 39 | + """This class is to define the InvalidMutationException |
| 40 | + """ |
| 41 | + pass |
| 42 | + |
40 | 43 |
|
41 | 44 | class MutationBatch: |
42 | | - """This is the mutation batch class, it can generate and execute a batch of |
43 | | - graphql's transaction. |
44 | | -
|
45 | | - Args: |
46 | | - client (GraphQLClient Object, optional): Instance of the GraphQLClient. |
47 | | - Defaults to None. |
48 | | - label (str, optional): Label that will get each transaction of the batch. |
49 | | - Defaults to 'mutation'. |
50 | | -
|
51 | | - Examples: |
52 | | - >>> Batch example: |
53 | | - with gql.batchMutate(label='mut') as batch: |
54 | | - for author in authors: |
55 | | - batch.add( |
56 | | - muts.create_author, { |
57 | | - 'name': author['name'] |
58 | | - } |
59 | | - ) |
60 | | - data, errors = batch.execute() |
61 | | - print(data) |
62 | | -
|
63 | | - >>> Mutation simple example: |
64 | | - mutation { |
65 | | - label_1: mutationName( |
66 | | - param1: valx |
67 | | - param2: valy |
68 | | - ){ |
69 | | - response1 |
70 | | - } |
71 | | - label_2: mutationName2( |
72 | | - param1: valz |
73 | | - ){ |
74 | | - response2 |
75 | | - } |
76 | | - } |
77 | | -
|
78 | | - >>> Mutation complex example: |
79 | | - mutation MutationAlias( |
80 | | - >>>$param1: Type1 |
81 | | - $param2: Type2 |
82 | | - $param3: Type3 |
83 | | - ){ |
84 | | - label_1: mutationName( |
85 | | - param1: $param1 |
86 | | - param2: $param2 |
87 | | - ){ |
88 | | - response1 |
| 45 | + """This is the mutation batch class, it can generate and execute a batch of |
| 46 | + graphql's transaction. |
| 47 | +
|
| 48 | + Args: |
| 49 | + client (GraphQLClient Object, optional): Instance of the GraphQLClient. |
| 50 | + Defaults to None. |
| 51 | + label (str, optional): Label that will get each transaction of the batch. |
| 52 | + Defaults to 'mutation'. |
| 53 | +
|
| 54 | + Examples: |
| 55 | + >>> Batch example: |
| 56 | + with gql.batchMutate(label='mut') as batch: |
| 57 | + for author in authors: |
| 58 | + batch.add( |
| 59 | + muts.create_author, { |
| 60 | + 'name': author['name'] |
| 61 | + } |
| 62 | + ) |
| 63 | + data, errors = batch.execute() |
| 64 | + # Process data and errors here |
| 65 | +
|
| 66 | + >>> Mutation simple example: |
| 67 | + mutation { |
| 68 | + label_1: mutationName( |
| 69 | + param1: valx |
| 70 | + param2: valy |
| 71 | + ){ |
| 72 | + response1 |
| 73 | + } |
| 74 | + label_2: mutationName2( |
| 75 | + param1: valz |
| 76 | + ){ |
| 77 | + response2 |
| 78 | + } |
89 | 79 | } |
90 | | - label_2: mutationName2( |
91 | | - param1: $param3 |
92 | | - ){ |
93 | | - response2 |
| 80 | +
|
| 81 | + >>> Mutation complex example: |
| 82 | + mutation MutationAlias( |
| 83 | + >>>$param1: Type1 |
| 84 | + $param2: Type2 |
| 85 | + $param3: Type3 |
| 86 | + ){ |
| 87 | + label_1: mutationName( |
| 88 | + param1: $param1 |
| 89 | + param2: $param2 |
| 90 | + ){ |
| 91 | + response1 |
| 92 | + } |
| 93 | + label_2: mutationName2( |
| 94 | + param1: $param3 |
| 95 | + ){ |
| 96 | + response2 |
| 97 | + } |
94 | 98 | } |
95 | | - } |
96 | | - |
97 | | - """ |
98 | | - def __init__(self, client=None, label='mutation'): |
99 | | - """Constructor of the MutatuibBatch object. |
| 99 | +
|
100 | 100 | """ |
101 | | - self.client = client |
102 | | - self.start_tag = 'mutation BatchMutation {' |
103 | | - self.batch_doc = '' |
104 | | - self.close_tag = '}' |
105 | | - self.label = label |
106 | | - self.count = 1 |
107 | | - |
108 | | - def __enter__(self): |
109 | | - # print(f'setting things up with client {self.client}') |
110 | | - return self |
111 | | - |
112 | | - def __exit__(self, type, value, traceback): |
113 | | - pass # print(f'tear down things in {self.client}') |
114 | | - |
115 | | - def append(self, doc, variables={}): |
116 | | - """This function makes each transactions for the batch. |
117 | 101 |
|
118 | | - Args: |
119 | | - doc (string): GraphQL transaction intructions. |
120 | | - variables (dict, optional): Variables of the transaction. Defaults to {}. |
| 102 | + def __init__(self, client=None, label='mutation'): |
| 103 | + """Constructor of the MutatuibBatch object. |
| 104 | + """ |
| 105 | + self.client = client |
| 106 | + self.start_tag = 'mutation BatchMutation {' |
| 107 | + self.batch_doc = '' |
| 108 | + self.close_tag = '}' |
| 109 | + self.label = label |
| 110 | + self.count = 1 |
121 | 111 |
|
122 | | - Raises: |
123 | | - InvalidMutationException: It raises when the doc is invalid. |
124 | | - """ |
125 | | - # extract document tokens |
126 | | - mp = MutationParser(doc) |
127 | | - valid_doc = mp.parse() |
128 | | - if not valid_doc: |
129 | | - raise InvalidMutationException('Invalid mutation document') |
130 | | - # build batch mutation from extracted tokens |
131 | | - parsed_doc = mp.content |
132 | | - for key, value in variables.items(): |
133 | | - parsed_doc = parsed_doc.replace(f'${key}', mp.format_value(value)) |
134 | | - self.batch_doc += f'\t{self.label}_{self.count}: {parsed_doc}\n' |
135 | | - self.count += 1 |
136 | | - |
137 | | - def get_doc(self): |
138 | | - """This function builds the transaction. |
139 | | -
|
140 | | - Returns: |
141 | | - (string): Returns the full transaction, ready to execute. |
142 | | - """ |
143 | | - full_doc = f'''{self.start_tag}\n{self.batch_doc} {self.close_tag}''' |
144 | | - return full_doc |
| 112 | + def __enter__(self): |
| 113 | + return self |
145 | 114 |
|
146 | | - def execute(self): |
147 | | - """This function can execute a TransactionBatch. |
| 115 | + def __exit__(self, type, value, traceback): |
| 116 | + pass |
148 | 117 |
|
149 | | - Returns: |
150 | | - (GraphqlResponse): Returns the Graphql response. |
151 | | - """ |
152 | | - error_dict = {} |
153 | | - data, errors = self.client.mutate(self.get_doc()) |
154 | | - if errors: |
155 | | - error_dict['server'] = errors |
156 | | - if data: |
157 | | - for label, response in data.items(): |
158 | | - error_dict[label] = response.get('messages', []) |
159 | | - return data, error_dict |
| 118 | + def append(self, doc, variables={}): |
| 119 | + """This function makes each transactions for the batch. |
| 120 | +
|
| 121 | + Args: |
| 122 | + doc (string): GraphQL transaction intructions. |
| 123 | + variables (dict, optional): Variables of the transaction. Defaults to {}. |
| 124 | +
|
| 125 | + Raises: |
| 126 | + InvalidMutationException: It raises when the doc is invalid. |
| 127 | + """ |
| 128 | + # extract document tokens |
| 129 | + mp = MutationParser(doc) |
| 130 | + valid_doc = mp.parse() |
| 131 | + if not valid_doc: |
| 132 | + raise InvalidMutationException('Invalid mutation document') |
| 133 | + # build batch mutation from extracted tokens |
| 134 | + parsed_doc = mp.content |
| 135 | + for key, value in variables.items(): |
| 136 | + parsed_doc = parsed_doc.replace(f'${key}', mp.format_value(value)) |
| 137 | + self.batch_doc += f'\t{self.label}_{self.count}: {parsed_doc}\n' |
| 138 | + self.count += 1 |
| 139 | + |
| 140 | + def get_doc(self): |
| 141 | + """This function builds the transaction. |
| 142 | +
|
| 143 | + Returns: |
| 144 | + (string): Returns the full transaction, ready to execute. |
| 145 | + """ |
| 146 | + full_doc = f'''{self.start_tag}\n{self.batch_doc} {self.close_tag}''' |
| 147 | + return full_doc |
| 148 | + |
| 149 | + def execute(self): |
| 150 | + """This function can execute a TransactionBatch. |
| 151 | +
|
| 152 | + Returns: |
| 153 | + (GraphqlResponse): Returns the Graphql response. |
| 154 | + """ |
| 155 | + error_dict = {} |
| 156 | + data, errors = self.client.mutate(self.get_doc()) |
| 157 | + if errors: |
| 158 | + error_dict['server'] = errors |
| 159 | + if data: |
| 160 | + for label, response in data.items(): |
| 161 | + error_dict[label] = response.get('messages', []) |
| 162 | + return data, error_dict |
0 commit comments