Using @client data as variables #231
Comments
You could make a
client = new ApolloClient({ cache, …})
cache.myClient = client;
query LocalCart() {
localCart @client {
items {
name
price
}
}
totalPrice
} 2.b. write your resolver, extract It's a good amount of boilerplate, but should work? |
Thanks, @fbartho, that is certainly a way to do it! What I am a bit worried about is attaching the client to the cache. From what I can understand, you have done it because there is no other way of getting access to the client in the resolvers. I would much rather be able to attach the client directly to the context. |
Another question - how do you pass along the inner part of the query to the
And that this can change dynamically by the incoming query to |
@tutturen I'm not sure how easy it would be, you possibly could examine the other args to your resolver -- As a fallback, you probably can get the string representation of the query off of the operation, and then with a regex that looks for Re: "there is no other way" Please bump this issue: apollographql/apollo-link#527 -- I want it to be easier than manually hooking it in. |
@fbartho Thanks, those are good suggestions. Although I am getting the feeling that becomes a hack upon a hack. As a consumer of this library, I don't want to deal with that. In the server graph, you can freely define edged between your nodes. In the local (client) graph, you can also define resolvers as edges between your local nodes. In server graphQL queries in But you can't query any server data in a client query. Unless you do as @fbartho suggests - attach the client to the cache and parse the inner query with regex. I would also like with a better API for it! Ideally, I would like to have a binding attached to the incoming context object, which you could easily pass along the
|
I just spent all day trying to figure out the syntax for setting a variable from the @client cached data so I can use the current ID which is saved locally and needed in a bunch of queries throughout the app. I was totally convinced something like this would totally work and was missing from the documentation if I could just find the right syntax: gql`query CurrentStoreQuery($id: ID! = app.currentStoreId @client) {
store(id: $id) {
id
title
}
}` |
I think I just found the proposed solution for this - using the It is explained in this video by Lee Byron: This means that I (in theory) could write a query like this:
|
Quick note - this is related to #168 (and is something we're planning on addressing for the 1.0 release). |
Stumbled on the same problem. Is this a thing yet ? |
@tutturen I have the same problem. I kept the category_ids in the local cache via link-state and I need to use it as an option variable via query. did you find a solution? |
Here is the example.
Here, |
To use variables in the local cache, or the need for an extra query, is precisely what I was trying to avoid. The team have acknowledged the problem, and will hopefully implement the feature in the 1.0 release. |
I found a solution for this. Here is the code.
|
If I am not wrong, that solution will execute two queries, which is defeats the core of GraphQL specified on graphql.com:
I appreciate that you are trying to help me, I really do. I have a working solution, but this is more about the inconvenience of having to execute two queries. |
composing the multi queries is completely fine and they are chained together. FYI, composing make more sense to me since we can compose the complex state via link-state. @export can be a solution for simple state but in practice, we have more complex structure. |
Any further info on this? I would like to use something like the following (I have a currentCar in local state, and want to use its id in a sub-detail page query):
|
@ak-rcg This functionality is now available in |
Thanks a bunch @hwillson ! |
I have this query, which fetches a list of item ids from local storage (
cartItemIds = [1, 3, 5]
)This data is then passed to this query which asks the server to calculate the cart based on the ids.
While this works, it does not sit right with me. Does the GraphQL way of "ask for what you want" apply here? I have to ask twice - first from the local cache, and then from the server.
Is there a better solution for achieving this?
The text was updated successfully, but these errors were encountered: