graphile-settings
Version:
graphile settings
103 lines (102 loc) • 3.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pgTypeToGqlType = pgTypeToGqlType;
exports.buildFieldMeta = buildFieldMeta;
const PG_TO_GQL_TYPE = {
text: 'String',
varchar: 'String',
char: 'String',
name: 'String',
bpchar: 'String',
uuid: 'UUID',
int2: 'Int',
int4: 'Int',
integer: 'Int',
int8: 'BigInt',
bigint: 'BigInt',
float4: 'Float',
float8: 'Float',
numeric: 'BigFloat',
bool: 'Boolean',
boolean: 'Boolean',
timestamptz: 'Datetime',
timestamp: 'Datetime',
date: 'Date',
time: 'Time',
timetz: 'Time',
json: 'JSON',
jsonb: 'JSON',
interval: 'Interval',
point: 'Point',
geometry: 'GeoJSON',
geography: 'GeoJSON',
inet: 'InternetAddress',
cidr: 'InternetAddress',
xml: 'String',
bytea: 'String',
macaddr: 'String',
};
function pgTypeToGqlType(pgTypeName) {
return PG_TO_GQL_TYPE[pgTypeName] || pgTypeName;
}
function getGeometrySubtype(attr) {
const subtype = attr?.extensions?.geometrySubtype;
return typeof subtype === 'string' ? subtype : null;
}
function resolveGqlTypeName(build, codec) {
if (!codec)
return 'unknown';
try {
const codecForLookup = codec.arrayOfCodec || codec;
if (build?.hasGraphQLTypeForPgCodec?.(codecForLookup, 'output')) {
const resolved = build.getGraphQLTypeNameByPgCodec?.(codecForLookup, 'output');
if (resolved)
return resolved;
}
}
catch {
// Fall through to static fallback mapping.
}
const pgTypeName = codec.name || 'unknown';
const mapped = pgTypeToGqlType(pgTypeName);
if (mapped !== pgTypeName)
return mapped;
const nestedTypeName = codec.arrayOfCodec?.name;
return nestedTypeName ? pgTypeToGqlType(nestedTypeName) : pgTypeName;
}
function extractEnumMeta(codec) {
if (!codec)
return null;
// Check the codec itself, or unwrap domain/array wrappers
const inner = codec.domainOfCodec ?? codec.arrayOfCodec ?? codec;
const values = inner.values;
if (!Array.isArray(values) || values.length === 0)
return null;
return {
name: inner.name || codec.name || 'unknown',
values: values.map((v) => (typeof v === 'string' ? v : v.value)),
};
}
function buildFieldMeta(name, attr, build, options) {
const pgType = attr?.codec?.name || 'unknown';
const isNotNull = attr?.notNull || false;
const hasDefault = attr?.hasDefault || false;
const subtype = getGeometrySubtype(attr);
return {
name,
type: {
pgType,
gqlType: build ? resolveGqlTypeName(build, attr?.codec) : pgTypeToGqlType(pgType),
isArray: !!attr?.codec?.arrayOfCodec,
isNotNull,
hasDefault,
subtype,
},
isNotNull,
hasDefault,
isPrimaryKey: options?.isPrimaryKey ?? false,
isForeignKey: options?.isForeignKey ?? false,
description: attr?.description ?? null,
enumValues: extractEnumMeta(attr?.codec),
};
}