Skip to content

Commit cf988b0

Browse files
Fix error caused by undefined "trackedQuery" in QueryManager (#8570)
* Add check for non-query refType in executeQuery argument * Add unit test checking for non-query refType in executeQuery argument * add changeset
1 parent 2e28041 commit cf988b0

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

.changeset/beige-roses-cross.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/data-connect': patch
3+
---
4+
5+
- Throw error when calling `executeQuery` with mutations

packages/data-connect/src/core/QueryManager.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { DataConnectTransport } from '../network';
3737
import { encoderImpl } from '../util/encoder';
3838
import { setIfNotExists } from '../util/map';
3939

40-
import { DataConnectError } from './error';
40+
import { Code, DataConnectError } from './error';
4141

4242
interface TrackedQuery<Data, Variables> {
4343
ref: Omit<OperationRef<Data, Variables>, 'dataConnect'>;
@@ -172,6 +172,12 @@ export class QueryManager {
172172
executeQuery<Data, Variables>(
173173
queryRef: QueryRef<Data, Variables>
174174
): QueryPromise<Data, Variables> {
175+
if (queryRef.refType !== QUERY_STR) {
176+
throw new DataConnectError(
177+
Code.INVALID_ARGUMENT,
178+
`ExecuteQuery can only execute query operation`
179+
);
180+
}
175181
const key = encoderImpl({
176182
name: queryRef.name,
177183
variables: queryRef.variables,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { deleteApp, FirebaseApp, initializeApp } from '@firebase/app';
19+
import { expect } from 'chai';
20+
import * as chai from 'chai';
21+
import chaiAsPromised from 'chai-as-promised';
22+
23+
import {
24+
DataConnect,
25+
executeQuery,
26+
getDataConnect,
27+
mutationRef,
28+
queryRef
29+
} from '../../src';
30+
import { Code, DataConnectError } from '../../src/core/error';
31+
chai.use(chaiAsPromised);
32+
33+
describe('Query Manager Tests', () => {
34+
let dc: DataConnect;
35+
let app: FirebaseApp;
36+
const APPID = 'MYAPPID';
37+
const APPNAME = 'MYAPPNAME';
38+
39+
beforeEach(() => {
40+
app = initializeApp({ projectId: 'p', appId: APPID }, APPNAME);
41+
dc = getDataConnect(app, {
42+
connector: 'c',
43+
location: 'l',
44+
service: 's'
45+
});
46+
});
47+
afterEach(async () => {
48+
await dc._delete();
49+
await deleteApp(app);
50+
});
51+
52+
it('should refuse to make requests to execute non-query operations', async () => {
53+
const query = queryRef<string>(dc, 'q');
54+
const mutation = mutationRef<string>(dc, 'm');
55+
56+
const error = new DataConnectError(
57+
Code.INVALID_ARGUMENT,
58+
`ExecuteQuery can only execute query operation`
59+
);
60+
61+
// @ts-ignore
62+
expect(() => executeQuery(mutation)).to.throw(error.message);
63+
expect(() => executeQuery(query)).to.not.throw(error.message);
64+
});
65+
});

0 commit comments

Comments
 (0)