Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Store function references? #342

Open
adjourn opened this issue Jan 21, 2019 · 0 comments
Open

Store function references? #342

adjourn opened this issue Jan 21, 2019 · 0 comments

Comments

@adjourn
Copy link

adjourn commented Jan 21, 2019

Im aware that this package is getting deprecated but this feature is still alpha in apollo-client.

I need to store function references in client state, is it possible with Apollo?

Example

Native iOS or Android like header component for web (on smaller screen widths). Would be nice to update the header anywhere in app, especially because what icons do on click and what title says usually depends on context/route/state of the world.

Would be so nice to do something like this:

# header component reads this 
# state and renders accordingly
type Header {
  title: String
  subtitle: String
  left: HeaderButton
  right: HeaderButton
}

type HeaderButton {
  icon: String
  callback: Function
}

extend type Query {
  header: Header
}
// React component
function AnywhereInApp(props) {
  // writeData is here for demo purpouse, would
  // be a waste to (try) write on each re-render
  client.writeData({data: {
    header: {
      __typename: 'Header',
      title: 'New Title',
      subtitle: '',
      left: {
        icon: 'back-arrow',
        callback: () => {
          window.history.back();
        }
      },  
      right: {
        icon: 'settings',
        callback: () => {
          openSettingsModal();
        } 
      }
    }
  }});

  function openSettingsModal() {
    // do something
  }

  return (/* ... */);
}

Im currently managing local state with React context which can have any value (including references), I'd be very happy if I could 100% replace context client state management with Apollo.


I have one hack in mind and it's a spicy one:

  • Set callback to window object, e.g to window.myAppName[name]
  • Write callback name (string) instead of reference to store
  • Read callback name from cache among other values
  • Use callback like this window.myAppName[name]()
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant