Skip to content

takashito/ew-graphql-with-edgekv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ew-graphql-with-edgekv

REST API federation by Akamai EdgeWoker Graphql Service with additional Object Caching layber using EdgeKV

ew-graphql-with-edgekv

How it work

there are 3 layers of caching

1, REST API Caching at Edge Server

REST API response will be cached at Edge Server
when data are responded from this cache, you can confirm it from Debug info {cacheHit:EDGE_MEM_HIT or EDGE_HIT etc)

2, Object Data Caching at EdgeKV

Object data will be extracted from REST API response, and saved to EdgeKV per object id.
Each object has set TTL (shorter than REST API TTL).
this cache will be used to create response to client until it expires TTL
when objects are responded from this cache, you can confirm it from Debug info {cacheHit:KV_HIT)

3, On Memory cache

once Object data is retrieved from EdgeKV cache, then it will be saved in EdgeWorker's memory space.
this cache will be used to create response to client until it expires TTL
when objects are responded from this cache, you can confirm it from Debug info {cacheHit:KV_MEM_HIT)

REST APIs

3 REST API to federate and turn into Single Graphql Service
I have used mockapi servece "My JSON Server" from typecode

Books : https://ewdemo.test.edgekey.net/mockapi/books
Authors : https://ewdemo.test.edgekey.net/mockapi/authors
Publishers : https://ewdemo.test.edgekey.net/mockapi/publishers

Database : https://ewdemo.test.edgekey.net/mockapi/db
Sorce Code : https://github.com/takashito/mockapi

Federated Graphql Schema

  type Author {
    id: ID!
    name: String!
    books: [Book!]!
    debug: Debug
  }
  
  type Book {
    id: ID!
    name: String!
    publisher: Publisher!
    authors: [Author!]!
    debug: Debug
  }
  
  type Publisher {
    id: ID!
    name: String!
    books: [Book!]!
    debug: Debug
  }

  type Debug {
    url: ID!
    status: String!
    latency: String
    cacheable: String
    cacheKey: String
    cacheHit: String
  }

  type Query {
    authors: [Author!]!
    author(id: ID!): Author!
    books: [Book!]!
    book(id: ID!): Book!
    publishers: [Publisher!]!
    publisher(id: ID!): Publisher!
  }

Now you can access your data with single graphql endpoint. Routing to rest api to fullfill query will be done by graqhpl engine.

when you hit these example URLs, you may see error due to EdgeWorker initialization timeout, but after hitting URL 2,3 times, it should work.

Example URLs without Debug info

https://ewdemo.test.edgekey.net/edgekv/graphql?query={book(id:3){name,authors{name},publisher{name}}}
https://ewdemo.test.edgekey.net/edgekv/graphql?query={publishers{name,books{name}}}
https://ewdemo.test.edgekey.net/edgekv/graphql?query={authors{name,books{name}}}
https://ewdemo.test.edgekey.net/edgekv/graphql?query={books{name,authors{name},publisher{name}}}

Example URLs with Debug info

https://ewdemo.test.edgekey.net/edgekv/graphql?query={book(id:3){name,debug{url,cacheHit,cacheKey},authors{name,debug{url,cacheHit,cacheKey}},publisher{name,debug{url,cacheHit,cacheKey}}}}
https://ewdemo.test.edgekey.net/edgekv/graphql?query={publishers{name,books{name},debug{url,cacheKey,cacheHit}}}
https://ewdemo.test.edgekey.net/edgekv/graphql?query={authors{name,books{name,debug{url,cacheKey,cacheHit}}}}
https://ewdemo.test.edgekey.net/edgekv/graphql?query={books{name,debug{url,cacheHit},authors{name,debug{url,cacheHit,cacheKey}},publisher{name,debug{url,cacheHit}}}}

Debug Info

  • url: REST API called by graphql engine
  • cacheable: REST API is cacheable or not
  • cacheKey: Akamai CacheKey or EdgeKV path
  • cacheHit:
    • KV_MEM_HIT : Object Data was responded from EdgeKV cache stored in EdgeWorker's Memory space
    • KV_HIT : Object Data was responded from EdgeKV cache
    • EDGE_MEM_HIT : REST API was responded from Edge memory cache
    • EDGE_HIT : REST API was responded from Edge disk cache
    • REMOTE_MEM_HIT : REST API was responded from parent memory cache
    • REMOTE_HIT : REST API was responded from parent disk cache
    • CACHE_MISS : REST API was responded from mock api server (origin)

Known EW Issue

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published