graphql-map-deduplicator
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

graphql-map-deduplicator

mapit() will deduplicate graphql responses by generating a map of every entity that has __typename and id properties. It will pull those out and place them into a new root property __map__ of the response. Entities without those properties are left in place.

unmapit() will remove the __map__ property and place them back into data object. It has an optional parameter {inMemory: true} to unmap the object back in memory. This makes use of getters on the object to define a function that returns the mapped entity itself. This allows for a smaller memory footprint on really large responses.

Server example:

formatResponse: (response, context) => {
  if (context.request.http?.headers.get('gql-map-dedupe') === 'true') {
    response.data = mapit(response.data || {})
    return response
  }
  return null
}

Client example:

const data = unmapit(mappedResponseData)

In memory example:

const data = unmapit(mappedResponseData, { inMemory: true })

Buy Me A Coffee

Package Sidebar

Install

npm i graphql-map-deduplicator

Weekly Downloads

2

Version

0.2.0

License

ISC

Unpacked Size

8.34 kB

Total Files

8

Last publish

Collaborators

  • terloon