Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
apollo-link-persisted-queries
Advanced tools
Unlike REST APIs that use a fixed URL to load data, GraphQL provides a rich query language that can be used to express the shape of application data requirements. This is a marvellous advancement in technology, but it comes at a cost: GraphQL query strings are often much longer than REST URLS — in some cases by many kilobytes.
In practice we've seen GraphQL query sizes ranging well above 10 KB just for the query text. This is actually significant overhead when compared with a simple URL of 50-100 characters. When paired with the fact that the uplink speed from the client is typically the most bandwidth-constrained part of the chain, large queries can become bottlenecks for client performance.
Automatic Persisted Queries solves this problem by sending a generated id instead of the query text as the request.
For more information about this solution, read [this article announcing Automatic Persisted Queries].
This library is a client implementation for use with Apollo Client by using custom Apollo Link.
npm install apollo-link-persisted-queries --save
The persisted query link requires using the http-link
. The easiest way to use them together to to concat them into a single link.
import { createPersistedQueryLink } from "apollo-link-persisted-queries";
import { createHttpLink } from "apollo-link-http";
// use this with Apollo Client
const link = createPersistedQueryLink().concat(createHttpLink({ uri: "/graphql" }));
Thats it! Now your client will start sending query signatures instead of the full text resulting in improved network performance!
The createPersistedQueryLink function takes an optional object with configuration. Currently the only supported configutation is a key called generateHash
which recieves the query and returns the hash.
generateHash
: a function that takes the query document and returns the hashApollo Engine supports recieving and fufulling Automatic Persisted Queries. Simply adding this link into your client app will improve your network response times when using Apollo Engine.
Automatic Persisted Queries are made up of three parts: the query signature, error responses, and the negotiaion protocal.
Query Signature The query signature for Automatic Persisted Queries is sent along the extensions field of a request from the client. This is a transport independent way to send extra information along with the operation.
{
operationName: 'MyQuery',
variables: null,
extensions: {
persistedQuery: {
version: 1,
sha256Hash: hashOfQuery
}
}
}
When sending an Automatic Persisted Query, the client ommits the query
field normally present, and instead sends an extension field with a persistedQuery
object as shown above. The hash is a sha256
hash of the query string.
If the client needs to register the hash, the query signature will be the same but include the full query text like so:
{
operationName: 'MyQuery',
variables: null,
query: `query MyQuery { id }`,
extensions: {
persistedQuery: {
version: 1,
sha256Hash: hashOfQuery
}
}
}
This should only happen once across all clients when a new query is introduced into your application.
Error Responses When the initial query signature is received by a backend, if it is unable to find the hash previously stored, it must send back the following response signature:
{
errors: [
{ message: 'PersistedQueryNotFound' }
]
}
If the backend doesn't support Automatic Persisted Queries, or does not want to support it for that particular client, it can send back the following which will tell the client to stop trying to send hashes all together:
{
errors: [
{ message: 'PersistedQueryNotSupported' }
]
}
Negotiation Protocal In order to support Automatic Persisted Queries, the client and server must follow the negotiaion steps as outlined here:
Happy Path
query
fieldMissing hash path
query
fieldIf you want to avoid hashing in the browser, you can use a build script to include the hash as part of the request. Then you pass a function to retrieve that hash when the operation is run. This works well with projects like this which uses webpack to generate the hashes at build time.
If you use the above loader, you can pass { generateHash: ({ documentId }) => documentId }
to the createPersistedQueryLink
call.
FAQs
Use persisted queries with Apollo Link
The npm package apollo-link-persisted-queries receives a total of 27,219 weekly downloads. As such, apollo-link-persisted-queries popularity was classified as popular.
We found that apollo-link-persisted-queries demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.