import get from "lodash/get" ; import set from "lodash/set" ; import { CmsModelField , CmsContext , CmsModelFieldToGraphQLCreateResolver } from "~/types" ; import { entryFieldFromStorageTransform } from "~/content/plugins/utils/entryStorage" ; import { Resolvers } from "@webiny/handler-graphql/types" ; interface CreateFieldResolvers { graphQLType : string ; fields : CmsModelField [ ] ; isRoot : boolean ; extraResolvers ? : Resolvers < any > ; } /**
 * We use a factory to avoid passing the parameters for recursive invocations.
 * This way they will always be in the function scope and we can only pass "fields".
 */ export function createFieldResolversFactory ( { endpointType , models , model , fieldTypePlugins } ) { return function createFieldResolvers ( params ) { const { graphQLType , fields , isRoot = false , extraResolvers = { } } = params ; const fieldResolvers = { ... extraResolvers } ; const typeResolvers = { } ; for ( const field of fields ) { if ( ! fieldTypePlugins [ field . type ] ) { continue ; } const createResolver = get ( fieldTypePlugins , `${ field . type }.${ endpointType }.createResolver` ) ; let resolver ; const fieldResolver = createResolver ? createResolver ( { graphQLType , models , model , field , createFieldResolvers } ) : null ; /**
             * When fieldResolver is false it will completely skip adding fieldId into the resolvers.
             * This is to fix the breaking of GraphQL schema.
             */ if ( fieldResolver === false ) { continue ; } else if ( typeof fieldResolver === "function" ) { resolver = fieldResolver ; } else if ( fieldResolver ) { resolver = fieldResolver . resolver ; Object . assign ( typeResolvers , fieldResolver . typeResolvers ) ; } const { fieldId } = field ; fieldResolvers [ fieldId ] = async ( parent , args , context , info ) => { const transformedValue = await entryFieldFromStorageTransform ( { context , model , field , value : isRoot ? parent . values [ fieldId ] : parent [ fieldId ] } ) ; set ( isRoot ? parent . values : parent , fieldId , transformedValue ) ; if ( ! resolver ) { return isRoot ? parent . values [ fieldId ] : parent [ fieldId ] ; } return await resolver ( isRoot ? parent . values : parent , args , context , info ) ; } ; } return { [ graphQLType ] : fieldResolvers , ... typeResolvers } ; } ; }