import ME_QUERY from '@/graphql/gql/queries/meQuery.gql';
import UPDATE_USER_SETTING from '@/graphql/gql/mutations/updateUserSetting.gql';
import MY_SCHOOL_CLASS_QUERY from '@/graphql/gql/queries/mySchoolClass.gql';

export default {
  methods: {
    updateSelectedClass(selectedClass) {
      return this.$apollo.mutate({
        mutation: UPDATE_USER_SETTING,
        variables: {
          input: {
            id: selectedClass.id
          }
        },
        update(store, data) {
          let meData = store.readQuery({query: ME_QUERY});

          meData.me.selectedClass = selectedClass;

          store.writeQuery({query: ME_QUERY, data: meData});
        },
        refetchQueries: [{
          query: MY_SCHOOL_CLASS_QUERY
        }]
      }).catch((error) => {
        console.warn('failed to update selected class', error);
      });
    }
  },
};
