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

Add helper methods to read Apollo data from store directly #227

Closed
stubailo opened this issue May 19, 2016 · 8 comments
Closed

Add helper methods to read Apollo data from store directly #227

stubailo opened this issue May 19, 2016 · 8 comments
Milestone

Comments

@stubailo
Copy link
Contributor

This probably just means nicely packaging the following:

  • readQueryFromApolloState
  • readFragmentFromApolloState

One question is, where do we get the config from? Since the configuration is currently passed into new ApolloClient(), we could do it like:

const client = new ApolloClient(...);

// somewhere else...

client.readQueryFromApolloState(state.apollo, gql`... query ...`, options);

options would be stuff like whether to return partial results, I guess.

Fun fact - with just a little bit of massaging these methods could eventually be modified to let you use GraphQL queries to access your client-side state as well, as long as it fits the correct normalized form!

@nevir
Copy link
Contributor

nevir commented Jul 15, 2016

facebook/relay#114 might have some relevant insights here, too

@jbaxleyiii
Copy link
Contributor

@stubailo is this still planned?

@Poincare
Copy link
Contributor

@jbaxleyiii Sort of, although the readObjectById that is currently a PR is similar to but not exactly the same as readFragmentFromApolloState. The readQueryFromApolloState seems to be the same thing as the noFetch option that was added.

@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
@SachaG
Copy link

SachaG commented Nov 3, 2016

I just ran into a use case for this, where I have to access the Apollo data from outside the React component tree. I actually managed to do it with the noFetch option, but my problem is that even with noFetch, query still returns a Promise, whereas I'm trying to get the results returned as a plain JS object.

In other words, I'm trying to replicate the behavior of Meteor's Collection.find().fetch() and I'm almost there but I'm just not sure what to do with that Promise object…

@SachaG
Copy link

SachaG commented Nov 3, 2016

FYI I ended up accessing client.store.getState().apollo.data directly, which works better for my purposes.

@stubailo
Copy link
Contributor Author

stubailo commented Nov 3, 2016

There is .watchQuery(...).currentResult() now, which runs synchronously. Would that be better? Curious what the use case is.

@SachaG
Copy link

SachaG commented Nov 3, 2016

Oh maybe, I'd have to try it out. And the use case is getting a list of categories from the store to populate a <select> form element (in Telescope's Autoform-like form module):

https://github.com/TelescopeJS/Telescope/blob/apollo/packages/nova-categories/lib/custom_fields.js#L31-L45

@helfer
Copy link
Contributor

helfer commented May 3, 2017

Done 🎉

@helfer helfer closed this as completed May 3, 2017
jbaxleyiii pushed a commit that referenced this issue Oct 17, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 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

7 participants