Relay - set a variable before a request

I am currently working on a page displaying the status of a specific set of "tasks" for a specific user. To get the data, I have the following relay container:

export default Relay.createContainer(TaskReview, { initialVariables: { limit: Number.MAX_SAFE_INTEGER, studentId: null, }, fragments: { task: () => Relay.QL` fragment on Task { id, title, instruction, end_date, taskDataList(first: $limit, type: "subTasks", member: $studentId) { edges { node { id, answer, completion_date, } } }, ... 

To determine which user I want to receive data from, I use:

 componentWillMount = () => { let {relay} = this.props; relay.setVariables({ studentId: this.props.member.id }); } 

However, the problem is that the request is first executed with null , as defined in initialVariables . My backend implementation has it so that if studentId is NULL, it returns data for all members.

As a result, the previous request is executed twice, the first time with all members, and the second time with the given memberId .

This has to do with other things in my componentWillUpdate . In any case, can I pass this variable and get only data for this element in the first request?

Thanks.

+7
reactjs graphql relayjs
source share
1 answer

Guide your application with these initial conditions by passing the variable down the route.

 class TaskReviewRoute extends Relay.Route { static paramDefinitions = { studentId: {required: true}, }; static queries = { task: () => Relay.QL`query { ... }`, }; static routeName = 'TaskReviewRoute'; } <Relay.RootContainer Container={TaskReview} route={new TaskReviewRoute({studentId: '123'})} /> 
+2
source share

All Articles