Gqlgen: Add support for Automatic Persisted Queries

Created on 28 Oct 2018  路  12Comments  路  Source: 99designs/gqlgen

This is more of a feature request than a bug, and I'd be willing to do the work if it's something people are interested in.

Apollo added this neat feature which allows for dynamic query caching.

This would require an addition to the parsing logic so that a request without a query would still work. For example:

{
  operationName: 'MyQuery',
  variables: null,
  extensions: {
    persistedQuery: {
      version: 1,
      sha256Hash: hashOfQuery
    }
  }
}

currently fails at https://github.com/99designs/gqlgen/blob/1b17b5a2489094911e701a299aedee3d1d1a2319/handler/graphql.go#L287,

Instead this would change to not send a 422, but rather a 200 with { message: 'PersistedQueryNotFound' }

enhancement

Most helpful comment

Persisted queries are great. Hope gqlgen gets them.

It makes the client code and refactoring in general much smoother.

I was even thinking of checking them at client compile time or at least an integration test at test time.

All 12 comments

It's something we've talked a bit about internally and a few others have expressed interest. It needs a little thought and care around the interfaces for saving and fetching queries, as these will need to actually be persisted.

I was imagining a first pass would just be to augment the LRU being used to also store a hash. Not great for storing lots of queries, but would probably work well without drastically changing the performance profile of the app.

My current understanding of APQ is roughly like this:

  1. In CI you enumerate all queries, and register the query with a central database, which returns the hash
  2. Store the hash against the query locally somewhere, committed in your client repo
  3. at query send the hash instead of the query
  4. all running servers need to be able to lookup the registered query from a central data source

Without some kind of persistent storage, shared by all the servers in the pool, this wont work.

That's how relay works, APQ though is different.

apq

Basically Apollo first hashes the query, and sends the operation name and a hash. If the server knows that hash + operation name combo, it returns the results, otherwise it returns a unique error that says either:

1) I don't support persisted queries
2) I haven't stored that hash yet

if #1, the client stops sending hashes. if #2, the client sends the full query, with the hash it expects it to be stored at. There isn't any shared storage between the client and the server.

Nice pic :+1:

Ah, thats interesting. It means more another round trip on the first page load, but if the server has warmed up it should be better. I still think the backend probably needs to be plugable here so if the server can have some persistence so the clients don't get hit with the extra round trip every time.

Persisted queries are great. Hope gqlgen gets them.

It makes the client code and refactoring in general much smoother.

I was even thinking of checking them at client compile time or at least an integration test at test time.

this will reduce client's payload amazingly.
i wish someone to contribute for it. I will try when available

I think this would be extremely useful. I wonder is this on the roadmap somewhere?

I can confirm this would be great to have. The major benefit for us would be that it allows us to transform complex POST requests into GET requests, which can then be cached by a CDN.

It seems like this feature is now implemented, so should this issue be closed?

As an extension to APQ, it'd be great if APQ can be "enforced". That is, query whitelisting based on hashes. Unless I'm mistaken, the current behaviour would fallback to executing the query if it is not in the cache.

Implemented on my installation, and works great! @MrSaints you should file another bug for a seperate feature request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ksoda picture ksoda  路  3Comments

bjm88 picture bjm88  路  4Comments

huanghantao picture huanghantao  路  3Comments

cemremengu picture cemremengu  路  3Comments

theoks picture theoks  路  3Comments