Dove CLI Generated Boilerplate Confusion #2711
-
First... Is there any plan to lean down the templates or abstract out default resolvers and schema functions and allow for overrides? Now my team will have to learn what to do with 4 schemas, 4 resolvers, and 4 hook types. Before... 80% of building service was writing hooks in separate files and layering in the structure in the .hooks file. One of the benefits to Feathers that my teams liked was that it was easy to pickup and hard to screw up. I'm worried about the added upfront complexity leading to worse code and influencing adoption. Second...
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
As you mentioned I think a few of those points are things that should be added to the documentation specifically to the CLI guide (https://dove.feathersjs.com/guides/cli/index.html) I am working on. Since hooks still work as is (in addition to the new - but as always entirely optional - hook type that allows to control a before, after and error flow in the same hook function), everything done in schemas and resolvers can still also be done in hooks (in fact, both schema validation and resolvers are just a hook and part of the flow as can be seen e.g. in https://github.com/feathersjs/feathers-chat/blob/dove/typescript-api/src/services/messages/messages.class.ts#L10). A basic description of the hook flow can be found in https://dove.feathersjs.com/api/hooks.html#hook-flow - I am talking to our designer about adding more diagrams for this as well. However schemas and resolvers do address several things that have been a persistent source of confusion and potential security problems in Feathers and that were causing issues both for beginners and in larger more advanced app:
As an example, here is the populating the user code from the v4 chat guide: // Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html
import { Hook, HookContext } from '@feathersjs/feathers';
export default (): Hook => {
return async (context: HookContext) => {
// Get `app`, `method`, `params` and `result` from the hook context
const { app, method, result, params } = context;
// Function that adds the user to a single message object
const addUser = async (message: any) => {
// Get the user based on their id, pass the `params` along so
// that we get a safe version of the user data
const user = await app.service('users').get(message.userId, params);
// Merge the message content to include the `user` object
return {
...message,
user
};
};
// In a find method we need to process the entire page
if (method === 'find') {
// Map all data to include the `user` information
context.result.data = await Promise.all(result.data.map(addUser));
} else {
// Otherwise just update the single result
context.result = await addUser(result);
}
return context;
};
} There is a lot going on here and you kind of really have to know about Feathers internals - e.g. how to process the data differently on In a resolver it looks like this: // Resolver for the data that is being returned
export const messagesResultResolver = resolve<MessagesResult, HookContext>({
schema: messagesResultSchema,
validate: false,
properties: {
user: async (_value, message, context) => {
// Associate the user that sent the message
return context.app.service('users').get(message.userId)
}
}
}) I found this pattern repeated in many different places that I used schemas in. A multi line hook or even an entire separate library (like authentication hooks or the various populate libraries) could be replaced with a one- or two-line resolver. If felt very similar to how the Feathers service concept got rid of a lot of the boilerplate you'd have to do in other more traditional API frameworks. As for how to look at it conceptually my rule of thumb (that I will also document more) is:
|
Beta Was this translation helpful? Give feedback.
As you mentioned I think a few of those points are things that should be added to the documentation specifically to the CLI guide (https://dove.feathersjs.com/guides/cli/index.html) I am working on. Since hooks still work as is (in addition to the new - but as always entirely optional - hook type that allows to control a before, after and error flow in the same hook function), everything done in schemas and resolvers can still also be done in hooks (in fact, both schema validation and resolvers are just a hook and part of the flow as can be seen e.g. in https://github.com/feathersjs/feathers-chat/blob/dove/typescript-api/src/services/messages/messages.class.ts#L10). A basic description of…