GraphQL persisted query: ID भेजें और operation safelist लागू करें
GraphQL persisted query किसी operation document को server storage में मौजूद ID से जोड़ती है, ताकि हर request में पूरा query text भेजने के बजाय ID भेजी जा सके। Automatic persisted queries (APQ) मुख्यतः request size घटाने की cache तकनीक हैं। Trusted documents या safelist ऐसी नीतियाँ हैं जो execution को पहले से स्वीकृत operations तक सीमित करती हैं। केवल hash भेजने से API allowlist तक सीमित नहीं होती।
GraphQL persisted query किसी operation document को server storage में मौजूद ID से जोड़ती है, ताकि हर request में पूरा query text भेजने के बजाय ID भेजी जा सके। Automatic persisted queries (APQ) मुख्यतः request size घटाने की cache तकनीक हैं। Trusted documents या safelist ऐसी नीतियाँ हैं जो execution को पहले से स्वीकृत operations तक सीमित करती हैं। केवल hash भेजने से API allowlist तक सीमित नहीं होती।
यह लेख HTTP आधारित GraphQL requests में APQ और पहले से register किए गए operations समझाता है। Registration और protocol विवरण client व server के अनुसार अलग हैं; Apollo उदाहरण केवल उन्हीं उत्पादों पर लागू हैं।
Persisted query कैसे काम करती है
Server पूरा GraphQL document एक स्थिर ID के साथ store करता है। बाद में client ID और variables भेजता है; server stored document खोजता, validate करता और execute करता है। Apollo APQ query string के SHA-256 hash को ID बनाता है। बड़े document के लिए ID भेजने से body या URL का text और network traffic घट सकता है। इससे response size या resolver/database का काम अपने-आप कम नहीं होता।
सामान्य Apollo APQ प्रवाह:
Client केवल hash और variables भेजता है।
Server cache में hash न मिले तो `PERSISTED_QUERY_NOT_FOUND` लौटाता है।
Client उसी hash और पूरे query document के साथ दोबारा request करता है।
Server document लेकर hash मिलाता, store करता और execute करता है।
बाद की requests में केवल hash और variables भेजे जा सकते हैं।
इसलिए APQ में पूरा document कम-से-कम एक बार server तक पहुँचना चाहिए। Cache expiry या eviction से पहले जाना hash फिर अज्ञात हो सकता है; client fallback और cache consistency जाँचें।
APQ, persisted operations और trusted documents
अलग implementations में शब्दावली बदलती है। “Persisted query” APQ या security allowlist, दोनों के लिए प्रयुक्त हो सकता है; वास्तविक व्यवहार देखें।
| तरीका | Registration कब | अज्ञात ID/document | उद्देश्य |
|---|---|---|---|
| APQ cache | Client पहली बार पूरा document भेजे तो गतिशील रूप से | न मिलने पर client पूरा text दोबारा भेज सकता है | Request size और पुनःप्रेषण घटाना |
| पहले से registered persisted operation | Build/deploy में manifest या सूची register होती है | सूची से बाहर operations को अस्वीकार करने के लिए सेट किया जा सकता है | स्वीकृत client operations सीमित करना और ID requests |
| Trusted document / safelist | समीक्षा और मंज़ूरी वाले documents server सूची में जोड़े जाते हैं | नीति के अनुसार सूची के बाहर अस्वीकार हो सकता है | भरोसेमंद apps के executable documents सीमित करना |
सुरक्षा प्रभाव इस पर निर्भर है कि server मनमाने documents को सचमुच रोकता है या नहीं। उदाहरण के लिए Apollo GraphOS Router operation ID अनुमति, audit और safelisting के अलग स्तर देता है। ऐसा mode जहाँ ID भी चले और मनमाने documents भी स्वीकार हों, enforced safelist नहीं है। Registration और enforcement का अंतर उत्पाद और config में जाँचें।
यह न मानें कि पहले registration सार्वजनिक API के लिए सही है जहाँ third-party developers मनमाने operations भेजते हैं। GraphQL security guide trusted documents को first-party apps के लिए उपयुक्त बताती है जिनके operations टीम ने लिखे और review किए हों; सार्वजनिक API के सभी third-party client operations पहले से ज्ञात नहीं हो सकते। Safelist के साथ भी authentication, authorization, page size, depth/complexity और rate limits चाहिए।
लागू करने के चरण
Request छोटा करना हो तो पहले APQ जाँचें। Client और server के APQ version तथा hash नियम समान हों। तय करें कि cache miss पर client पूरा document फिर भेज सकता है या नहीं। सार्वजनिक या मिश्रित API में यह fallback मनमाने documents को server तक पहुँचा सकता है।
Executable operations सीमित करने हों तो registration और enforcement साथ तैयार करें। Build में उपयोग हुए operations और fragments से manifest बनाएँ और CI/CD या deployment से server allowlist में publish करें। सूची से बाहर query strings अस्वीकार करने की setting चालू करें और client IDs/headers को server policy से मिलाएँ। Hash document की पहचान है, authorization या secret key नहीं।
Release और rollback की योजना बनाएँ। Server के नया manifest स्वीकारने से पहले client deploy हुआ तो नई IDs विफल हो सकती हैं। सामान्य क्रम: पहले registrations publish करें, फिर clients deploy करें, और पुराने clients हट जाने के बाद अप्रयुक्त operations साफ़ करें। उत्पाद की वास्तविक deployment sequence और compatibility नियम जाँचें।
Hash mismatch जाँचें। Text normalization, fragment composition या tool options बदलने से एक ही intended operation के hashes अलग हो सकते हैं। Manifest generator और runtime client में समान source operations और ID settings रखें। Unknown IDs और mismatch की निगरानी करें। Apollo के अनुसार PQL IDs unique और document आधारित होने चाहिए, operation name आधारित नहीं।
क्या सत्यापित करें
Unknown hash का response और पूरा document भेजने का client fallback
Server अनधिकृत query strings को वास्तव में अस्वीकार करता है या नहीं
Server, client और manifest समान hashes बनाते हैं या नहीं
Variables operation document से अलग भेजे जाते रहें
Registration, client rollout और पुराने entries हटाने का क्रम
Cache eviction, कई instances के बीच sharing, list versions और recovery
Operation सीमाओं से अलग authentication, object/field permissions, cost और rate limits
Persisted queries बड़े operation text का transmission घटा सकती हैं; लागू की गई approved list executable documents सीमित कर सकती है। Performance लक्ष्य और trust model के आधार पर चुनें, और यदि केवल registered IDs स्वीकार होने चाहिए तो server config तथा failure responses से इसकी पुष्टि करें।
स्रोत
[GraphQL आधिकारिक Performance मार्गदर्शिका: persisted queries](https://graphql.org/learn/performance/)
[GraphQL आधिकारिक Security मार्गदर्शिका: trusted documents](https://graphql.org/learn/security/)
[Apollo Server APQ दस्तावेज़](https://www.apollographql.com/docs/apollo-server/performance/apq)
[Apollo Client persisted queries मार्गदर्शिका](https://www.apollographql.com/docs/react/data/persisted-queries)
[Apollo GraphOS persisted-query safelisting](https://www.apollographql.com/docs/graphos/platform/security/persisted-queries)
[GraphQL over HTTP घोषणा: persisted documents standardization की स्थिति](https://graphql.org/blog/2026-11-12-announcing-the-graphql-over-http-spec/)