@@ -46,7 +46,7 @@ const FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT_WITH_CONNECTOR =
4646const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql' ;
4747const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead' ;
4848const EXECUTE_QUERY_ENDPOINT = 'executeQuery' ;
49- // const EXECUTE_MUTATION_ENDPOINT = 'executeMutation';
49+ const EXECUTE_MUTATION_ENDPOINT = 'executeMutation' ;
5050
5151const DATA_CONNECT_CONFIG_HEADERS = {
5252 'X-Firebase-Client' : `fire-admin-node/${ utils . getSdkVersion ( ) } `
@@ -81,7 +81,8 @@ export class DataConnectApiClient {
8181 query : string ,
8282 options ?: GraphqlOptions < Variables > ,
8383 ) : 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 ) ;
8586 }
8687
8788 /**
@@ -100,15 +101,28 @@ export class DataConnectApiClient {
100101 return this . executeHelper ( EXECUTE_GRAPH_QL_READ_ENDPOINT , options , query ) ;
101102 }
102103
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
105109 */
106110 public async executeQuery < Data , Variables > (
107111 options : GraphqlOptions < Variables > ,
108112 ) : Promise < ExecuteGraphqlResponse < Data > > {
109- // const {data} = await this.executeHelper(options.operationName!, EXECUTE_QUERY_ENDPOINT, options);
110113 return this . executeHelper ( EXECUTE_QUERY_ENDPOINT , options ) ;
111114}
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+ }
112126
113127 private async executeHelper < GraphqlResponse , Variables > (
114128 endpoint : string ,
@@ -136,8 +150,6 @@ export class DataConnectApiClient {
136150 query : gql ,
137151 ...( ! gql && { name : options ?. operationName } ) ,
138152 ...( 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?
141153 ...( options ?. operationName && { operationName : options ?. operationName } ) ,
142154 ...( options ?. impersonate && { extensions : { impersonate : options ?. impersonate } } ) ,
143155 } ;
@@ -168,55 +180,6 @@ export class DataConnectApiClient {
168180 } ) ;
169181 }
170182
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-
220183 private async getUrl ( version : string , locationId : string , serviceId : string , endpointId : string , connector ?: string ) : Promise < string > {
221184 return this . getProjectId ( )
222185 . then ( ( projectId ) => {
0 commit comments