decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.65k stars 1 forks source link

Delete image from CMS admin breaks GraphQL queries #7186

Open smarcet opened 2 months ago

smarcet commented 2 months ago

Hello i created a simple image field at cms when i deleted the image from cms admin its sets to empty on my json file { image:"} and start to got this error message Field "image" must not have a selection since type "String" has no subfields Please replace with a clear and descriptive title from graphql queries at page there is a way to tell the widget to remove the field from json file

i already have the custom type defined gastby-node.js

at exports.createSchemaCustomization like this

type MemberCompanyNode {
    id: String
    big_logo: String
    name: String
  }

  type CompanyNode {
    image: File @fileByRelativePath
    paragraph1: String
    paragraph2: String
    link: String
    memberCompany: MemberCompanyNode
  }

  type OSFMemberNode {
    osfMember: CompanyNode
  }

  type MemberSpotLiteSectionJson implements Node {
    companies: [OSFMemberNode]
  }

and graphql query at page is

 memberSpotliteSectionJson {
    companies {
      osfMember {
        image {
          publicURL
        }
        link
        memberCompany {
          big_logo
        }
        paragraph1
        paragraph2
      }
    }
  }

also i tried to create a custom resolver at gastby node.js exports.createResolvers


createResolvers({

    MemberSpotliteSectionJsonCompaniesOsfMember: {
      image: {
        type: "File",
        resolve: (source) => {
          console.log(`MemberSpotliteSectionJsonCompaniesOsfMember`,source.image)
          (source?.image?.publicURL ? source.image : {publicURL:null})
        },
      },
    },
  });

but i got warn `createResolvers` passed resolvers for field `MemberSpotliteSectionJsonCompaniesOsfMember.image` with type `File`. Such a field with type `String` already exists on the type. Use `createTypes` to override type
fields.

really i am stuck at this
there is no simple way to delete an image at CMS and keep it working ?
much thanks
tizzle commented 1 month ago

Same here,

for me this is a rather big problem. I would appreciate a fix for this on CMS side.

Can the team acknowledge this and probably hint at how we can get this fixed?

tizzle commented 1 month ago

also related to #7120

smarcet commented 1 month ago

i ended up with a work around exports.createResolvers = ({ createResolvers, pathPrefix }) => { const resolvers = { MemberSpotliteSectionJsonCompanies: { companyImagePublicURL: { type: "String", resolve: async (source, args, context) => { console.log("createResolvers", source); const node = await context.nodeModel.findOne({ query: { filter: { base: { eq: source?.image }, }, }, type: "File", }); console.log("createResolvers entries", node); if (!node) return source.memberCompany.big_logo;

      const { absolutePath } = node;
      const { ext, name } = path.parse(absolutePath);
      const filename = `${name}--${node.internal.contentDigest}${ext}`;
      const newPath = path.join(publicStaticDir, filename);

      try {
        const exists = await fs.exists(newPath);
        if (!exists) await fs.copy(absolutePath, newPath);
        return `${pathPrefix}/static/${filename}`;
      } catch (e) {
        console.error(
          `error copying file from ${absolutePath} to ${newPath}`,
          e,
        );
      }
      return source.memberCompany.big_logo;
    },
  },
},

}; createResolvers(resolvers); };