Skip to content
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

Fix generated mutations for custom keys. 0.1.9 ver bump #7161

Merged
merged 9 commits into from
May 11, 2024
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
5 changes: 5 additions & 0 deletions firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 0.1.9

- Fix "Add Data" for nonnull and custom keys
- Emulator Bump 1.1.17

## 0.1.8

- Update Extensions page Logo
Expand Down
4 changes: 2 additions & 2 deletions firebase-vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion firebase-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "firebase",
"icon": "./resources/firebase_logo.png",
"description": "VSCode Extension for Firebase",
"version": "0.1.8",
"version": "0.1.9",
"engines": {
"vscode": "^1.69.0"
},
Expand Down
49 changes: 26 additions & 23 deletions firebase-vscode/src/data-connect/ad-hoc-mutations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import vscode, { Disposable } from "vscode";
import { DocumentNode, Kind, ObjectTypeDefinitionNode } from "graphql";
import { DocumentNode, GraphQLInputObjectType, GraphQLScalarType, Kind, ObjectTypeDefinitionNode, buildClientSchema, buildSchema } from "graphql";
import { checkIfFileExists, upsertFile } from "./file-utils";
import { DataConnectService } from "./service";

export function registerAdHoc(): Disposable {
export function registerAdHoc(dataConnectService: DataConnectService): Disposable {
const defaultScalarValues = {
Any: "{}",
AuthUID: '""',
Expand Down Expand Up @@ -122,7 +123,7 @@ query {
// generate content for the file
const preamble =
"# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file.";
const adhocMutation = generateMutation(ast);
const adhocMutation = await generateMutation(ast);
const content = [preamble, adhocMutation].join("\n");

const basePath = vscode.workspace.rootPath + "/dataconnect/";
Expand All @@ -149,33 +150,35 @@ query {
}
}

function generateMutation(ast: ObjectTypeDefinitionNode): string {
const name =
async function generateMutation(
ast: ObjectTypeDefinitionNode,
): Promise<string> {
const introspect = (await dataConnectService.introspect())?.data;
const schema = buildClientSchema(introspect);

const name = ast.name.value;
const lowerCaseName =
ast.name.value.charAt(0).toLowerCase() + ast.name.value.slice(1);
const dataName = `${name}_Data`;
const mutationDataType: GraphQLInputObjectType = schema.getTypeMap()[dataName] as GraphQLInputObjectType;

// build mutation as string
const functionSpacing = "\t";
const fieldSpacing = "\t\t";
const mutation = [];

mutation.push("mutation {"); // mutation header
mutation.push(`${functionSpacing}${name}_insert(data: {`); // insert function
for (const field of ast.fields) {
mutation.push(`${functionSpacing}${lowerCaseName}_insert(data: {`);
for (const [fieldName, field] of Object.entries(mutationDataType.getFields())) {
// necessary to avoid type error
let fieldType: any = field.type;
// We unwrap NonNullType to obtain the actual type
if (fieldType.kind === Kind.NON_NULL_TYPE) {
fieldType = fieldType.type;
}
let fieldTypeName: string = fieldType.name.value;
let fieldName: string = field.name.value;
let defaultValue = defaultScalarValues[fieldTypeName] as string;
if (!isDataConnectScalarType(fieldTypeName)) {
fieldTypeName += "Id";
fieldName += "Id";
defaultValue = '""';
const fieldtype: any = field.type;
// use all argument types that are of scalar, except x_expr
if (isDataConnectScalarType(fieldtype.name) && !field.name.includes("_expr")) {
const defaultValue = defaultScalarValues[fieldtype.name] || "";
mutation.push(
`${fieldSpacing}${fieldName}: ${defaultValue} # ${fieldtype.name}`,
); // field name + temp value + comment
}
mutation.push(
`${fieldSpacing}${fieldName}: ${defaultValue} # ${fieldTypeName}`,
); // field name + temp value + comment

}
mutation.push(`${functionSpacing}})`, "}"); // closing braces/paren
return mutation.join("\n");
Expand Down
2 changes: 1 addition & 1 deletion firebase-vscode/src/data-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
context.subscriptions.push({
dispose: effect(() => {
const configs = dataConnectConfigs.value?.tryReadValue;
if (client) client.stop();

Check warning on line 167 in firebase-vscode/src/data-connect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Expected { after 'if' condition
if (configs && configs.values.length > 0) {
client = setupLanguageClient(context, configs, lsOutputChannel);
vscode.commands.executeCommand("fdc-graphql.start");
Expand Down Expand Up @@ -221,7 +221,7 @@
registerExecution(context, broker, fdcService, emulatorController),
registerExplorer(context, broker, fdcService),
registerFirebaseDataConnectView(context, broker, emulatorController),
registerAdHoc(),
registerAdHoc(fdcService),
registerConnectors(context, broker, fdcService),
registerFdcDeploy(broker),
registerTerminalTasks(broker),
Expand Down
Loading