Skip to content

Commit

Permalink
Handle relation fields in subgraph entities (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi authored and prathamesh0 committed Dec 23, 2021
1 parent 10a5a0f commit 75e9025
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 67 deletions.
106 changes: 42 additions & 64 deletions packages/codegen/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ export class Entity {

_addBigIntTransformerOption (entityObject: any): void {
entityObject.columns.forEach((column: any) => {
if (column.tsType === 'bigint') {
// Implement bigintTransformer for bigint types.
if (['bigint', 'bigint[]'].includes(column.tsType)) {
column.columnOptions.push(
{
option: 'transformer',
Expand Down Expand Up @@ -310,6 +311,11 @@ export class Entity {

_addSubgraphColumns (subgraphTypeDefs: any, entityObject: any, def: any): any {
def.fields.forEach((field: any) => {
if (field.directives.some((directive: any) => directive.name.value === 'derivedFrom')) {
// Do not add column if it is a derived field.
return;
}

let name = field.name.value;

// Column id is already added.
Expand All @@ -331,79 +337,51 @@ export class Entity {
const { typeName, array, nullable } = this._getFieldType(field.type);
let tsType = getTsForGql(typeName);

if (tsType) {
// Handle basic array types.
if (array) {
columnObject.columnOptions.push({
option: 'array',
value: 'true'
});
if (!tsType) {
tsType = 'string';
}

columnObject.tsType = `${tsType}[]`;
} else {
columnObject.tsType = tsType;
}
} else {
// TODO Handle array of custom types.
tsType = typeName;
columnObject.tsType = tsType;
columnObject.tsType = tsType;

// Handle basic array types.
if (array) {
columnObject.columnOptions.push({
option: 'array',
value: 'true'
});

columnObject.tsType = `${tsType}[]`;
}

const pgType = getPgForTs(tsType);
if (subgraphTypeDefs.some((typeDef: any) => typeDef.kind === 'EnumTypeDefinition' && typeDef.name.value === typeName)) {
// Create enum type column.

// If basic type: create a column.
if (pgType) {
columnObject.pgType = pgType;
} else {
if (subgraphTypeDefs.some((typeDef: any) => typeDef.kind === 'EnumTypeDefinition' && typeDef.name.value === typeName)) {
// Create enum type column.

const entityImport = entityObject.imports.find(({ from }: any) => from === '../types');

if (!entityImport) {
entityObject.imports.push(
{
toImport: new Set([typeName]),
from: '../types'
}
);
} else {
entityImport.toImport.add(typeName);
}
const entityImport = entityObject.imports.find(({ from }: any) => from === '../types');

columnObject.columnOptions.push(
{
option: 'type',
value: "'enum'"
},
if (!entityImport) {
entityObject.imports.push(
{
option: 'enum',
value: typeName
toImport: new Set([typeName]),
from: '../types'
}
);
} else {
// Create a relation.

columnObject.columnType = 'ManyToOne';
columnObject.lhs = '()';
columnObject.rhs = tsType;

entityObject.imports[0].toImport.add('ManyToOne');

// Check if type import already added.
const importObject = entityObject.imports.find((element: any) => {
return element.from === `./${tsType}`;
});

if (!importObject) {
entityObject.imports.push(
{
toImport: new Set([tsType]),
from: `./${tsType}`
}
);
}
entityImport.toImport.add(typeName);
}

columnObject.columnOptions.push(
{
option: 'type',
value: "'enum'"
},
{
option: 'enum',
value: typeName
}
);
} else {
// Enum type does not require pgType.
columnObject.pgType = getPgForTs(tsType);
}

if (nullable) {
Expand Down
3 changes: 0 additions & 3 deletions packages/codegen/src/utils/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export function parseSubgraphSchema (subgraphPath: string): any {

if (def.kind === 'ObjectTypeDefinition') {
def.fields.forEach((field: any) => {
// Remove field directives.
field.directives = [];

// Parse the field type.
field.type = parseType(field.type);
});
Expand Down

0 comments on commit 75e9025

Please sign in to comment.