{"_id":"@cran/gql.link.merge","_rev":"1-31c58953d64729293c81db029fd5dd8b","name":"@cran/gql.link.merge","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@cran/gql.link.merge","version":"0.0.1","description":"Apollo Link to Merge GQL Queries","main":"out/index.js","module":"out/index.js","typings":"out/index.d.ts","scripts":{"clean":"rm -rf out","pretest":"pnpm run clean && pnpm install","test":"eslint .","prebuild":"pnpm install && pnpm run test","build":"tsc -d","prepublishOnly":"pnpm run build"},"repository":{"type":"git","url":"git@gitlab.com:c6s/lib/gql/merge-link.git"},"keywords":["cranberry"],"author":{"name":"Cranberry"},"license":"CC-BY-ND-4.0","peerDependencies":{"@cran/util.gen":"0.x >0.0.1","@apollo/client":"3.x","graphql":"15.x"},"devDependencies":{"@apollo/client":"3.x","@cran/eslint-plugin":"0.x","@cran/tsconfig":"0.x","@cran/util.gen":"0.x >0.0.1","@types/node":"x","@typescript-eslint/eslint-plugin":"4.x","@typescript-eslint/parser":"4.x","eslint":"7.x","graphql":"15.x","tslib":"2.x","typescript":"4.x"},"gitHead":"c9ffc9b044a2a6917f136c5cb5beb390b4a8444a","_id":"@cran/gql.link.merge@0.0.1","_nodeVersion":"15.5.0","_npmVersion":"6.14.9","dist":{"integrity":"sha512-Jr8szLzqW4OftRr7eTwe6JWLnrqkvVgbnA0IIvuzWH45gD3z0JbfcR9TR4euTCkkqugWcv5v/fN+ViKi13Papg==","shasum":"80f32dd8f0d28c2cbef07d3f09e74fd32a50891c","tarball":"https://registry.npmjs.org/@cran/gql.link.merge/-/gql.link.merge-0.0.1.tgz","fileCount":16,"unpackedSize":18520,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf9nFfCRA9TVsSAnZWagAAotEP/3rF/i7xI10OORQEG+mj\nqlS7SlQkDu+l7yr7L/uZoPd2z9GCP8QAFqy30D/BLBYLJKTnAGnIWcw7OEHA\nBN0AXFzrW/dpkMTgIXrHZLwzErt/M2z9Jr9e0lATcaJVsCMN1ntSCc7IPqi6\nYSJhscj2IO0egdiyjKYD6OU+IKyY3bR3htw7qkefxLKRgDcLaG0zjWauMzm+\nonH6kNVtCXE7c3enYGRq5HwWRDzbkVmeVZvUiiSNoHNnBCmDf3oVkeonN9Sr\nquLxgsm/xFIEPmhwWVCelOeiDi6qemNIztaY6X8423mQF4YqsdtzISKCnHPS\nKJ2CUDdAyUvGhmm/jpkOiC6+jNdC8Mn2SYmBEdLi3YZEiaaOC8NgWBAqW6mz\n4k77Tey2e3l8sR86HI7mg1DxsXlwiN018IhVdFHQZERjKe5eFlAfKw0FpZDT\n/cESrzoNKTs3V/9z6GhQwJ0OHNRyW3glul9FlqV9VnKUZKKqZsOrkcivkPuu\nYgnAdeMTfonfY/nDXmoYlYfdjU5rMThCNHVrafjpUAfDL54a157mPUXne38J\nCO4LioYTz6FhAUXmfjMJPrYhEuAnJBSroSqbnVWEFNluS5xN28dGJPDQ+d4r\n1s6apGwA00WD44gVvQq6aovCSrGXHLgFpfg7OCfJo+mGoiWslaUpNCYiB7Ci\nZCFg\r\n=s9La\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDvQOExIyZbtTCdc7QX7uvHmYrUleahJIXHXD4MpT9BbAiEA+G/hU+NyAcA33XB6gkUrxc7SapHPj6kuoroz3WzXjqs="}]},"_npmUser":{"name":"common-cranberry","email":"chris@cranberry.ink"},"directories":{},"maintainers":[{"name":"common-cranberry","email":"chris@cranberry.ink"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/gql.link.merge_0.0.1_1609986399350_0.760730022737349"},"_hasShrinkwrap":false}},"time":{"created":"2021-01-07T02:26:39.139Z","0.0.1":"2021-01-07T02:26:39.476Z","modified":"2022-04-05T01:36:49.417Z"},"maintainers":[{"name":"common-cranberry","email":"chris@cranberry.ink"}],"description":"Apollo Link to Merge GQL Queries","keywords":["cranberry"],"repository":{"type":"git","url":"git@gitlab.com:c6s/lib/gql/merge-link.git"},"author":{"name":"Cranberry"},"license":"CC-BY-ND-4.0","readme":"\n# Apollo Merge Link\n\n- Merge all Queries\n- Forward all Mutations and Subscriptions\n\n## Caveats\n\nThe use of `no-cache` fetch policy will\ncause this link to fail. The cache is used\nto re-evaluate a merged query to avoid\nhaving to back-parse the merge. This\nimproves efficiency and makes the code base\nsmaller, but at the cost of requiring a cache.\n\nIf you wish to use the `no-cache` policy, or\nfor any other reason do not want to merge,\nprovide in the context `merge: false`,\nwhich will disable this link.\n\nThis is useful for authentication or other\nsensitive / ephemeral data which should not be\ncached. This does however imply that these\nqueries cannot be merged.\n\nFor more information about fetch policies\nview [this article](https://medium.com/@galen.corey/understanding-apollo-fetch-policies-705b5ad71980).\n\n## Initialization\n\n```ts\nimport { MergeLink } from \"@cran/gql.merge-link\";\n\nexport const client = new ApolloLink({\n  cache: new InMemoryCache(),\n  link: from([\n    // apollo-link-context\n    new MergeLink({ client ( ) { return client; } }),\n    // apollo-link-retry\n    // apollo-link-queue\n    new BatchHttpLink({ uri: \"0.0.0.0\" }),\n  ]),\n});\n```\n\n## Usage\n\n> Async IIFE is for example only.\n\nIf you are manually calling the client query\nstatement, wrap any independent queries\nin a promise all statement, otherwise the\nmerge link will not be effective.\n\n```ts\n// DO THIS\n(async function ( ) {\n  const [\n    { data: { a, }, },\n    { data: { b, }, },\n  ] = Promise.all([\n    client.query(gql`{a}`),\n    client.query(gql`{b}`),\n  ]);\n\n  return [ a, b, ];\n})();\n\n// DO NOT DO THIS\n(async function ( ) {\n  const { data: { a, }, } = await client.query({\n    query: gql`{a}`,\n  });\n  const { data: { b, }, } = await client.query({\n    query: gql`{b}`,\n  });\n\n  return [ a, b, ];\n})();\n\n// UNLESS DEPENDENT\n(async function ( ) {\n  const { data: { a, }, } = await client.query({\n    query: gql`{a}`,\n  });\n  const { data: { b, }, } = await client.query({\n    query: gql`query($a:String){ b(a:$a) }`,\n    variables: { a, },\n  });\n\n  return [ a, b, ];\n})();\n```\n\n## Merge Rules\n\n- A query must exist at the same path to be merged\n- A query must have the same arguments to be merged\n- Variables are not substituted for inline values\n- Variables are not created for inline values\n- Variables with the same value are merged\n- Variable values are compared with pointer equivalence\n- Aliases are merged by path, but return successfully\n\n### Queries\n\n```gql\nquery { a }\n```\n\n```gql\nquery { b { a } }\n```\n\n```gql\nquery { b { c } }\n```\n\n```gql\nquery { b(a:1) { c } }\n```\n\n```gql\nquery($a:String!) { b(a:$a) { c } }\n# { a: 1 }\n```\n\n```gql\nquery($b:String!) { alias:b(a:$a) { d } }\n# { a: 1 }\n```\n\n```gql\nquery($b:String!) { alias:b(a:$a) { c } }\n# { a: 2 }\n```\n\n```gql\nquery { auth { token } }\n# context: { merge: false }\n# fetchPolicy: no-cache\n```\n\n```gql\nmutation {\n  update() { id }\n}\n```\n\n### Merge Result\n\n```gql\nquery {\n  auth { token }\n}\nmutation {\n  update() { id }\n}\nquery($a:String!,$b:String!) {\n  a\n  b { a c }\n  b(a:1) { c }\n  b(a:$a) { c d }\n  b(a:$b) { c }\n}\n# { a: 1, b: 2 }\n```\n\nWhile it may cause some confusion\nhere, it should be noted that variables\nare created with a26 incrementing ids.\ni.e. If you have 26 active variables, the\nnext will be labeled `aa`. These variable\nnames are reset once a merged query is sent.\n\n### Order\n\nIf you noticed in the above example that\nthe merged query came last, this is because\nthe merger is debounced by 10ms (which is\nthe same default length as the batch link).\nThis allows time to collect queries for merging\nas well as ensuring mutations requested during\nthe merge process are executed before receiving\nthe query data.\n\n#### Why 10ms?\n\nA common cycle in the JS queue-stack is 4-5ms\nmeaning any timeout is delayed by at least this\namount. Allowing for twice this ensures that\nat least 1 cycle will have passed before\nprocessing the merge.\n","readmeFilename":"readme.md"}