VirtusLab-Open-Source / strapi-plugin-comments

A plugin for Strapi Headless CMS that provides end to end comments feature with their moderation panel, bad words filtering, abuse reporting and more.
MIT License
404 stars 63 forks source link

[CU-86934hpbh] Author avatar is always null in Graphql Queries #225

Open jainsuneet opened 11 months ago

jainsuneet commented 11 months ago

Author avatar is always null in Graphql Queries. It only works with the API call after passing the populate /flat?populate[author][populate][0]=avatar

111Tashmo commented 1 month ago

REST is fine, you can try

jusiho commented 1 week ago

but Graphql is better, how to do it?

jusiho commented 1 week ago

you just need to extend the response, create a field and resolve, in index.js

const extensionService = strapi.plugin("graphql").service("extension");
    extensionService.use(({ nexus }) => ({
      types: [
        nexus.extendType({
          type: "CommentSingle",
          definition(t) {
            t.string("authorFull", {
              type: "UsersPermissionsUserEntityResponse",
              description: "Author full data",
              resolve: async (parent, root, args) => {
                const { toEntityResponse } = strapi.service(
                  "plugin::graphql.format"
                ).returnTypes;
                console.log("parent", parent.author.id);
                const user = await strapi.db
                  .query("plugin::users-permissions.user")
                  .findOne({
                    where: { id: parent.author.id },
                    populate: { profile_image: true },
                  });
                console.log("user", user);

                return toEntityResponse(user);
              },
            });
          },
        }),
      ],