Skip to content

[bestpractices] Add caching section to talk about identifiers #70

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

Merged
merged 1 commit into from
Sep 14, 2016

Conversation

dschafer
Copy link
Contributor

Add a caching section, talking about how to replace a URL-based cache. This mentions globally unique identifier as a recommendation, describes how to use additional fields for backwards compatibility, then talks about how you can also allow the client to derive this, which is ideal in situations where existing APIs already have an established pattern for what ID means.

@ghost ghost added the CLA Signed label Sep 13, 2016
@dschafer
Copy link
Contributor Author

Goal here was to have the article establish three major things:

  • You really, really want the client to be able to derive a globally unique ID, for caching.
  • If the server can provide that ID? That's a pretty short path to the above, do that.
  • There server does not have to provide that ID, though. If the server doesn't, have a plan for how the client will derive it.

@stubailo - really interested in your thoughts on this one , I tried to incorporate some of what you wrote up in https://medium.com/apollo-stack/graphql-at-facebook-by-dan-schafer-38d65ef075af#.x1mwck563

@stubailo
Copy link
Contributor

Going to take a look in a bit!

@lacker
Copy link
Contributor

lacker commented Sep 14, 2016

I don't think you need a globally unique id for caching per se. You pretty much always have an object type, so as long as you have a per-type-unique id, it should be fine for caching. Ember.js requires this, for example - in the ember store, every object needs both a type and a per-type-unique-id. So to me this is one way to do it but I wouldn't really describe it as best practices, to think in terms of "globally unique ids".

@stubailo
Copy link
Contributor

@lacker yeah that's about what we do in our example app: https://github.com/apollostack/GitHunt-React/blob/a1756d31ac5f6c1d048eab642b5b507c91c302c2/ui/client.js#L43

I think the way this section is worded has enough caveats that it makes sense to me. I think it's reasonable to say that life becomes a bit easier if you have unique IDs provided by the server, but this page reinforces that it's not a hard requirement.

@lacker
Copy link
Contributor

lacker commented Sep 14, 2016

OK, YOLO let's run with it 🏃

permalink: /learn/caching/
---

> Providing Object Identifiers allows clients to build rich caches
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree that this is associated with whether or not it is possible to build a rich cache, since it's definitely achievable without one. So I think this is a best practice, but not because it's easier to cache - it just makes your API cleaner since you have a consistent source of IDs for all objects.

@leebyron leebyron merged commit 383e5cf into new-source Sep 14, 2016
@leebyron leebyron deleted the bestpractice branch September 14, 2016 03:21
@leebyron
Copy link
Collaborator

This looks good.

Something to mention just on this thread is that we've been toying with a __id meta-field which would expose a globally unique ID, so it's not too crazy to assert it as a best practice for now.

@stubailo
Copy link
Contributor

Yeah for sure.

@taion
Copy link

taion commented Sep 21, 2016

I'm not sure if this is relevant here, but UUID-based caches are in general not enough for Relay-style patterns that allow refetching based on these IDs. I guess maybe this is one of those things that Relay 2 fixes and makes non-existent, but I had to think a bit before realizing why I couldn't just use my UUIDs as my global IDs for my Relay-compatible GraphQL server.

jonirrings added a commit to GraphQL-China/graphql-china.github.io that referenced this pull request Aug 17, 2017
* APIReference-Utilities translation done

Signed-off-by: Jonir Rings <[email protected]>

* 修正用词,添加几个标题的翻译

Signed-off-by: Jonir Rings <[email protected]>

* 统一“根模块”这个用词

Signed-off-by: Jonir Rings <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants