strapi-plugin-comments icon indicating copy to clipboard operation
strapi-plugin-comments copied to clipboard

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

Open jainsuneet opened this issue 1 year ago • 3 comments

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

jainsuneet avatar Aug 06 '23 17:08 jainsuneet

REST is fine, you can try

111Tashmo avatar May 14 '24 13:05 111Tashmo

but Graphql is better, how to do it?

jusiho avatar Jun 20 '24 05:06 jusiho

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);
              },
            });
          },
        }),
      ],

jusiho avatar Jun 20 '24 13:06 jusiho