Skip to content

Commit 6bdef60

Browse files
committed
spread GraphqlOptions arguments in OperationRefs and executeOperation functions
1 parent bad9808 commit 6bdef60

File tree

3 files changed

+88
-67
lines changed

3 files changed

+88
-67
lines changed

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { PrefixedFirebaseError } from '../utils/error';
2424
import * as utils from '../utils/index';
2525
import * as validator from '../utils/validator';
26-
import { ConnectorConfig, ExecuteGraphqlResponse, GraphqlOptions } from './data-connect-api';
26+
import { ConnectorConfig, ExecuteGraphqlResponse, GraphqlOptions, RefOptions } from './data-connect-api';
2727

2828
const API_VERSION = 'v1';
2929

@@ -152,9 +152,11 @@ export class DataConnectApiClient {
152152
* @returns A promise that fulfills with the GraphQL response.
153153
*/
154154
public async executeQuery<GraphqlResponse, Variables>(
155-
options: GraphqlOptions<Variables>
155+
name: string,
156+
variables: Variables,
157+
options?: RefOptions
156158
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
157-
return this.executeOperationHelper(IMPERSONATE_QUERY_ENDPOINT, options);
159+
return this.executeOperationHelper(IMPERSONATE_QUERY_ENDPOINT, name, variables, options);
158160
}
159161

160162
/**
@@ -164,9 +166,11 @@ export class DataConnectApiClient {
164166
* @returns A promise that fulfills with the GraphQL response.
165167
*/
166168
public async executeMutation<GraphqlResponse, Variables>(
167-
options: GraphqlOptions<Variables>
169+
name: string,
170+
variables: Variables,
171+
options?: RefOptions
168172
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
169-
return this.executeOperationHelper(IMPERSONATE_MUTATION_ENDPOINT, options);
173+
return this.executeOperationHelper(IMPERSONATE_MUTATION_ENDPOINT, name, variables, options);
170174
}
171175

172176
/**
@@ -179,24 +183,17 @@ export class DataConnectApiClient {
179183
*/
180184
private async executeOperationHelper<GraphqlResponse, Variables>(
181185
endpoint: string,
182-
options: GraphqlOptions<Variables>
186+
name: string,
187+
variables: Variables,
188+
options?: RefOptions
183189
): Promise<ExecuteGraphqlResponse<GraphqlResponse>> {
184190
if (
185-
typeof options.operationName === 'undefined' ||
186-
!validator.isNonEmptyString(options.operationName)
191+
typeof name === 'undefined' ||
192+
!validator.isNonEmptyString(name)
187193
) {
188194
throw new FirebaseDataConnectError(
189195
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
190-
'`options.operationName` must be a non-empty string.'
191-
);
192-
}
193-
if (
194-
typeof options.impersonate === 'undefined' ||
195-
!validator.isNonNullObject(options?.impersonate)
196-
) {
197-
throw new FirebaseDataConnectError(
198-
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
199-
'`options.impersonate` must be a non-null object.'
196+
'`name` must be a non-empty string.'
200197
);
201198
}
202199

@@ -208,9 +205,9 @@ export class DataConnectApiClient {
208205
}
209206

210207
const data = {
211-
...(options.variables && { variables: options?.variables }),
212-
operationName: options.operationName,
213-
extensions: { impersonate: options.impersonate },
208+
...(variables && { variables: variables }),
209+
operationName: name,
210+
extensions: { impersonate: options?.impersonate },
214211
};
215212
const url = await this.getUrl(
216213
API_VERSION,

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export interface ConnectorConfig {
3333

3434
/**
3535
* Name of the Data Connect connector.
36-
* Required for operations that interact with connectors, such as executeQuery and executeMutation.
3736
*/
3837
connector?: string;
3938
}
@@ -59,7 +58,6 @@ export interface GraphqlOptions<Variables> {
5958

6059
/**
6160
* The name of the GraphQL operation.
62-
* Required for operations that interact with connectors, such as executeQuery and executeMutation.
6361
* Required for operations that interact with services, such as executeGraphql, if
6462
* `query` contains multiple operations.
6563
*/
@@ -72,6 +70,17 @@ export interface GraphqlOptions<Variables> {
7270
impersonate?: ImpersonateAuthenticated | ImpersonateUnauthenticated;
7371
}
7472

73+
/**
74+
* Interface representing options for OperationRefs.
75+
*/
76+
export interface RefOptions {
77+
/**
78+
* If set, impersonate a request with given Firebase Auth context and evaluate the auth
79+
* policies on the operation. If omitted, bypass any defined auth policies.
80+
*/
81+
impersonate?: ImpersonateAuthenticated | ImpersonateUnauthenticated;
82+
}
83+
7584
/**
7685
* Type representing the partial claims of a Firebase Auth token used to evaluate the
7786
* Data Connect auth policy.

src/data-connect/data-connect.ts

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ConnectorConfig,
2626
ExecuteGraphqlResponse,
2727
GraphqlOptions,
28+
RefOptions,
2829
} from './data-connect-api';
2930

3031
export class DataConnectService {
@@ -162,78 +163,97 @@ export class DataConnect {
162163
}
163164

164165
/** @internal */
165-
public executeQuery<Data, Variables>(options: GraphqlOptions<Variables>): Promise<ExecuteGraphqlResponse<Data>> {
166-
return this.client.executeQuery<Data, Variables>(options);
166+
public executeQuery<Data, Variables>(
167+
name: string,
168+
variables: Variables,
169+
options?: RefOptions
170+
): Promise<ExecuteGraphqlResponse<Data>> {
171+
return this.client.executeQuery<Data, Variables>(name, variables, options);
167172
}
168173

169174
/** @internal */
170-
public executeMutation<Data, Variables>(options: GraphqlOptions<Variables>): Promise<ExecuteGraphqlResponse<Data>> {
171-
return this.client.executeMutation<Data, Variables>(options);
175+
public executeMutation<Data, Variables>(
176+
name: string,
177+
variables: Variables,
178+
options?: RefOptions
179+
): Promise<ExecuteGraphqlResponse<Data>> {
180+
return this.client.executeMutation<Data, Variables>(name, variables, options);
172181
}
173182

174183
/**
175184
* Create a reference to a specific "instance" of a named query.
176-
* @param options - Required {@link GraphqlOptions} when executing a GraphQL query.
177-
* @returns an reference to the named query with the specified impersonation and variables.
185+
* @param name - The name of the query.
186+
* @param options - The RefOptions for the query (optional).
187+
* @returns A reference to the named query with the specified impersonation and variables.
178188
*/
179189
public queryRef<Data>(
180-
options: GraphqlOptions<undefined>
190+
name: string,
191+
options?: RefOptions
181192
): QueryRef<Data, undefined>;
182193

183194
/**
184195
* Create a reference to a specific "instance" of a named query.
185-
* @param options - Required {@link GraphqlOptions} when executing a GraphQL query.
186-
* @returns an reference to the named query with the specified impersonation and variables.
196+
* @param name - The name of the query.
197+
* @param variables - The variables for the query. May be optional if the query's variables are optional.
198+
* @param options - The RefOptions for the query (optional).
199+
* @returns A reference to the named query with the specified impersonation and variables.
187200
*/
188201
public queryRef<Data, Variables>(
189-
options: GraphqlOptions<Variables>
202+
name: string,
203+
variables: Variables,
204+
options?: RefOptions
190205
): QueryRef<Data, Variables>;
191206

192207
public queryRef<Data, Variables>(
193-
options: GraphqlOptions<Variables>
208+
name: string,
209+
variables: Variables,
210+
options?: RefOptions
194211
): QueryRef<Data, Variables> {
195212
if (!('connector' in this.connectorConfig)){
196213
throw new FirebaseDataConnectError(
197214
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
198215
`The 'connectorConfig.connector' field used to instantiate your Data Connect
199216
instance must be a non-empty string (the connectorId) when creating a queryRef.`);
200217
}
201-
return new QueryRef(this, options);
218+
return new QueryRef(this, name, variables, options);
202219
}
203220

204221
/**
205222
* Create a reference to a specific "instance" of a named mutation.
206-
* @param options - Required {@link GraphqlOptions} when executing a GraphQL mutation.
207-
* @returns an reference to the named mutation with the specified impersonation and variables.
223+
* @param name - The name of the mutation.
224+
* @param options - The RefOptions for the mutation (optional).
225+
* @returns A reference to the named mutation with the specified impersonation and variables.
208226
*/
209227
public mutationRef<Data>(
210-
options: GraphqlOptions<undefined>
228+
name: string,
229+
options?: RefOptions
211230
): MutationRef<Data, undefined>
212231

213232
/**
214233
* Create a reference to a specific "instance" of a named mutation.
215-
* @param options - Required {@link GraphqlOptions} when executing a GraphQL mutation.
216-
* @returns an reference to the named mutation with the specified impersonation and variables.
234+
* @param name - The name of the mutation.
235+
* @param variables - The variables for the mutation. May be optional if the mutation's variables are optional.
236+
* @param options - The RefOptions for the mutation (optional).
237+
* @returns A reference to the named mutation with the specified impersonation and variables.
217238
*/
218239
public mutationRef<Data, Variables>(
219-
options: GraphqlOptions<Variables>
240+
name: string,
241+
variables: Variables,
242+
options?: RefOptions
220243
): MutationRef<Data, Variables>;
221244

222-
/**
223-
* Create a reference to a specific "instance" of a named mutation.
224-
* @param options - Required {@link GraphqlOptions} when executing a GraphQL mutation.
225-
* @returns an reference to the named mutation with the specified impersonation and variables.
226-
*/
227245
public mutationRef<Data, Variables>(
228-
options: GraphqlOptions<Variables>
246+
name: string,
247+
variables: Variables,
248+
options?: RefOptions
229249
): MutationRef<Data, Variables> {
230250
if (!('connector' in this.connectorConfig)){
231251
throw new FirebaseDataConnectError(
232252
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
233253
`The 'connectorConfig.connector' field used to instantiate your Data Connect
234254
instance must be a non-empty string (the connectorId) when creating a mutationRef.`);
235255
}
236-
return new MutationRef(this, options);
256+
return new MutationRef(this, name, variables, options);
237257
}
238258
}
239259

@@ -244,54 +264,49 @@ interface OperationResult<Data, Variables> {
244264
dataConnect: DataConnect;
245265
}
246266

267+
/**
268+
* The result of executing a query.
269+
*/
247270
export interface QueryResult<Data, Variables> extends OperationResult<Data, Variables> {
248271
ref: QueryRef<Data, Variables>;
249272
}
250273

274+
/**
275+
* The result of executing a mutation.
276+
*/
251277
export interface MutationResult<Data, Variables> extends OperationResult<Data, Variables> {
252278
ref: MutationRef<Data, Variables>;
253279
}
254280

255281
abstract class OperationRef<Data, Variables> {
256282
constructor(
257283
public readonly dataConnect: DataConnect,
258-
public readonly options: GraphqlOptions<Variables>,
259-
) {
260-
if (typeof options.operationName === 'undefined') {
261-
throw new FirebaseDataConnectError(
262-
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
263-
`The 'options.operationName' field must be provided when creating a queryRef
264-
or mutationRef.`);
265-
}
266-
if (typeof options.impersonate === 'undefined') {
267-
throw new FirebaseDataConnectError(
268-
DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT,
269-
`The 'options.impersonate' field must be provided when creating a queryRef
270-
or mutationRef.`);
271-
}
272-
}
284+
public readonly name: string,
285+
public readonly variables: Variables,
286+
public readonly options?: RefOptions
287+
) {}
273288
abstract execute(): Promise<OperationResult<Data, Variables>>;
274289
}
275290

276291
class QueryRef<Data, Variables> extends OperationRef<Data, Variables> {
277292
async execute(): Promise<QueryResult<Data, Variables>> {
278-
const { data } = await this.dataConnect.executeQuery<Data, Variables>(this.options);
293+
const { data } = await this.dataConnect.executeQuery<Data, Variables>(this.name, this.variables, this.options);
279294
return {
280295
ref: this,
281296
data: data,
282-
variables: this.options.variables as Variables,
297+
variables: this.variables,
283298
dataConnect: this.dataConnect
284299
}
285300
}
286301
}
287302

288303
class MutationRef<Data, Variables> extends OperationRef<Data, Variables> {
289304
async execute(): Promise<MutationResult<Data, Variables>> {
290-
const { data } = await this.dataConnect.executeMutation<Data, Variables>(this.options)
305+
const { data } = await this.dataConnect.executeMutation<Data, Variables>(this.name, this.variables, this.options)
291306
return {
292307
ref: this,
293308
data: data,
294-
variables: this.options.variables as Variables,
309+
variables: this.variables,
295310
dataConnect: this.dataConnect
296311
}
297312
}

0 commit comments

Comments
 (0)