Meteor connections into Apollo resolvers


#1

Hi Folks,

I’m currently working on a big multi-tenant app. We started developping it with the meteor’s cassical way (methods and pub/sub), but we quickly found that such a big app would need a much more relational data-management system, so we decided to rework some modules with Apollo / GraphQL.

A user can be a member of several teams, but he can work on a single team at a time in a single tab, as the current team’s id is stored in redux. And as sending the current team’s id on every method call would be a huge pain, we simply save the current team the user’s working on in the server’s connection object

Meteor.methods({
    setWorkingTeamForThisConnection( teamId ){
        this.connection.currentTeam = teamId;
    },

    addUserToTheCurrentTeam( userId ){
        addUserToTeam( userId, this.connection.currentTeam );
    }
});

And it works great ! It even allow the user to have multiple tabs opened on different teams without any problem ! But now that we are switching to Apollo, I’d like to get this info in my resolvers, but I can’t find a way to get access to the user’s current connection object.

How could I do so ?
If I can’t, what would be an elegant work-around for this usecase ? I’d like not to have to send the current team id in each resolver…

Thanks you, and sorry for my poor english !


#2

You can add context to ApolloLink and parse this context in resolvers…
Or even better https://www.apollographql.com/docs/link/links/context.html


#3

Could you give mesome insights on how to use setContext ?

What I did :

Meteor.call( 'pairTeamToCurrentConnection', currentTeamId, ( error, result ) => {
			console.log( "pairTeamToCurrentConnection" );
			if ( error ) {
				console.log( error );
				Alert.error( error.reason );
			} else {
				console.log( "result cbk" );
				setContext( ( request, previousContext ) => {
					console.log( " setContextLog ", request, previousContext );

					const newCtx = ({ currentTeamId });
					return newCtx
				} );
				this.props.changeCurrentTeam( currentTeamId );
			}
		} );

But when I actually execute this code on the client, the “setContextLog” log is never executed, the whole setContext callback stay unexecuted. And when after this I execute a graphql query / mutation, the resolver context has nothing more than the userId and the user document.

Unfortunately, there is not much documentation yet about apollo on google.