@@ -32,7 +32,7 @@ const DATA_CONNECT_API_URL_FORMAT =
32
32
'{host}/v1alpha/projects/{projectId}/locations/{locationId}/services/{serviceId}:{endpointId}' ;
33
33
34
34
const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql' ;
35
- // const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead';
35
+ const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead' ;
36
36
37
37
const DATA_CONNECT_CONFIG_HEADERS = {
38
38
'X-Firebase-Client' : `fire-admin-node/${ utils . getSdkVersion ( ) } `
@@ -66,6 +66,29 @@ export class DataConnectApiClient {
66
66
public async executeGraphql < GraphqlResponse , Variables > (
67
67
query : string ,
68
68
options ?: GraphqlOptions < Variables > ,
69
+ ) : Promise < ExecuteGraphqlResponse < GraphqlResponse > > {
70
+ return this . executeGraphqlHelper ( query , EXECUTE_GRAPH_QL_ENDPOINT , options ) ;
71
+ }
72
+
73
+ /**
74
+ * Execute arbitrary read-only GraphQL queries
75
+ *
76
+ * @param query - The GraphQL (read-only) string to be executed.
77
+ * @param options - Options
78
+ * @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
79
+ * @throws FirebaseDataConnectError
80
+ */
81
+ public async executeGraphqlRead < GraphqlResponse , Variables > (
82
+ query : string ,
83
+ options ?: GraphqlOptions < Variables > ,
84
+ ) : Promise < ExecuteGraphqlResponse < GraphqlResponse > > {
85
+ return this . executeGraphqlHelper ( query , EXECUTE_GRAPH_QL_READ_ENDPOINT , options ) ;
86
+ }
87
+
88
+ private async executeGraphqlHelper < GraphqlResponse , Variables > (
89
+ query : string ,
90
+ endpoint : string ,
91
+ options ?: GraphqlOptions < Variables > ,
69
92
) : Promise < ExecuteGraphqlResponse < GraphqlResponse > > {
70
93
if ( ! validator . isNonEmptyString ( query ) ) {
71
94
throw new FirebaseDataConnectError (
@@ -79,16 +102,17 @@ export class DataConnectApiClient {
79
102
}
80
103
}
81
104
const host = ( process . env . DATA_CONNECT_EMULATOR_HOST || DATA_CONNECT_HOST ) ;
82
- return this . getUrl ( host , this . connectorConfig . location , this . connectorConfig . serviceId , EXECUTE_GRAPH_QL_ENDPOINT )
105
+ const data = {
106
+ query,
107
+ ...( options ?. variables && { variables : options ?. variables } ) ,
108
+ } ;
109
+ return this . getUrl ( host , this . connectorConfig . location , this . connectorConfig . serviceId , endpoint )
83
110
. then ( async ( url ) => {
84
111
const request : HttpRequestConfig = {
85
112
method : 'POST' ,
86
113
url,
87
114
headers : DATA_CONNECT_CONFIG_HEADERS ,
88
- data : {
89
- query,
90
- ...( options ?. variables && { variables : options ?. variables } ) ,
91
- }
115
+ data,
92
116
} ;
93
117
const resp = await this . httpClient . send ( request ) ;
94
118
return Promise . resolve ( {
@@ -103,7 +127,7 @@ export class DataConnectApiClient {
103
127
} ) ;
104
128
}
105
129
106
- private getUrl ( host : string , locationId : string , serviceId : string , endpointId : string ) : Promise < string > {
130
+ private async getUrl ( host : string , locationId : string , serviceId : string , endpointId : string ) : Promise < string > {
107
131
return this . getProjectId ( )
108
132
. then ( ( projectId ) => {
109
133
const urlParams = {
0 commit comments