Skip to content

Commit

Permalink
Fix generated mutations for custom keys. 0.1.9 ver bump (#7161)
Browse files Browse the repository at this point in the history
* fix non-null type support in generated mutations

* remove console log

* use schema to determine args for generated mutation

* upgrade 0.1.9

---------

Co-authored-by: joehan <joehanley@google.com>
  • Loading branch information
hlshen and joehan committed May 11, 2024
1 parent 80e47a7 commit 502d570
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
5 changes: 5 additions & 0 deletions firebase-vscode/CHANGELOG.md
@@ -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
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
@@ -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
Expand Up @@ -221,7 +221,7 @@ export function registerFdc(
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

0 comments on commit 502d570

Please sign in to comment.