apollographql / meteor-integration

🚀 meteor add apollo
http://dev.apollodata.com/core/meteor.html
108 stars 45 forks source link

Feature Request: Ability to add to context after user is added, but before being returned to graphqlExpress #99

Closed todda00 closed 7 years ago

todda00 commented 7 years ago

I would like to modify the context after the user is added. I need to add some permission data which requires both the user object and a different object I have added to the context via options.context.

Since I am using this permission data in many resolvers, I would like to avoid a utility function in all resolvers and include this data in the context.

I am thinking the way to do this is providing a hook function in the customOptions object:

createApolloServer({
  modifyContext(context){
    ... change the context as needed
    return context;
  },
});

Or if the location where the context is added from customOptions.context, this option could be provided as a function which accepted the current context and returns the desired one. ( this is probably a cleaner approach if moving the context merge to after the user is added is acceptable)

createApolloServer({
  context(context){
    ... change the context as needed
    return context;
  },
});

Either way this will have to hook in here: https://github.com/apollographql/meteor-integration/blob/master/src/main-server.js#L93

I can come up with a PR if we can determine what we want the API to work like.

lorensr commented 7 years ago

modifyContext looks good to me. @xavcz ?

Might be cleaner to copy only the relevant options over here:

https://github.com/apollographql/meteor-integration/blob/master/src/main-server.js#L80

todda00 commented 7 years ago

I started working on this modifying context to accept either an object or a function, it just feels like a redundancy providing both context and modifyContext. I am working on it this way unless you strongly feel adding modifyContext is the way to go.

lorensr commented 7 years ago

Would the fn be called after the user is added?

if customOptionsObject.context is an object
  copy it over

addCurrentUserToContext(options)

if customOptionsObject.context is a function
  options.context = customOptionsObject.context(options.context)
xavxyz commented 7 years ago

PR merged, thanks a lot @todda00 @lorensr! Package published 👍