1- import ApolloClient from 'apollo-boost'
1+ import ApolloClient , { InMemoryCache } from 'apollo-boost'
22
33const client = new ApolloClient ( {
44 uri : process . env . REACT_APP_GQL_URI ,
5+ headers : {
6+ Authorization : process . env . GQL_AUTH_TOKEN ,
7+ } ,
8+ cache : new InMemoryCache ( ) ,
9+ resolvers : {
10+ Mutation : {
11+ setStatus : ( _root , variables , { cache, getCacheKey } ) => {
12+ // TODO: optimize status setting to act on diffs
13+
14+ // set local cache
15+ function set ( typename : string , id : string , status : 'ACTIVE' | 'COMPLETE' ) {
16+ const writeId = getCacheKey ( { __typename : typename , id } )
17+ const data = { status }
18+ cache . writeData ( { id : writeId , data } )
19+ }
20+
21+ const { progress, position } = variables
22+
23+ // set level progress & active
24+ for ( const levelId of Object . keys ( progress . levels ) ) {
25+ set ( 'Level' , levelId , 'COMPLETE' )
26+ }
27+ set ( 'Level' , position . levelId , 'ACTIVE' )
28+
29+ // set stage progress & active
30+ for ( const stageId of Object . keys ( progress . stages ) ) {
31+ set ( 'Stage' , stageId , 'COMPLETE' )
32+ }
33+ set ( 'Stage' , position . stageId , 'ACTIVE' )
34+
35+ // set step progress & active
36+ for ( const stepId of Object . keys ( progress . steps ) ) {
37+ set ( 'Step' , stepId , 'COMPLETE' )
38+ }
39+ set ( 'Step' , position . stepId , 'ACTIVE' )
40+
41+ return null
42+ } ,
43+ } ,
44+ } ,
545} )
646
747export default client
0 commit comments