import SCROLL_POSITION from '@/graphql/gql/local/scrollPosition.gql';
import HELLO_EMAIL from '@/graphql/gql/local/helloEmail.gql';
import SIDEBAR from '@/graphql/gql/local/sidebar.gql';

export const resolvers = {
  Mutation: {
    scrollTo: (_, {scrollTo}, {cache}) => {
      const data = cache.readQuery({query: SCROLL_POSITION});
      data.scrollPosition.scrollTo = scrollTo;
      cache.writeQuery({query: SCROLL_POSITION, data});
      return data.scrollPosition;
    },
    helloEmail: (_, {email}, {cache}) => {
      const data = cache.readQuery({query: HELLO_EMAIL});
      data.helloEmail.email = email;
      cache.writeQuery({query: HELLO_EMAIL, data});
      return data.helloEmail;
    },
    toggleSidebar: (_, {sidebar: {profile, navigation}}, {cache}) => {
      const data = cache.readQuery({query: SIDEBAR});
      if (typeof profile !== 'undefined') {
        data.sidebar.profile = profile;
      }
      if (typeof navigation !== 'undefined') {
        data.sidebar.navigation = navigation;
      }
      cache.writeQuery({query: SIDEBAR, data});
      return data.sidebar;
    }
  }
};
