Skip to content

Commit d69cf6c

Browse files
committed
feat: add custom schema support in getSchema function and improve node suggestion handling
1 parent 93a1ac8 commit d69cf6c

1 file changed

Lines changed: 33 additions & 32 deletions

File tree

src/suggestion/getSchema.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
Flow,
44
FunctionDefinition,
55
LiteralValue,
6-
NodeFunction, ReferencePath,
6+
NodeFunction,
7+
ReferencePath,
78
ReferenceValue
89
} from "@code0-tech/sagittarius-graphql-types"
910
import {createCompilerHost, generateFlowSourceCode, sanitizeId} from "../utils"
@@ -54,7 +55,8 @@ export const getSchema = (
5455
flow: Flow,
5556
dataTypes: DataType[],
5657
functions: FunctionDefinition[],
57-
nodeId?: NodeFunction['id']
58+
nodeId?: NodeFunction['id'],
59+
schema?: (type: ts.Type) => Schema | null
5860
): ParameterSchema[] => {
5961

6062
const sourceCode = generateFlowSourceCode(flow, functions, dataTypes)
@@ -140,6 +142,9 @@ export const getSchema = (
140142
suggestions: [...literalValueSuggestions, ...referenceSuggestions, ...nodeSuggestions],
141143
}
142144

145+
const customSchema = schema?.(type)
146+
if (customSchema) return customSchema
147+
143148
if (isPrimitiveLiteralUnion(type)) return {input: "select", ...suggestions}
144149

145150
if (isBoolean(type)) return {input: "boolean", ...suggestions}
@@ -249,18 +254,16 @@ function getLiteralValueSuggestions(type: ts.Type): LiteralValue[] {
249254

250255
function getNodeSuggestions(checker: ts.TypeChecker, functionDeclarations: ts.FunctionDeclaration[], functions: FunctionDefinition[], paramType: ts.Type): NodeFunction[] {
251256

252-
//TODO: if paramType is callable than we should parse in all functions that match the parameters
253-
//TODO: otherwise we should only parse in functions that match the return type
254-
//TODO: we should differentiate between inline usable suggestions and not
257+
if (isSubFlow(paramType)) return []
255258

256-
return functionDeclarations.flatMap(func => {
259+
return functionDeclarations.flatMap(func => {
257260

258261
const signature = checker.getSignatureFromDeclaration(func)
259262
const returnType = checker.getReturnTypeOfSignature(signature!)
260263

261-
const simplifiedReturnType = returnType.isTypeParameter()
262-
? (checker.getBaseConstraintOfType(returnType) || checker.getAnyType())
263-
: returnType
264+
const simplifiedReturnType = returnType.isTypeParameter()
265+
? (checker.getBaseConstraintOfType(returnType) || checker.getAnyType())
266+
: returnType
264267

265268
if (checker.isTypeAssignableTo(simplifiedReturnType, paramType)) {
266269
const functionName = func.name?.getText().replace("fn_", "").replace("_", "::").replace("_", "::")
@@ -274,30 +277,28 @@ function getNodeSuggestions(checker: ts.TypeChecker, functionDeclarations: ts.Fu
274277
id: funktion?.id,
275278
identifier: funktion?.identifier,
276279
},
277-
parameters: {
278-
__typename: "NodeParameterConnection",
279-
nodes: (funktion?.parameterDefinitions?.nodes || []).map(p => ({
280-
__typename: "NodeParameter",
281-
parameterDefinition: {
282-
__typename: "ParameterDefinition",
283-
id: p?.id,
284-
identifier: p?.identifier
285-
},
286-
value: p?.defaultValue ? {
287-
__typename: "LiteralValue",
288-
value: p.defaultValue.value
289-
} : null
290-
}))
291-
}
280+
...((funktion?.parameterDefinitions?.nodes?.length ?? 0) > 0 ? {
281+
parameters: {
282+
__typename: "NodeParameterConnection",
283+
nodes:
284+
(funktion?.parameterDefinitions?.nodes || []).map(p => ({
285+
__typename: "NodeParameter",
286+
parameterDefinition: {
287+
__typename: "ParameterDefinition",
288+
id: p?.id,
289+
identifier: p?.identifier
290+
},
291+
value: p?.defaultValue ? {
292+
__typename: "LiteralValue",
293+
value: p.defaultValue.value
294+
} : null
295+
}))
296+
}
297+
} : {}),
292298
}
293-
294299
return node
295-
296300
}
297-
298301
return []
299-
300-
301302
})
302303

303304
}
@@ -358,7 +359,7 @@ function getReferenceSuggestions(checker: ts.TypeChecker, node: ts.VariableDecla
358359
return typeArguments.flatMap((tupleElementType, tupleIndex) => {
359360
const propertyPaths = extractObjectProperties(tupleElementType, checker, paramType)
360361

361-
return propertyPaths.flatMap(({ path }) => {
362+
return propertyPaths.flatMap(({path}) => {
362363
const referenceValue: ReferenceValue = {
363364
__typename: 'ReferenceValue',
364365
nodeFunctionId: nodeFunctionId as any,
@@ -380,7 +381,7 @@ function getReferenceSuggestions(checker: ts.TypeChecker, node: ts.VariableDecla
380381
} else if (name.startsWith("flow_")) {
381382
const propertyPaths = extractObjectProperties(symbolType, checker, paramType)
382383

383-
return propertyPaths.flatMap(({ path }) => {
384+
return propertyPaths.flatMap(({path}) => {
384385
const referenceValue: ReferenceValue = {
385386
__typename: 'ReferenceValue',
386387
nodeFunctionId: null
@@ -462,7 +463,7 @@ const extractObjectProperties = (
462463
): Array<{ path: ReferencePath[], type: ts.Type }> => {
463464
const results: Array<{ path: ReferencePath[], type: ts.Type }> = []
464465

465-
if (checker.isTypeAssignableTo(type, expectedType)) results.push({ path: currentPath, type })
466+
if (checker.isTypeAssignableTo(type, expectedType)) results.push({path: currentPath, type})
466467

467468
if (isRealObjectType(type)) {
468469
const properties = type.getProperties()

0 commit comments

Comments
 (0)