Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copying query result into another point in store. #525

Closed
tomitrescak opened this issue Aug 10, 2016 · 4 comments
Closed

Copying query result into another point in store. #525

tomitrescak opened this issue Aug 10, 2016 · 4 comments
Milestone

Comments

@tomitrescak
Copy link

tomitrescak commented Aug 10, 2016

In alignment to discussion in #98 this problem seems not to be tackled yet. Is there a possibility to redirect or store query results in another point in the redux store.

I currently have it working my way by intercepting apollo actions in my reducers and copying it into the store there. Yet, this approach duplicates data (which may be alright I guess).

Reducer

export default function reducer (state = { exercises: {}}, action: any) {
  switch (getQuery(action)) {
    case 'exercise': // i can detect the query name and copy results where I want in the given reducer
      return copyQuery(state, 'exercises', action.result.data.exercise);
    case 'practical':
      if (action.result.data.practical) {
        return copyQuery(state, 'exercises', action.result.data.practical.exercises, '_id', false);
      }
      break;
  }
}

Helpers

const QUERY_INIT = 'APOLLO_QUERY_INIT';
const QUERY_RESULT = 'APOLLO_QUERY_RESULT';
const queryMappings: any = {};
const regExQuery = /query ([\w_]*)/;

function matchQueryName(qs: string) {
  let result = regExQuery.exec(qs);
  if (result) {
    return result[1];
  }
  return regExBare.exec(qs)[1];
}

export function getQuery(action: any) {
  if (action.type === QUERY_INIT) {
    if (!queryMappings[action.queryId]) {
      // add query with that name into the query index
      queryMappings[action.queryId] = matchQueryName(action.queryString);
    }
  } else if (action.type === QUERY_RESULT) { // || action.type === QUERY_RESULT_CLIENT) {
    return queryMappings[action.queryId];
  }
  return null;
}

Would this be a valid approach, or is there something better?

There is a set of tools at https://github.com/tomitrescak/apollo-redux-tools you can check out that is currently heavily undocumented, but am working on documentation today.

@helfer
Copy link
Contributor

helfer commented Sep 6, 2016

@tomitrescak What's the specific use-case you have for this? It seems to me that this isn't a feature that should be in the core of apollo client, but that instead we should make it easy to configure or replace the mechanisms that interact with the store, which would then also let us support other stores like MobX or ngrx.

@tomitrescak
Copy link
Author

I'm using this with optimistic updates. But with the latest apollo-react I am using this less and less as new version has optimistic updates and query manipulation.

Yet, there are still use cases when I need to remap the query result to a different point in store when I'm doing more complex forms and I need query result for various UI updates through redux store.

@helfer helfer added the feature label Sep 19, 2016
@stubailo stubailo added this to the New API/Refactor milestone Sep 29, 2016
@helfer helfer modified the milestones: Post 0.5, Release 0.5 Oct 26, 2016
@madebyherzblut
Copy link

Our use-case is to duplicate data to a different data structure for faster reads and less re-rendering (e. g. one reducers manages a hash map of id<->shift and another startDate<->shift). At the moment we also take what we need from QUERY_RESULT, but it relies on string splitting and is therefore a bit cumbersome.

I believe that a more general way to hook into the store management of apollo would solve this use-case very nicely 👍

@helfer
Copy link
Contributor

helfer commented Nov 17, 2016

This should be possible with #921 and/or result/reducers. It's not as straight forward as you might like, but I think together with custom redux reducers it's good enough for now.

@helfer helfer closed this as completed Nov 17, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2023
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

4 participants