@@ -46,7 +46,7 @@ const FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT_WITH_CONNECTOR =
46
46
const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql' ;
47
47
const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead' ;
48
48
const EXECUTE_QUERY_ENDPOINT = 'executeQuery' ;
49
- // const EXECUTE_MUTATION_ENDPOINT = 'executeMutation';
49
+ const EXECUTE_MUTATION_ENDPOINT = 'executeMutation' ;
50
50
51
51
const DATA_CONNECT_CONFIG_HEADERS = {
52
52
'X-Firebase-Client' : `fire-admin-node/${ utils . getSdkVersion ( ) } `
@@ -81,7 +81,8 @@ export class DataConnectApiClient {
81
81
query : string ,
82
82
options ?: GraphqlOptions < Variables > ,
83
83
) : Promise < ExecuteGraphqlResponse < GraphqlResponse > > {
84
- return this . executeGraphqlHelper ( query , EXECUTE_GRAPH_QL_ENDPOINT , options ) ;
84
+ // return this.executeGraphqlHelper(query, EXECUTE_GRAPH_QL_ENDPOINT, options);
85
+ return this . executeHelper ( EXECUTE_GRAPH_QL_ENDPOINT , options , query ) ;
85
86
}
86
87
87
88
/**
@@ -100,15 +101,28 @@ export class DataConnectApiClient {
100
101
return this . executeHelper ( EXECUTE_GRAPH_QL_READ_ENDPOINT , options , query ) ;
101
102
}
102
103
103
- /**
104
- * Uses the name and the variables parameters to execute a query.
104
+ /**
105
+ * Execute pre-existing <QueryResult<Data, Variables>> read-only queries
106
+ * @param options - GraphQL Options
107
+ * @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
108
+ * @throws FirebaseDataConnectError
105
109
*/
106
110
public async executeQuery < Data , Variables > (
107
111
options : GraphqlOptions < Variables > ,
108
112
) : Promise < ExecuteGraphqlResponse < Data > > {
109
- // const {data} = await this.executeHelper(options.operationName!, EXECUTE_QUERY_ENDPOINT, options);
110
113
return this . executeHelper ( EXECUTE_QUERY_ENDPOINT , options ) ;
111
114
}
115
+ /**
116
+ * Execute pre-existing <MutationResult<Data, Variables>> read and write queries
117
+ * @param options - GraphQL Options
118
+ * @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
119
+ * @throws FirebaseDataConnectError
120
+ */
121
+ public async executeMutation < Data , Variables > (
122
+ options : GraphqlOptions < Variables > ,
123
+ ) : Promise < ExecuteGraphqlResponse < Data > > {
124
+ return this . executeHelper ( EXECUTE_MUTATION_ENDPOINT , options ) ;
125
+ }
112
126
113
127
private async executeHelper < GraphqlResponse , Variables > (
114
128
endpoint : string ,
@@ -136,8 +150,6 @@ export class DataConnectApiClient {
136
150
query : gql ,
137
151
...( ! gql && { name : options ?. operationName } ) ,
138
152
...( options ?. variables && { variables : options ?. variables } ) ,
139
- //change to if query != operationName for executeQuery and executeMutation
140
- //Also how was this needed in conjuncton with executeGraphql before? Just the name of an operation normally doesn't that mean this is how it was used before?
141
153
...( options ?. operationName && { operationName : options ?. operationName } ) ,
142
154
...( options ?. impersonate && { extensions : { impersonate : options ?. impersonate } } ) ,
143
155
} ;
@@ -168,55 +180,6 @@ export class DataConnectApiClient {
168
180
} ) ;
169
181
}
170
182
171
- private async executeGraphqlHelper < GraphqlResponse , Variables > (
172
- query : string ,
173
- endpoint : string ,
174
- options ?: GraphqlOptions < Variables > ,
175
- ) : Promise < ExecuteGraphqlResponse < GraphqlResponse > > {
176
- if ( ! validator . isNonEmptyString ( query ) ) {
177
- throw new FirebaseDataConnectError (
178
- DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
179
- '`query` must be a non-empty string.' ) ;
180
- }
181
- if ( typeof options !== 'undefined' ) {
182
- if ( ! validator . isNonNullObject ( options ) ) {
183
- throw new FirebaseDataConnectError (
184
- DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
185
- 'GraphqlOptions must be a non-null object' ) ;
186
- }
187
- }
188
- const data = {
189
- query,
190
- ...( options ?. variables && { variables : options ?. variables } ) ,
191
- ...( options ?. operationName && { operationName : options ?. operationName } ) ,
192
- ...( options ?. impersonate && { extensions : { impersonate : options ?. impersonate } } ) ,
193
- } ;
194
- return this . getUrl ( API_VERSION , this . connectorConfig . location , this . connectorConfig . serviceId , endpoint )
195
- . then ( async ( url ) => {
196
- const request : HttpRequestConfig = {
197
- method : 'POST' ,
198
- url,
199
- headers : DATA_CONNECT_CONFIG_HEADERS ,
200
- data,
201
- } ;
202
- const resp = await this . httpClient . send ( request ) ;
203
- if ( resp . data . errors && validator . isNonEmptyArray ( resp . data . errors ) ) {
204
- const allMessages = resp . data . errors . map ( ( error : { message : any ; } ) => error . message ) . join ( ' ' ) ;
205
- throw new FirebaseDataConnectError (
206
- DATA_CONNECT_ERROR_CODE_MAPPING . QUERY_ERROR , allMessages ) ;
207
- }
208
- return Promise . resolve ( {
209
- data : resp . data . data as GraphqlResponse ,
210
- } ) ;
211
- } )
212
- . then ( ( resp ) => {
213
- return resp ;
214
- } )
215
- . catch ( ( err ) => {
216
- throw this . toFirebaseError ( err ) ;
217
- } ) ;
218
- }
219
-
220
183
private async getUrl ( version : string , locationId : string , serviceId : string , endpointId : string , connector ?: string ) : Promise < string > {
221
184
return this . getProjectId ( )
222
185
. then ( ( projectId ) => {
0 commit comments