Proper way to update author info
jnovak-SM2Dev opened this issue · 14 comments
I'm a bit confused about how the author is handled. I have to pass in the author's email, avatar, etc. What happens if that info changes (ie the user updates their profile)? Do I need to get all comments by the user and update each instance?
Looks like this was somewhat answered here, but how can I add additional fields/extend calls for this plugin for GraphQL?
If you base on Strapi Users simply add fields to them and populate proper ones in your query.
For more detailed suggestion please provide idea you've got.
I guess the larger question would be, what is the proper way to use extensionService
with this plugin? I'm not sure how to override the findAllFlat
and findAllInHierarchy
GraphQL methods to be able to adjust what is returned.
About extensions you can read here but I guess you want to add more fields to User Collection to which we have got relation in the comment schema. After adding fields you should follow the "Populating" chapter in the Strapi Doc.
@cyp3rius I understand how to add fields to users and how to populate. My confusion is around extending GraphQL for comments specifically. For example, this is what i'd expect to be able to do, something like below.
extensionService.use(({ strapi }) => ({
typeDefs: `
type CommentSingle {
authorFull: UsersPermissionsUserEntityResponse
}
`,
resolvers: {
Query: {
findAllFlat: {
resolve: async (parent, args, context) => {
const { toEntityCollectionResponse } = strapi.service(
"plugin::graphql.format"
).returnTypes;
const data = await strapi.services["api::comment.comment"].find(
{}
);
const response = toEntityCollectionResponse(data.results);
return response;
},
},
},
},
}));
Is there an example on how to do this or does this need to be handled a special way? Sorry, it's very confusing and Strapi 4's docs kind of suck.
Let me check that on weekend and back to you with some advice :)
@cyp3rius Thanks! That'd be great. Even you just find a sample somewhere or an article. It's just tough cause Strapi's docs are all over the place with V3 and V4, GraphQL makes it tough, and then you add in the complexity of this plugin having specific calls and types, it's tough to know how to handle things properly.
@SM2DevLLC I did some research and coding. What you expect you should be able to do not as a plugin extension but as part of the register
routing of your Strapi project.
File: src/index.js
'use strict';
module.exports = {
/**
* An asynchronous register function that runs before
* your application is initialized.
*
* This gives you an opportunity to extend code.
*/
register({ strapi }) {
const extensionService = strapi.service("plugin::graphql.extension");
extensionService.use(({ strapi }) => ({
typeDefs: `
type CommentSingle {
authorFull: UsersPermissionsUserEntityResponse
}
`,
resolvers: {
Query: {
findAllFlat: {
resolve: async (parent, args, context) => {
const { toEntityCollectionResponse } = strapi.service(
"plugin::graphql.format"
).returnTypes;
const data = await strapi.services["api::comment.comment"].find(
{}
);
const response = toEntityCollectionResponse(data.results);
return response;
},
},
},
},
}));
},
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
*
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
bootstrap(/*{ strapi }*/) { },
};
Is that what you expect to do?
@cyp3rius Yes that's what i'm already doing. That's where the code is. It just doesn't work. I think part of it is the ["api::comment.comment"]
portion. I think it may need to be ["plugin::comment.comment"]
or something along those lines.
Please add the like feature, manage likes for each comment. I think adding the like feature will make your plugin more perfect
@cyp3rius It looks like the authorUser
relationship exists, it just isn't being allowed to be selected in GraphQL queries. Can you please enable this? If you do, it'll be easy for people to select that as a return field and even extend it.
Looking into that