Skip to content

Commit ae8096a

Browse files
committed
convert executeOperation api to OperationRef(...).execute() api
1 parent 23fe1f4 commit ae8096a

File tree

4 files changed

+270
-119
lines changed

4 files changed

+270
-119
lines changed

src/data-connect/data-connect.ts

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*/
1717

1818
import { App } from '../app';
19-
import { DataConnectApiClient } from './data-connect-api-client-internal';
19+
import {
20+
DATA_CONNECT_ERROR_CODE_MAPPING,
21+
DataConnectApiClient,
22+
FirebaseDataConnectError } from './data-connect-api-client-internal';
2023

2124
import {
2225
ConnectorConfig,
@@ -102,7 +105,6 @@ export class DataConnect {
102105
return this.client.executeGraphqlRead(query, options);
103106
}
104107

105-
/**
106108
/**
107109
* Insert a single row into the specified table.
108110
*
@@ -158,4 +160,137 @@ export class DataConnect {
158160
): Promise<ExecuteGraphqlResponse<GraphQlResponse>> {
159161
return this.client.upsertMany(tableName, variables);
160162
}
163+
164+
/**
165+
* Create a reference to a specific "instance" of a named query.
166+
* @param name Name of query
167+
* @param impersonate Impersonation options for this query
168+
* @returns an reference to the named query with the specified impersonation and variables.
169+
*/
170+
public queryRef<Data>(
171+
options: GraphqlOptions<undefined>
172+
): QueryRef<Data, undefined>;
173+
174+
/**
175+
* Create a reference to a specific "instance" of a named query.
176+
* @param name Name of query
177+
* @param impersonate Impersonation options for this query
178+
* @param variables Variables passed to this query, may be omitted.
179+
* @returns an reference to the named query with the specified impersonation and variables.
180+
*/
181+
public queryRef<Data, Variables>(
182+
options: GraphqlOptions<Variables>
183+
): QueryRef<Data, Variables>;
184+
185+
public queryRef<Data, Variables>(
186+
options: GraphqlOptions<Variables>
187+
): QueryRef<Data, Variables> {
188+
if (!('connector' in this.connectorConfig)){
189+
throw new FirebaseDataConnectError(
190+
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
191+
`The 'connectorConfig.connector' field used to instantiate your Data Connect
192+
instance must be a non-empty string (the connectorId) when creating a queryRef.`);
193+
}
194+
return new QueryRef(this, options, this.client);
195+
}
196+
197+
/**
198+
* Create a reference to a specific "instance" of a named mutation.
199+
* @param name Name of mutation
200+
* @param impersonate Impersonation options for this mutation
201+
* @returns an reference to the named mutation with the specified impersonation and variables.
202+
*/
203+
public mutationRef<Data>(
204+
options: GraphqlOptions<undefined>
205+
): MutationRef<Data, undefined>
206+
207+
/**
208+
* Create a reference to a specific "instance" of a named mutation.
209+
* @param name Name of mutation
210+
* @param impersonate Impersonation options for this mutation
211+
* @param variables Variables passed to this mutation, may be omitted.
212+
* @returns an reference to the named mutation with the specified impersonation and variables.
213+
*/
214+
public mutationRef<Data, Variables>(
215+
options: GraphqlOptions<Variables>
216+
): MutationRef<Data, Variables>;
217+
/**
218+
* Create a reference to a specific "instance" of a named mutation.
219+
* @param name Name of mutation
220+
* @param impersonate Impersonation options for this mutation
221+
* @param variables Variables passed to this mutation, may be omitted.
222+
* @returns an reference to the named mutation with the specified impersonation and variables.
223+
*/
224+
public mutationRef<Data, Variables>(
225+
options: GraphqlOptions<Variables>
226+
): MutationRef<Data, Variables> {
227+
if (!('connector' in this.connectorConfig)){
228+
throw new FirebaseDataConnectError(
229+
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
230+
`The 'connectorConfig.connector' field used to instantiate your Data Connect
231+
instance must be a non-empty string (the connectorId) when creating a mutationRef.`);
232+
}
233+
return new MutationRef(this, options, this.client);
234+
}
161235
}
236+
237+
interface OperationResult<Data, Variables> {
238+
ref: OperationRef<Data, Variables>;
239+
data: Data;
240+
variables: Variables;
241+
dataConnect: DataConnect;
242+
}
243+
244+
export interface QueryResult<Data, Variables> extends OperationResult<Data, Variables> {
245+
ref: QueryRef<Data, Variables>;
246+
}
247+
248+
export interface MutationResult<Data, Variables> extends OperationResult<Data, Variables> {
249+
ref: MutationRef<Data, Variables>;
250+
}
251+
252+
abstract class OperationRef<Data, Variables> {
253+
constructor(
254+
public readonly dataConnect: DataConnect,
255+
public readonly options: GraphqlOptions<Variables>,
256+
protected readonly client: DataConnectApiClient
257+
) {
258+
if (typeof options.operationName === 'undefined') {
259+
throw new FirebaseDataConnectError(
260+
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
261+
`The 'options.operationName' field must be provided when creating a queryRef
262+
or mutationRef.`);
263+
}
264+
if (typeof options.impersonate === 'undefined') {
265+
throw new FirebaseDataConnectError(
266+
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
267+
`The 'options.impersonate' field must be provided when creating a queryRef
268+
or mutationRef.`);
269+
}
270+
}
271+
abstract execute(): Promise<OperationResult<Data, Variables>>;
272+
}
273+
274+
class QueryRef<Data, Variables> extends OperationRef<Data, Variables> {
275+
async execute(): Promise<QueryResult<Data, Variables>> {
276+
const { data } = await this.client.executeQuery<Data, Variables>(this.options);
277+
return {
278+
ref: this,
279+
data: data,
280+
variables: this.options.variables as Variables,
281+
dataConnect: this.dataConnect
282+
}
283+
}
284+
}
285+
286+
class MutationRef<Data, Variables> extends OperationRef<Data, Variables> {
287+
async execute(): Promise<MutationResult<Data, Variables>> {
288+
const { data } = await this.client.executeMutation<Data, Variables>(this.options)
289+
return {
290+
ref: this,
291+
data: data,
292+
variables: this.options.variables as Variables,
293+
dataConnect: this.dataConnect
294+
}
295+
}
296+
}

0 commit comments

Comments
 (0)