Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Changes default CF3 runtime to nodejs22 (#8037)
- Fixed an issue where `--import` would error for the Data Connect emulator if `dataDir` was also set.
- Fixed an issue where `firebase init dataconnect` errored when importing a schema with no GQL files.
3 changes: 3 additions & 0 deletions src/dataconnect/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
import { DataConnectMultiple } from "../firebaseConfig";
import { load } from "./load";

export function readFirebaseJson(config?: Config): DataConnectMultiple {

Check warning on line 11 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
if (!config?.has("dataconnect")) {
return [];
}
const validator = (cfg: any) => {

Check warning on line 15 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

Check warning on line 15 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (!cfg["source"]) {

Check warning on line 16 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access ["source"] on an `any` value
throw new FirebaseError("Invalid firebase.json: DataConnect requires `source`");
}
return {
source: cfg["source"],

Check warning on line 20 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 20 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access ["source"] on an `any` value
};
};
const configs = config.get("dataconnect");

Check warning on line 23 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
if (typeof configs === "object" && !Array.isArray(configs)) {
return [validator(configs)];
} else if (Array.isArray(configs)) {
Expand All @@ -32,13 +32,13 @@
}
}

export async function readDataConnectYaml(sourceDirectory: string): Promise<DataConnectYaml> {

Check warning on line 35 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const file = await readFileFromDirectory(sourceDirectory, "dataconnect.yaml");
const dataconnectYaml = await wrappedSafeLoad(file.source);

Check warning on line 37 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
return validateDataConnectYaml(dataconnectYaml);
}

function validateDataConnectYaml(unvalidated: any): DataConnectYaml {

Check warning on line 41 in src/dataconnect/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
// TODO: Use json schema for validation here!
if (!unvalidated["location"]) {
throw new FirebaseError("Missing required field 'location' in dataconnect.yaml");
Expand All @@ -58,6 +58,9 @@
}

export async function readGQLFiles(sourceDir: string): Promise<File[]> {
if (!fs.existsSync(sourceDir)) {
return [];
}
const files = await fs.readdir(sourceDir);
// TODO: Handle files in subdirectories such as `foo/a.gql` and `bar/baz/b.gql`.
return files
Expand Down
27 changes: 27 additions & 0 deletions src/init/features/dataconnect/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as sinon from "sinon";
import { expect } from "chai";
import * as fs from "fs-extra";

import * as init from "./index";
import { Config } from "../../../config";
Expand All @@ -17,9 +18,11 @@ describe("init dataconnect", () => {
const sandbox = sinon.createSandbox();
let provisionCSQLStub: sinon.SinonStub;
let askWriteProjectFileStub: sinon.SinonStub;
let ensureSyncStub: sinon.SinonStub;

beforeEach(() => {
provisionCSQLStub = sandbox.stub(provison, "provisionCloudSql");
ensureSyncStub = sandbox.stub(fs, "ensureFileSync");
});

afterEach(() => {
Expand All @@ -33,6 +36,7 @@ describe("init dataconnect", () => {
expectedSource: string;
expectedFiles: string[];
expectCSQLProvisioning: boolean;
expectEnsureSchemaGQL: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change required, but this could've been optional, and you wouldn't have to change any of the existing test-cases

}[] = [
{
desc: "empty project should generate template",
Expand All @@ -47,6 +51,7 @@ describe("init dataconnect", () => {
"dataconnect/connector/mutations.gql",
],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: false,
},
{
desc: "exiting project should use existing directory",
Expand All @@ -55,6 +60,7 @@ describe("init dataconnect", () => {
expectedSource: "not-dataconnect",
expectedFiles: ["not-dataconnect/dataconnect.yaml"],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: false,
},
{
desc: "should write schema files",
Expand All @@ -70,6 +76,7 @@ describe("init dataconnect", () => {
expectedSource: "dataconnect",
expectedFiles: ["dataconnect/dataconnect.yaml", "dataconnect/schema/schema.gql"],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: false,
},
{
desc: "should write connector files",
Expand All @@ -95,6 +102,7 @@ describe("init dataconnect", () => {
"dataconnect/hello/queries.gql",
],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: false,
},
{
desc: "should provision cloudSQL resources ",
Expand All @@ -111,6 +119,22 @@ describe("init dataconnect", () => {
"dataconnect/connector/mutations.gql",
],
expectCSQLProvisioning: true,
expectEnsureSchemaGQL: false,
},
{
desc: "should handle schema with no files",
requiredInfo: mockRequiredInfo({
schemaGql: [],
}),
config: mockConfig({
dataconnect: {
source: "dataconnect",
},
}),
expectedSource: "dataconnect",
expectedFiles: ["dataconnect/dataconnect.yaml"],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: true,
},
];

Expand All @@ -129,6 +153,9 @@ describe("init dataconnect", () => {
c.requiredInfo,
);
expect(c.config.get("dataconnect.source")).to.equal(c.expectedSource);
if (c.expectEnsureSchemaGQL) {
expect(ensureSyncStub).to.have.been.calledWith("dataconnect/schema/schema.gql");
}
expect(askWriteProjectFileStub.args.map((a) => a[0])).to.deep.equal(c.expectedFiles);
expect(provisionCSQLStub.called).to.equal(c.expectCSQLProvisioning);
});
Expand Down
5 changes: 5 additions & 0 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join, basename } from "path";
import * as clc from "colorette";
import * as fs from "fs-extra";

import { confirm, promptOnce } from "../../../prompt";
import { Config } from "../../../config";
Expand Down Expand Up @@ -196,7 +197,11 @@ async function writeFiles(config: Config, info: RequiredInfo) {
for (const f of info.schemaGql) {
await config.askWriteProjectFile(join(dir, "schema", f.path), f.content);
}
} else {
// Even if the schema is empty, lets give them an empty .gql file to get started.
fs.ensureFileSync(join(dir, "schema", "schema.gql"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth also filling in that schema with the commented out schema? I could go either way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little safer in case they already had a schema.gql file - ensure won't overwrite it

}

for (const c of info.connectors) {
await writeConnectorFiles(config, c);
}
Expand Down
Loading