mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-22 19:19:05 +00:00
Handle relation fields in subgraph entities (#87)
This commit is contained in:
parent
4b1b0e0ed6
commit
d96cc095bb
@ -282,7 +282,8 @@ export class Entity {
|
|||||||
|
|
||||||
_addBigIntTransformerOption (entityObject: any): void {
|
_addBigIntTransformerOption (entityObject: any): void {
|
||||||
entityObject.columns.forEach((column: any) => {
|
entityObject.columns.forEach((column: any) => {
|
||||||
if (column.tsType === 'bigint') {
|
// Implement bigintTransformer for bigint types.
|
||||||
|
if (['bigint', 'bigint[]'].includes(column.tsType)) {
|
||||||
column.columnOptions.push(
|
column.columnOptions.push(
|
||||||
{
|
{
|
||||||
option: 'transformer',
|
option: 'transformer',
|
||||||
@ -310,6 +311,11 @@ export class Entity {
|
|||||||
|
|
||||||
_addSubgraphColumns (subgraphTypeDefs: any, entityObject: any, def: any): any {
|
_addSubgraphColumns (subgraphTypeDefs: any, entityObject: any, def: any): any {
|
||||||
def.fields.forEach((field: 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;
|
let name = field.name.value;
|
||||||
|
|
||||||
// Column id is already added.
|
// Column id is already added.
|
||||||
@ -331,79 +337,51 @@ export class Entity {
|
|||||||
const { typeName, array, nullable } = this._getFieldType(field.type);
|
const { typeName, array, nullable } = this._getFieldType(field.type);
|
||||||
let tsType = getTsForGql(typeName);
|
let tsType = getTsForGql(typeName);
|
||||||
|
|
||||||
if (tsType) {
|
if (!tsType) {
|
||||||
// Handle basic array types.
|
tsType = 'string';
|
||||||
if (array) {
|
|
||||||
columnObject.columnOptions.push({
|
|
||||||
option: 'array',
|
|
||||||
value: 'true'
|
|
||||||
});
|
|
||||||
|
|
||||||
columnObject.tsType = `${tsType}[]`;
|
|
||||||
} else {
|
|
||||||
columnObject.tsType = tsType;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// TODO Handle array of custom types.
|
|
||||||
tsType = typeName;
|
|
||||||
columnObject.tsType = tsType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const pgType = getPgForTs(tsType);
|
columnObject.tsType = tsType;
|
||||||
|
|
||||||
// If basic type: create a column.
|
// Handle basic array types.
|
||||||
if (pgType) {
|
if (array) {
|
||||||
columnObject.pgType = pgType;
|
columnObject.columnOptions.push({
|
||||||
} else {
|
option: 'array',
|
||||||
if (subgraphTypeDefs.some((typeDef: any) => typeDef.kind === 'EnumTypeDefinition' && typeDef.name.value === typeName)) {
|
value: 'true'
|
||||||
// Create enum type column.
|
});
|
||||||
|
|
||||||
const entityImport = entityObject.imports.find(({ from }: any) => from === '../types');
|
columnObject.tsType = `${tsType}[]`;
|
||||||
|
}
|
||||||
|
|
||||||
if (!entityImport) {
|
if (subgraphTypeDefs.some((typeDef: any) => typeDef.kind === 'EnumTypeDefinition' && typeDef.name.value === typeName)) {
|
||||||
entityObject.imports.push(
|
// Create enum type column.
|
||||||
{
|
|
||||||
toImport: new Set([typeName]),
|
|
||||||
from: '../types'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
entityImport.toImport.add(typeName);
|
|
||||||
}
|
|
||||||
|
|
||||||
columnObject.columnOptions.push(
|
const entityImport = entityObject.imports.find(({ from }: any) => from === '../types');
|
||||||
|
|
||||||
|
if (!entityImport) {
|
||||||
|
entityObject.imports.push(
|
||||||
{
|
{
|
||||||
option: 'type',
|
toImport: new Set([typeName]),
|
||||||
value: "'enum'"
|
from: '../types'
|
||||||
},
|
|
||||||
{
|
|
||||||
option: 'enum',
|
|
||||||
value: typeName
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Create a relation.
|
entityImport.toImport.add(typeName);
|
||||||
|
|
||||||
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}`
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
columnObject.columnOptions.push(
|
||||||
|
{
|
||||||
|
option: 'type',
|
||||||
|
value: "'enum'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
option: 'enum',
|
||||||
|
value: typeName
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Enum type does not require pgType.
|
||||||
|
columnObject.pgType = getPgForTs(tsType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nullable) {
|
if (nullable) {
|
||||||
|
@ -25,9 +25,6 @@ export function parseSubgraphSchema (subgraphPath: string): any {
|
|||||||
|
|
||||||
if (def.kind === 'ObjectTypeDefinition') {
|
if (def.kind === 'ObjectTypeDefinition') {
|
||||||
def.fields.forEach((field: any) => {
|
def.fields.forEach((field: any) => {
|
||||||
// Remove field directives.
|
|
||||||
field.directives = [];
|
|
||||||
|
|
||||||
// Parse the field type.
|
// Parse the field type.
|
||||||
field.type = parseType(field.type);
|
field.type = parseType(field.type);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user