Skip to content

Commit c79e05d

Browse files
author
FOLUSO ONATEMOWO
committed
Draft: Implemented the initialization API and 'querRef' function; refactored and renamed the 'executeGraphqlHelper' function to support the 'executeQuery' and 'executeMutation' functions.
1 parent 9ef4dba commit c79e05d

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ test/resources/appid.txt
2020
firebase-admin-*.tgz
2121

2222
docgen/markdown/
23+
service_account.json

dataconnect_test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ const { initializeApp } = require('./lib/app');
44
const app = initializeApp();
55

66
const config = {
7-
serviceId: "your-service-id",
8-
location: "us-central1",
9-
connector:"movie-connector"
7+
serviceId: "<your-service-id>",
8+
location: "<us-central>",
9+
// connector:"<connector?"
1010
};
1111

12-
const dataConnect = getDataConnect({
13-
connectorConfig: config
14-
});
12+
// const dataConnect = getDataConnect(config);
1513

16-
const listOfMovies = dataConnect.queryRef('ListMovies').execute();
14+
// async function queryref_test (){
15+
// // const listOfMovies_res = await dataConnect.queryRef('ListMovies', {orderByRating: "DESC",orderByReleaseYear: "DESC", limit: 3}).execute();
16+
// // const getActorsDetails = await dataConnect.queryRef('GetActorById', {id: "11111111222233334444555555555555"}).execute()
17+
// const graphqlRead_listOfMovies_res = await dataConnect.executeGraphqlRead(" query { movies {id, title}}");
1718

18-
console.log(listOfMovies)
19+
// console.log(graphqlRead_listOfMovies_res.data)
20+
// }
21+
22+
// queryref_test()

src/data-connect/data-connect-api-client-internal.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT =
4141

4242
/** Firebase Data Connect base URl format when using the Data Connect emulator including a connector. */
4343
const FIREBASE_DATA_CONNECT_EMULATOR_BASE_URL_FORMAT_WITH_CONNECTOR =
44-
'http://{host}/{version}/projects/{projectId}/locations/{locationId}/services/{serviceId}/connectors/${connector}:{endpointId}';
44+
'http://{host}/{version}/projects/{projectId}/locations/{locationId}/services/{serviceId}/connectors/{connector}:{endpointId}';
4545

4646
const EXECUTE_GRAPH_QL_ENDPOINT = 'executeGraphql';
4747
const EXECUTE_GRAPH_QL_READ_ENDPOINT = 'executeGraphqlRead';
@@ -96,7 +96,8 @@ export class DataConnectApiClient {
9696
query: string,
9797
options?: GraphqlOptions<Variables>,
9898
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
99-
return this.executeGraphqlHelper(query, EXECUTE_GRAPH_QL_READ_ENDPOINT, options);
99+
// return this.executeGraphqlHelper(query, EXECUTE_GRAPH_QL_READ_ENDPOINT, options);
100+
return this.executeHelper(EXECUTE_GRAPH_QL_READ_ENDPOINT,options, query);
100101
}
101102

102103
/**
@@ -114,17 +115,22 @@ export class DataConnectApiClient {
114115
options?: GraphqlOptions<Variables>,
115116
gql?: string
116117
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
117-
if (!validator.isNonEmptyString(gql)) {
118+
if (!validator.isNonEmptyString(gql) && typeof options == 'undefined') {
118119
throw new FirebaseDataConnectError(
119120
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
120-
'`query` must be a non-empty string.');
121-
}
122-
if (typeof options !== 'undefined') {
121+
'`gql` must be a non-empty string or GraphqlOptions should be a non-null object');
122+
} //How would we steer them in the right direction or let them know which area makes the most sense to follow for what they are trying to accomplish? they might want gql to be empty and the message should say
123+
if (typeof options !== 'undefined' && !validator.isNonEmptyString(gql)) {
123124
if (!validator.isNonNullObject(options)) {
124125
throw new FirebaseDataConnectError(
125126
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
126127
'GraphqlOptions must be a non-null object');
127128
}
129+
if (!("operationName" in options)) {
130+
throw new FirebaseDataConnectError(
131+
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
132+
'`gql` missing thus GraphqlOptions must contain `operationName`.');//Is this too descriptive?
133+
}
128134
}
129135
const data = {
130136
query: gql,
@@ -157,6 +163,7 @@ export class DataConnectApiClient {
157163
return resp;
158164
})
159165
.catch((err) => {
166+
console.log(err)
160167
throw this.toFirebaseError(err);
161168
});
162169
}

src/data-connect/data-connect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class DataConnect {
180180
* @returns QueryRef
181181
*/
182182
public queryRef<Data, Variables>(name: string, variables?: Variables): QueryRef<Data, Variables> {
183-
console.log(this)
183+
// console.log(this)
184184
if (!("connector" in this.connectorConfig)){
185185
throw new FirebaseDataConnectError(DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,'executeQuery requires a connector');
186186
}
@@ -242,7 +242,7 @@ class QueryRef<Data, Variables> extends OperationRef<Data, Variables> {
242242
operationName: this.name
243243
};
244244
const {data} = await this.client.executeQuery<Data, Variables>(option_params)
245-
245+
246246
return {
247247
ref: this,
248248
data: data,

src/data-connect/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ export {
7171
* @returns The default `DataConnect` service with the provided connector configuration
7272
* if no app is provided, or the `DataConnect` service associated with the provided app.
7373
*/
74+
// export function getDataConnect(connectorConfig: ConnectorConfig, app?: App): DataConnect {
75+
// if (typeof app === 'undefined') {
76+
// app = getApp();
77+
// }
78+
79+
// const firebaseApp: FirebaseApp = app as FirebaseApp;
80+
// const dataConnectService = firebaseApp.getOrInitService('dataConnect', (app) => new DataConnectService(app));
81+
// return dataConnectService.getDataConnect(connectorConfig);
82+
// }
7483
export function getDataConnect(connectorConfig: ConnectorConfig, app?: App): DataConnect {
7584
if (typeof app === 'undefined') {
7685
app = getApp();

0 commit comments

Comments
 (0)