Skip to content

Commit

Permalink
FAI-5080: v2 flow author ids are unknown (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-faros committed Feb 10, 2023
1 parent ab9d9fc commit d3322eb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
7 changes: 6 additions & 1 deletion scripts/load-schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

// eslint-disable-next-line @typescript-eslint/no-var-requires
const {outputFile} = require('fs-extra');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {Command} = require('commander');
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -9,10 +11,13 @@ const program = new Command('perf')
.description('Run schema loader against Hasura instance')
.requiredOption('--url <url>', 'Hasura URL')
.requiredOption('--admin-secret <key>', 'admin secret')
.requiredOption('--output <file>', 'location to write schema')
.action(async (opts) => {
const loader = new HasuraSchemaLoader(opts.url, opts.adminSecret, true);
const res = await loader.loadSchema();
console.log(JSON.stringify(res, null, 2));
const schema = JSON.stringify(res, null, 2);
console.log(`writing hasura schema to ${opts.output}...`);
await outputFile(opts.output, schema);
});

program.parse(process.argv);
42 changes: 34 additions & 8 deletions src/graphql/hasura-schema-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {VError} from 'verror';
import {
foreignKeyForArray,
foreignKeyForObj,
foreignKeyForReverseObj,
isManualConfiguration,
MULTI_TENANT_COLUMNS,
parsePrimaryKeys,
Expand Down Expand Up @@ -108,15 +109,23 @@ export class HasuraSchemaLoader implements SchemaLoader {
const res: Dictionary<Dictionary<string>> = {};
for (const table of source.tables) {
const targetTable: string = table.table.name;
if (table.array_relationships) {
for (const arrRel of table.array_relationships) {
const fkCol = foreignKeyForArray(arrRel);
const sourceTable = remoteTableForArray(arrRel);
ok(sourceTable, `missing source table on ${JSON.stringify(arrRel)}`);
if (!res[sourceTable]) {
res[sourceTable] = {};
for (const arrRel of table.array_relationships || []) {
const fkCol = foreignKeyForArray(arrRel);
const sourceTable = remoteTableForArray(arrRel);
ok(sourceTable, `missing source table on ${JSON.stringify(arrRel)}`);
if (!res[sourceTable]) {
res[sourceTable] = {};
}
res[sourceTable][fkCol] = targetTable;
}
// find array relationship represented as reverse object relationships
for (const objRel of table.object_relationships || []) {
const reverseObjFk = foreignKeyForReverseObj(objRel);
if (reverseObjFk) {
if (!res[reverseObjFk.sourceTable]) {
res[reverseObjFk.sourceTable] = {};
}
res[sourceTable][fkCol] = targetTable;
res[reverseObjFk.sourceTable][reverseObjFk.fkCol] = targetTable;
}
}
}
Expand Down Expand Up @@ -179,6 +188,10 @@ export class HasuraSchemaLoader implements SchemaLoader {
const tableReferences: Dictionary<Reference> = {};
for (const rel of table.object_relationships ?? []) {
const fk = foreignKeyForObj(rel);
// skip reverse object relationships
if (fk === 'id') {
continue;
}
const relFldName = this.camelCaseFieldNames ? camelCase(fk) : fk;
const relMetadata = {
field: rel.name,
Expand All @@ -204,6 +217,19 @@ export class HasuraSchemaLoader implements SchemaLoader {
};
}
);
const reverseBackRefs: BackReference[] = [];
(table.object_relationships ?? []).forEach(
(rel) => {
const reverseObjFk = foreignKeyForReverseObj(rel);
if (reverseObjFk) {
reverseBackRefs.push({
field: rel.name,
model: reverseObjFk.sourceTable,
});
}
},
);
backReferences[tableName].push(...reverseBackRefs);
}
const modelDeps: [string, string][] = [];
for (const model of Object.keys(references)) {
Expand Down
16 changes: 16 additions & 0 deletions src/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ export function foreignKeyForArray(rel: ArrayRelationship): string {
return fk;
}

export function foreignKeyForReverseObj(
rel: ObjectRelationship
): {fkCol: string; sourceTable: string} | undefined {
if (isManualConfiguration(rel.using)) {
// for reverse object rel, column_mapping contains one entry
// mapping literal id to FK on remote table
const fkCol = rel.using.manual_configuration.column_mapping?.id;
if (fkCol) {
const sourceTable = rel.using.manual_configuration.remote_table?.name;
ok(sourceTable, `missing remote_table for ${JSON.stringify(rel)}`);
return {fkCol, sourceTable};
}
}
return undefined;
}

/**
* Parse elements of primary key from pkey function definition.
* e.g. pkey(VARIADIC ARRAY[tenant_id, graph_name, source, uid])
Expand Down

0 comments on commit d3322eb

Please sign in to comment.