Skip to content

Commit da6056b

Browse files
authored
Use config to determine path for generated operations (#7890)
* Use config to determine path for generated operations * remove console log * changelog * update test * update other test * changelog format
1 parent 96f8e50 commit da6056b

File tree

4 files changed

+126
-19
lines changed

4 files changed

+126
-19
lines changed

firebase-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## NEXT
22

3+
- [Fixed] Fixed an issue where Add data and Read data would generate operations in the wrong folder
4+
35
## 0.10.6
46

57
- Updated internal firebase-tools dependency to 13.23.1

firebase-vscode/src/data-connect/ad-hoc-mutations.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
import { checkIfFileExists, upsertFile } from "./file-utils";
1717
import { DataConnectService } from "./service";
1818
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
19+
import { dataConnectConfigs } from "./config";
20+
import { firstWhereDefined } from "../utils/signal";
1921

2022
export function registerAdHoc(
2123
dataConnectService: DataConnectService,
@@ -47,6 +49,7 @@ export function registerAdHoc(
4749
async function schemaReadData(
4850
document: DocumentNode,
4951
ast: ObjectTypeDefinitionNode,
52+
documentPath: string,
5053
) {
5154
// TODO(rrousselGit) - this is a temporary solution due to the lack of a "schema".
5255
// As such, we hardcoded the list of allowed primitives.
@@ -62,8 +65,12 @@ export function registerAdHoc(
6265
"Any",
6366
]);
6467

65-
const basePath = vscode.workspace.rootPath + "/dataconnect/";
66-
const filePath = vscode.Uri.file(`${basePath}${ast.name.value}_read.gql`);
68+
const configs = await firstWhereDefined(dataConnectConfigs);
69+
const dataconnectConfig =
70+
configs.tryReadValue?.findEnclosingServiceForPath(documentPath);
71+
72+
const basePath = dataconnectConfig?.path;
73+
const filePath = vscode.Uri.file(`${basePath}/${ast.name.value}_read.gql`);
6774

6875
// Recursively build a query for the object type.
6976
// Returns undefined if the query is empty.
@@ -136,12 +143,20 @@ query {
136143
* File will be created (unsaved) in operations/ folder, with an auto-generated named based on the schema type
137144
* Mutation will be generated with all
138145
* */
139-
async function schemaAddData(ast: ObjectTypeDefinitionNode) {
146+
async function schemaAddData(
147+
ast: ObjectTypeDefinitionNode,
148+
documentPath: string,
149+
) {
140150
// generate content for the file
141151
const preamble =
142152
"# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file.";
143-
const introspect = (await dataConnectService.introspect())?.data;
144-
const schema = buildClientSchema(introspect!);
153+
154+
const introspect = await dataConnectService.introspect();
155+
if (!introspect.data) {
156+
vscode.window.showErrorMessage("Failed to generate mutation. Please check your compilation errors.");
157+
return;
158+
}
159+
const schema = buildClientSchema(introspect.data);
145160
const dataType = schema.getType(`${ast.name.value}_Data`);
146161
if (!isInputObjectType(dataType)) return;
147162

@@ -152,8 +167,14 @@ query {
152167
),
153168
);
154169
const content = [preamble, adhocMutation].join("\n");
155-
const basePath = vscode.workspace.rootPath + "/dataconnect/";
156-
const filePath = vscode.Uri.file(`${basePath}${ast.name.value}_insert.gql`);
170+
171+
// get root where dataconnect.yaml lives
172+
const configs = await firstWhereDefined(dataConnectConfigs);
173+
const dataconnectConfig =
174+
configs.tryReadValue?.findEnclosingServiceForPath(documentPath);
175+
const basePath = dataconnectConfig?.path;
176+
177+
const filePath = vscode.Uri.file(`${basePath}/${ast.name.value}_insert.gql`);
157178
const doesFileExist = await checkIfFileExists(filePath);
158179

159180
if (!doesFileExist) {
@@ -202,7 +223,10 @@ query {
202223
selections: [
203224
{
204225
kind: Kind.FIELD,
205-
name: { kind: Kind.NAME, value: `${singularName.charAt(0).toLowerCase()}${singularName.slice(1)}_insert` },
226+
name: {
227+
kind: Kind.NAME,
228+
value: `${singularName.charAt(0).toLowerCase()}${singularName.slice(1)}_insert`,
229+
},
206230
arguments: [
207231
{
208232
kind: Kind.ARGUMENT,
@@ -251,16 +275,16 @@ query {
251275
return Disposable.from(
252276
vscode.commands.registerCommand(
253277
"firebase.dataConnect.schemaAddData",
254-
(ast) => {
278+
(ast, uri) => {
255279
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.ADD_DATA);
256-
schemaAddData(ast);
280+
schemaAddData(ast, uri);
257281
},
258282
),
259283
vscode.commands.registerCommand(
260284
"firebase.dataConnect.schemaReadData",
261-
(document, ast) => {
285+
(document, ast, uri) => {
262286
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.READ_DATA);
263-
schemaReadData(document, ast);
287+
schemaReadData(document, ast, uri);
264288
},
265289
),
266290
);

firebase-vscode/src/data-connect/code-lens-provider.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,14 @@ export class SchemaCodeLensProvider extends ComputedCodeLensProvider {
150150
if (x.kind === Kind.OBJECT_TYPE_DEFINITION && x.loc) {
151151
const line = x.loc.startToken.line - 1;
152152
const range = new vscode.Range(line, 0, line, 0);
153-
const position = new vscode.Position(line, 0);
154-
const schemaLocation = {
155-
documentPath: document.fileName,
156-
position: position,
157-
};
153+
const documentPath = document.fileName;
158154

159155
codeLenses.push(
160156
new vscode.CodeLens(range, {
161157
title: `$(database) Add data`,
162158
command: "firebase.dataConnect.schemaAddData",
163159
tooltip: "Generate a mutation to add data of this type",
164-
arguments: [x, schemaLocation],
160+
arguments: [x, documentPath],
165161
}),
166162
);
167163

@@ -170,7 +166,7 @@ export class SchemaCodeLensProvider extends ComputedCodeLensProvider {
170166
title: `$(database) Read data`,
171167
command: "firebase.dataConnect.schemaReadData",
172168
tooltip: "Generate a query to read data of this type",
173-
arguments: [documentNode, x],
169+
arguments: [documentNode, x, documentPath],
174170
}),
175171
);
176172
}

firebase-vscode/src/test/integration/fishfood/graphql.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,89 @@ firebaseSuite("GraphQL", async function () {
201201
expect(editorTitle).toBe("Post_read.gql");
202202
},
203203
);
204+
205+
firebaseTest(
206+
"Add Data should generate file in correct folder",
207+
async function () {
208+
const workbench = await browser.getWorkbench();
209+
210+
const sidebar = new FirebaseSidebar(workbench);
211+
await sidebar.openExtensionSidebar();
212+
213+
const schemaFilePath = path.join(
214+
__dirname,
215+
"..",
216+
"..",
217+
"test_projects",
218+
"fishfood",
219+
"dataconnect",
220+
"schema",
221+
"schema.gql",
222+
);
223+
224+
// Open the schema file
225+
const editorView = new EditorView(workbench);
226+
await editorView.openFile(schemaFilePath);
227+
228+
// Verify that inline Add Data button is displayed
229+
const addDataButton = await editorView.addDataButton;
230+
await addDataButton.waitForDisplayed();
231+
232+
// Click the Add Data button
233+
await addDataButton.click();
234+
235+
// Wait a bit for the mutation to be generated
236+
await browser.pause(1500);
237+
238+
// Verify the generated mutation file path
239+
const activeEditor = await editorView.getActiveEditor();
240+
const filePath = activeEditor?.document.fileName;
241+
expect(filePath).toContain(
242+
"test_projects/fishfood/dataconnect/Post_insert.gql",
243+
);
244+
245+
await editorView.closeCurrentEditor();
246+
},
247+
);
248+
249+
firebaseTest(
250+
"Read Data should generate file in correct folder",
251+
async function () {
252+
const workbench = await browser.getWorkbench();
253+
254+
const sidebar = new FirebaseSidebar(workbench);
255+
await sidebar.openExtensionSidebar();
256+
257+
const schemaFilePath = path.join(
258+
__dirname,
259+
"..",
260+
"..",
261+
"test_projects",
262+
"fishfood",
263+
"dataconnect",
264+
"schema",
265+
"schema.gql",
266+
);
267+
268+
// Open the schema file
269+
const editorView = new EditorView(workbench);
270+
await editorView.openFile(schemaFilePath);
271+
272+
// Verify that inline Read Data button is displayed
273+
const readDataButton = await editorView.readDataButton;
274+
await readDataButton.waitForDisplayed();
275+
276+
// Click the Read Data button
277+
await readDataButton.click();
278+
279+
// Wait a bit for the query to be generated
280+
await browser.pause(1500);
281+
282+
// Verify the generated query file path
283+
const activeEditor = await editorView.getActiveEditor();
284+
const filePath = activeEditor?.document.fileName;
285+
expect(filePath).toContain("test_projects/fishfood/dataconnect/Post_read.gql");
286+
await editorView.closeCurrentEditor();
287+
},
288+
);
204289
});

0 commit comments

Comments
 (0)