import { ModelDefinition } from "@graphback/core" ; import { parseMetadata } from "graphql-metadata" ; import { loadFilesSync } from "@graphql-tools/load-files" ; import { mergeTypeDefs } from "@graphql-tools/merge" ; import { isListType , getNamedType , isCompositeType , GraphQLType , buildSchema , buildASTSchema } from "graphql" ; import { mkdirSync , existsSync , lstatSync , readFileSync } from "fs" ; import { join } from "path" ; export const isDataSyncClientModel = ( model ) => { return parseMetadata ( "datasync" , model . graphqlType ) ; } ; export const makeDirIfNotExists = ( path ) => { try { mkdirSync ( path , { recursive : true } ) ; } catch ( error ) { } } ; param type returns export const convertToTsType = ( type ) : string => { if ( isListType ( type ) ) { return `${ convertToTsType ( getNamedType ( type ) ) }[]` ; } if ( isCompositeType ( type ) || type . toString ( ) === "ID" || type . toString ( ) === "GraphbackObjectID" ) { return "string" ; } let tsType = type . name . toLowerCase ( ) ; if ( tsType === "int" || tsType === "float" ) { tsType = "number" ; } else if ( tsType !== "string" && tsType !== "boolean" ) { tsType = "any" ; } return tsType ; } ; export param { string } modelDir returns { string } export function loadSchema ( modelPath ) { const fullModelPath = join ( process . cwd ( ) , modelPath ) ; if ( typeof modelPath === "string" && existsSync ( fullModelPath ) && lstatSync ( fullModelPath ) . isDirectory ( ) ) { const typesArrary = loadFilesSync ( modelPath , { extensions : [ "graphql" ] } ) ; return buildASTSchema ( mergeTypeDefs ( typesArrary ) ) ; } const schema = readFileSync ( modelPath ) . toString ( ) ; return buildSchema ( schema ) ; }