import { ErrorResponse , Response } from "@webiny/handler-graphql" ; import { CmsModelCreateInput , CmsModelUpdateInput , CmsContext , CmsModelCreateFromInput } from "~/types" ; import { GraphQLSchemaPlugin } from "@webiny/handler-graphql/plugins/GraphQLSchemaPlugin" ; import { Resolvers } from "@webiny/handler-graphql/types" ; import { CmsModelPlugin } from "~/content/plugins/CmsModelPlugin" ; interface CreateCmsModelArgs { data : CmsModelCreateInput ; } interface CreateFromCmsModelFromArgs { modelId : string ; data : CmsModelCreateFromInput ; } interface ReadCmsModelArgs { modelId : string ; } interface UpdateCmsModelArgs extends ReadCmsModelArgs { data : CmsModelUpdateInput ; } interface DeleteCmsModelArgs { modelId : string ; } const plugin = ( context ) : GraphQLSchemaPlugin < CmsContext > => { const resolvers = { Query : { getContentModel : async ( _ , args , context ) => { try { const model = await context . cms . getModel ( args . modelId ) ; return new Response ( model ) ; } catch ( e ) { return new ErrorResponse ( e ) ; } } , listContentModels : async ( _ , __ , context ) => { try { const model = await context . cms . listModels ( ) ; return new Response ( model ) ; } catch ( e ) { return new ErrorResponse ( e ) ; } } } , CmsContentModel : { plugin : async ( model , _ , context ) => { const modelPlugin = context . plugins . byType < CmsModelPlugin > ( CmsModelPlugin . type ) . find ( ( item ) => item . contentModel . modelId === model . modelId ) ; return Boolean ( modelPlugin ) ; } } } ; let manageSchema = "" ; if ( context . cms . MANAGE ) { resolvers . Mutation = { createContentModel : async ( _ , args , context ) => { try { const model = await context . cms . createModel ( args . data ) ; return new Response ( model ) ; } catch ( e ) { return new ErrorResponse ( e ) ; } } , createContentModelFrom : async ( _ , args , context ) => { try { const model = await context . cms . createModelFrom ( args . modelId , args . data ) ; return new Response ( model ) ; } catch ( e ) { return new ErrorResponse ( e ) ; } } , updateContentModel : async ( _ , args , context ) => { const { modelId , data } = args ; try { const model = await context . cms . updateModel ( modelId , data ) ; return new Response ( model ) ; } catch ( e ) { return new ErrorResponse ( e ) ; } } , deleteContentModel : async ( _ , args , context ) => { const { modelId } = args ; try { await context . cms . deleteModel ( modelId ) ; return new Response ( true ) ; } catch ( e ) { return new ErrorResponse ( e ) ; } } } ; manageSchema = `
            input CmsPredefinedValueInput {
                label: String!
                value: String!
                selected: Boolean
            }

            input CmsPredefinedValuesInput {
                enabled: Boolean
                values: [CmsPredefinedValueInput]
            }
            input CmsFieldRendererInput {
                name: String
            }

            input CmsFieldValidationInput {
                name: String!
                message: String
                settings: JSON
            }

            input CmsContentModelFieldInput {
                id: ID!
                label: String!
                helpText: String
                placeholderText: String
                fieldId: String!
                type: String!
                multipleValues: Boolean
                predefinedValues: CmsPredefinedValuesInput
                renderer: CmsFieldRendererInput
                validation: [CmsFieldValidationInput]
                listValidation: [CmsFieldValidationInput]
                settings: JSON
            }

            input CmsContentModelCreateInput {
                name: String!
                modelId: String
                group: RefInput!
                description: String
            }

            input CmsContentModelCreateFromInput {
                name: String!
                modelId: String
                group: RefInput!
                description: String
                locale: String
            }

            input CmsContentModelUpdateInput {
                name: String
                group: RefInput
                description: String
                layout: [[ID!]!]!
                fields: [CmsContentModelFieldInput!]!
                titleFieldId: String
            }

            extend type Mutation {
                createContentModel(data: CmsContentModelCreateInput!): CmsContentModelResponse

                createContentModelFrom(
                    modelId: ID!
                    data: CmsContentModelCreateFromInput!
                ): CmsContentModelResponse

                updateContentModel(
                    modelId: ID!
                    data: CmsContentModelUpdateInput!
                ): CmsContentModelResponse

                deleteContentModel(modelId: ID!): CmsDeleteResponse
            }
        ` ; } return new GraphQLSchemaPlugin < CmsContext > ( { typeDefs : `
            type CmsFieldValidation {
                name: String!
                message: String
                settings: JSON
            }

            type CmsFieldRenderer {
                name: String
            }

            type CmsPredefinedValue {
                label: String
                value: String
                selected: Boolean
            }

            type CmsPredefinedValues {
                enabled: Boolean
                values: [CmsPredefinedValue]
            }

            type CmsContentModelField {
                id: ID!
                fieldId: String!
                label: String!
                helpText: String
                placeholderText: String
                type: String!
                multipleValues: Boolean
                predefinedValues: CmsPredefinedValues
                renderer: CmsFieldRenderer
                validation: [CmsFieldValidation!]
                listValidation: [CmsFieldValidation!]
                settings: JSON
            }

            type CmsContentModel {
                name: String!
                modelId: String!
                description: String
                group: CmsContentModelGroup!
                createdOn: DateTime
                savedOn: DateTime
                createdBy: CmsCreatedBy
                fields: [CmsContentModelField!]!
                lockedFields: [JSON]
                layout: [[String!]!]!
                titleFieldId: String

                # Returns true if the content model is registered via a plugin.
                plugin: Boolean!
            }

            type CmsContentModelResponse {
                data: CmsContentModel
                error: CmsError
            }

            type CmsContentModelListResponse {
                data: [CmsContentModel]
                meta: CmsListMeta
                error: CmsError
            }

            extend type Query {
                getContentModel(modelId: ID!, where: JSON, sort: String): CmsContentModelResponse

                listContentModels: CmsContentModelListResponse
            }

            ${ manageSchema }
        ` , resolvers } ) ; } ; export default plugin ;