apollo-link-state icon indicating copy to clipboard operation
apollo-link-state copied to clipboard

how to query local state by Id

Open lifeiscontent opened this issue 6 years ago • 9 comments

I'd like to know what's the proper way to query a collection with apollo-link-state

e.g.

if I have a list of todos which initialize to an empty array, and I write a query as follows:

const cache = new InMemoryCache();
const client = new ApolloClient({
  cache,
  link: new HttpLink({
    uri: "http://localhost:4000/graphql",
    headers: {
      authorization: localStorage.getItem("token"),
      "client-name": "Space Explorer [web]",
      "client-version": "1.0.0"
    }
  }),
  initializers: {
    todos: () => [{ title: "Test", id: v4(), __typename: "Todo" }]
  },
  resolvers,
  typeDefs
});
query GetTodo($id: ID!) {
  todo(id: $id) {
    id
    title
    completed
  }
}

I tried using a cache redirect, but that didn't work.

lifeiscontent avatar Nov 14 '18 00:11 lifeiscontent

@peggyrayzis @stubailo @jbaxleyiii I wish I could help you guys write docs. Is there a way I can directly interact with the apollo team in order to help write docs on Apollo?

lifeiscontent avatar Nov 15 '18 02:11 lifeiscontent

Very surprising that a cache redirect didn't work - that's what I would have suggested! I don't work at Apollo as of 3 months ago, but I can help you get docs PRs merged in!

stubailo avatar Nov 15 '18 02:11 stubailo

Does this work if you try 2.4 version of ApolloClient? Also what v4() returns for id and what if you try this with static id?

jsslai avatar Nov 15 '18 06:11 jsslai

@jsslai it's just a uuid v4. from the uuid npm package.

in theory this should "just work" with a cache redirect, but... it doesn't maybe due to something not being setup in apollo-link-state "yet" I'm using the alpha release @peggyrayzis talked about at GraphQL Summit.

lifeiscontent avatar Nov 15 '18 21:11 lifeiscontent

Any news on this? I also feel a need for something like

query GetTodo($id: ID!) {
  todo(id: $id) {
    id
    title
    completed
  }
}

But I don't really know how to do it. Is there any complex example available for some complex local state with queries with parameters. I only saw local-link-state mutations with parameteres, not queries...

asokani avatar Jan 11 '19 21:01 asokani

For something so basic, information on how to do this is surprisingly hard to come by. I believe this is what you're looking for:

/**
 * Apollo link state resolvers
 */
const resolvers = {
    Query: {
        getTodo: (parent, { id }, { cache, getCacheKey }) => {

			return cache.readFragment({
                id: getCacheKey({ __typename: 'Todo', id: id }),
                fragment: gql`
                    fragment myTodo on todos {
                        id
                        label
                        description
                        complete
                    }
                `,
            });

        }
    },
    Mutation: {}
};

tkambler avatar Jan 14 '19 16:01 tkambler

@tkambler thanks for the solution, but it doesn't solve the case where you call getTodo without all the fields in the fragment it should be deferred to the caller I'd assume.

lifeiscontent avatar Feb 12 '19 01:02 lifeiscontent

Did you ever get the solution you were looking for, @lifeiscontent?

darrylyoung avatar May 02 '19 12:05 darrylyoung

@darrylyoung no unfortunately not

lifeiscontent avatar May 02 '19 22:05 lifeiscontent