Skip to content

[FDC init] Move the default value to actuate #8554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 13, 2025
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
27 changes: 25 additions & 2 deletions src/init/features/dataconnect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ describe("init dataconnect", () => {
requiredInfo: mockRequiredInfo(),
config: mockConfig({ dataconnect: { source: "not-dataconnect" } }),
expectedSource: "not-dataconnect",
expectedFiles: ["not-dataconnect/dataconnect.yaml"],
expectedFiles: [
"not-dataconnect/dataconnect.yaml",
// Populate the default template.
"not-dataconnect/schema/schema.gql",
"not-dataconnect/connector/connector.yaml",
"not-dataconnect/connector/queries.gql",
"not-dataconnect/connector/mutations.gql",
],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: false,
},
Expand Down Expand Up @@ -125,14 +132,30 @@ describe("init dataconnect", () => {
desc: "should handle schema with no files",
requiredInfo: mockRequiredInfo({
schemaGql: [],
connectors: [
{
id: "my-connector",
path: "hello",
files: [
{
path: "queries.gql",
content: "## Fake GQL",
},
],
},
],
}),
config: mockConfig({
dataconnect: {
source: "dataconnect",
},
}),
expectedSource: "dataconnect",
expectedFiles: ["dataconnect/dataconnect.yaml"],
expectedFiles: [
"dataconnect/dataconnect.yaml",
"dataconnect/hello/connector.yaml",
"dataconnect/hello/queries.gql",
],
expectCSQLProvisioning: false,
expectEnsureSchemaGQL: true,
},
Expand Down
56 changes: 22 additions & 34 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ export async function askQuestions(setup: Setup): Promise<void> {
isNewInstance: false,
cloudSqlDatabase: "",
isNewDatabase: false,
connectors: [defaultConnector],
schemaGql: [defaultSchema],
connectors: [],
schemaGql: [],
shouldProvisionCSQL: false,
};
// Query backend and pick up any existing services quickly.
info = await promptForExistingServices(setup, info, hasBilling);
info = await promptForExistingServices(setup, info);

const requiredConfigUnset =
info.serviceId === "" ||
Expand All @@ -110,6 +110,7 @@ export async function askQuestions(setup: Setup): Promise<void> {
default: true,
}));
if (shouldConfigureBackend) {
// TODO: Prompt for app idea and use GiF backend to generate them.
info = await promptForService(info);
info = await promptForCloudSQL(setup, info);

Expand All @@ -122,14 +123,6 @@ export async function askQuestions(setup: Setup): Promise<void> {
default: true,
}))
);
} else {
// Ensure that the suggested name is DNS compatible
const defaultServiceId = toDNSCompatibleId(basename(process.cwd()));
info.serviceId = info.serviceId || defaultServiceId;
info.cloudSqlInstanceId =
info.cloudSqlInstanceId || `${info.serviceId.toLowerCase() || "app"}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
}
setup.featureInfo = setup.featureInfo || {};
setup.featureInfo.dataconnect = info;
Expand All @@ -147,6 +140,20 @@ export async function actuate(setup: Setup, config: Config): Promise<void> {
if (!info) {
throw new Error("Data Connect feature RequiredInfo is not provided");
}
// Populate the default values of required fields.
const defaultServiceId = toDNSCompatibleId(basename(process.cwd()));
info.serviceId = info.serviceId || defaultServiceId;
info.cloudSqlInstanceId =
info.cloudSqlInstanceId || `${info.serviceId.toLowerCase() || "app"}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
// Make sure to add some GQL files.
// Use the template if it starts from scratch or the existing service has no GQL source.
if (!info.schemaGql.length && !info.connectors.flatMap((r) => r.files).length) {
info.schemaGql = [defaultSchema];
info.connectors = [defaultConnector];
}

await writeFiles(config, info);

if (setup.projectId && info.shouldProvisionCSQL) {
Expand Down Expand Up @@ -181,16 +188,6 @@ async function writeFiles(config: Config, info: RequiredInfo) {
...info,
connectorDirs: info.connectors.map((c) => c.path),
});
// If we are starting from a fresh project without data connect,
if (!config.get("dataconnect.source")) {
// Make sure to add add some GQL files.
// Use the template if the existing service is empty (no schema / connector GQL).
if (!info.schemaGql.length && !info.connectors.flatMap((r) => r.files).length) {
info.schemaGql = [defaultSchema];
info.connectors = [defaultConnector];
}
}

config.set("dataconnect", { source: dir });
await config.askWriteProjectFile(
join(dir, "dataconnect.yaml"),
Expand Down Expand Up @@ -266,24 +263,15 @@ function subConnectorYamlValues(replacementValues: { connectorId: string }): str
return replaced;
}

async function promptForExistingServices(
setup: Setup,
info: RequiredInfo,
isBillingEnabled: boolean,
): Promise<RequiredInfo> {
if (!setup.projectId || !isBillingEnabled) {
// TODO(b/368609569): Don't gate this behind billing once backend billing fix is rolled out.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got rid of the isBillingEnabled check here because we no longer needs it.

FDC API can be used without billing.

async function promptForExistingServices(setup: Setup, info: RequiredInfo): Promise<RequiredInfo> {
// Check for existing Firebase Data Connect services.
if (!setup.projectId) {
return info;
}

// Check for existing Firebase Data Connect services.
const existingServices = await listAllServices(setup.projectId);
const existingServicesAndSchemas = await Promise.all(
existingServices.map(async (s) => {
return {
service: s,
schema: await getSchema(s.name),
};
return { service: s, schema: await getSchema(s.name) };
}),
);
if (existingServicesAndSchemas.length) {
Expand Down
Loading