Commit d2db12ed authored by Marek Veselý's avatar Marek Veselý
Browse files

Merge branch 'fix-double-exercise-creation' into 'main'

fix: double exercise creation event

See merge request inject/frontend!669
parents d5e8c995 0c498193
Loading
Loading
Loading
Loading
+21 −7
Original line number Diff line number Diff line
@@ -594,7 +594,6 @@ const cache: Exchange = offlineExchange<GraphCacheConfig>({
        })
      },
      exercisesSubscription({ exercisesSubscription }, _args, cache) {
        console.log('exercisesSubscription', exercisesSubscription)
        const { exercise: newExercise, eventType } = exercisesSubscription || {}
        if (!newExercise || !eventType) {
          return
@@ -602,18 +601,33 @@ const cache: Exchange = offlineExchange<GraphCacheConfig>({

        const params = ['Query', 'exercises'] as const
        switch (eventType) {
          case 'CREATE':
            {
          case 'CREATE': {
            const linkNot = cache.resolve(...params) || []
            if (!Array.isArray(linkNot)) {
              throw Error('Generic typing error, look up exercisesSub')
            }
            /*
             * only add the exercise if it does not exist;
             * this can happen when the exercise is created:
             * 1. the MODIFY event is sent because the creator is assigned as an instructor
             * 2. the CREATE event is sent because the exercise is created
             *
             * the CREATE event has to stay, because other clients have to be
             * informed about the new exercise (and they will not have the MODIFY event)
             */
            if (
              !linkNot.some(
                exerciseKey =>
                  cache.resolve(exerciseKey, 'id') === newExercise.id
              )
            ) {
              cache.link(...params, [
                ...linkNot,
                { __typename: 'ExerciseType', id: newExercise.id },
              ])
            }
            break
          }
          case 'DELETE': {
            const linkNot = cache.resolve(...params) || []