-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow "kind of" middlewares for extending user model before returning it to the client #995
Comments
I see, well the first thing that is coming to my mind for a generic solution would be to actually fetch the user yourself inside the graphql context. Right now we inject the user (doing a db call) inside the context, I think a good option would be to actually skip this call and just inject the user id to the context. Then you can fetch your user as you want so with mongoose in your case. It could look like this: const server = new ApolloServer({
schema,
context: async ({ req, connection }: any) => {
const accountJsContext = await accountsGraphQL.context({ req }, { skipUserLoadOrWhateverOptionName: true });
let user;
if (accountJsContext.userId) {
user = await myMongooseUserModel.findOneById(accountJsContext.userId)
}
return {
...accountJsContext,
user
};
})
}); The same behavior can also be applied to rest :) What do you think? |
This is an option but definitely looks like too much overhead :D |
@pradel yeah, for getting correct mongoose model in context we already do sth like this export async function getWrappedAccountsContext(session) {
const accountsContext = await accounts.context(session);
return {
...accountsContext,
user: accountsContext.user ? User.hydrate(accountsContext.user) : null,
};
} And calling it inside context creator fn. My question was more about user models which are returned from queries/mutations. E.g. Query: {
// Override this mutation to return mongoose model which we already have in context
getUser(_, __, { user }) {
return user || null;
},
}, or const res =await AccountsMutation.authenticate(...);
if (res.user) res.user = User.hydrate(res.user);
return res |
@nsine okay I guess it's something not easy to do currently and probably the best way to achieve this would be to have a mongoose database adapter. But that would require to maintain one more database adapter. I was taking a look and this is the choice other projects did. |
Another option would be to create a directive that hydrates the user. |
Feature request
Is your feature request related to a problem? Please describe.
Will try to explain what i mean because not sure how clearly describe in the header 😅
On our project we use GraphQL, MongoDb and Typegoose/Mongoose as ORM. We don't use raw mongodb objects anywhere as we expect everything is wrapped to Mongoose model. In any resolver that expect user as a
parent
parameter - we expect it's mongoose document so we can use virtual getters/methods on it. However that's not true with accounts-js as all it's resolvers return raw mongodb object.And e.g. if I have mongoose model
and a graphql type
if i query a user with default
getUser
mutation i won't be able to queryisEmailVerified
field as the user is raw object, not mongoose modelDescribe the solution you'd like
Currently like a workaround i manually wrap user into mongoose in each resolver
But wondering if that's a useful feature to allow customize how result
User
object should look like.E.g. i pass somewhere
modifyUser = (user) => User.hydrate(user)
to automatically wrap it to what i need.I didn't came to some exact solution as i'm not sure to which part of layered architecture we can add such middleware.
DatabaseInterface
likedatabase-mongo
butdatabase-mongoose
(seems to be too much overhead)Let me know what do you think about this (i'm sure this is useful for graphql+mongoose users). And if you have any ideas how we can implement that. I can help with implementation afterwards.
Thanks ❤️
The text was updated successfully, but these errors were encountered: