GraphQL Federation: एक API के पीछे subgraph जोड़ना
GraphQL Federation कई सेवाओं के स्वामित्व वाले GraphQL schema को जोड़कर client को एक सुसंगत API देता है। हर सेवा एक subgraph देती है; composition उन schemas को supergraph schema में जोड़ता है। Router metadata के आधार पर अनुरोध जरूरी subgraph तक भेजता और परिणाम मिलाता है। केवल सेवाओं का विभाजित होना Federation अपनाने का कारण नहीं है: टीमों की स्वतंत्र release के लाभ की तुलना composition, routing और schema संचालन के खर्च से करें।
GraphQL Federation कई सेवाओं के स्वामित्व वाले GraphQL schema को जोड़कर client को एक सुसंगत API देता है। हर सेवा एक subgraph देती है; composition उन schemas को supergraph schema में जोड़ता है। Router metadata के आधार पर अनुरोध जरूरी subgraph तक भेजता और परिणाम मिलाता है। केवल सेवाओं का विभाजित होना Federation अपनाने का कारण नहीं है: टीमों की स्वतंत्र release के लाभ की तुलना composition, routing और schema संचालन के खर्च से करें।
यह लेख Apollo Federation की शब्दावली और उदाहरण, खासकर Federation 2 syntax, का उपयोग करता है। यह नहीं मानता कि अन्य implementation या भविष्य का मानक समान व्यवहार करेगा।
घटक और अनुरोध प्रवाह
**Subgraph:** किसी domain के लिए GraphQL schema और resolver देने वाली सेवा। Product सेवा product fields और queries की मालिक हो सकती है; review सेवा reviews और products से उनके संबंध की।
**Composition:** subgraph schemas की जाँच और संयोजन कर supergraph schema बनाता है। यह केवल type definition जोड़ना नहीं है: field ownership और resolvability, type compatibility की जाँच तथा routing metadata तैयार होता है।
**Router:** client का एकल प्रवेश-बिंदु। यह supergraph metadata से अनुरोध की योजना बनाता, आवश्यक subgraph बुलाता और उत्तर जोड़ता है।
**Supergraph और API schema:** supergraph में Federation तथा routing की जानकारी होती है। Client को दिखने वाला API schema अंदरूनी implementation विवरण छिपा सकता है।
Client router को एक GraphQL operation भेजता है। उदाहरण के लिए, product और उसकी reviews के लेखकों के नाम माँगे जा सकते हैं। Router product-owning subgraph से product लेकर entity reference और चुने हुए fields के सहारे लेखक जानकारी के लिए दूसरा subgraph बुला सकता है। वास्तविक network calls और performance query plan, server, data और deployment पर निर्भर हैं। एक GraphQL request का मतलब एक backend call नहीं है।
Entity सेवाओं के बीच types जोड़ती है
जब subgraph एक ही अवधारणा के अलग fields देते हैं, तो object जोड़ने के लिए साझा पहचान नियम चाहिए। Apollo Federation 2 में @key fields के समूह से entity की पहचान करता है, जिसे अन्य subgraph reference कर सकते हैं। उदाहरण के लिए, products subgraph Product के नाम और मूल्य रखता हो और reviews subgraph समीक्षाएँ दे, तो साझा key Product entity को जोड़ सकती है।
type Product @key(fields: "id") {
id: ID!
name: String!
}Reviews subgraph संगत Federation syntax से entity का संदर्भ देता और reviews जैसे अपने हल किए जाने वाले fields जोड़ता है। Entity reference resolver और directives चुनी गई subgraph library और Federation version के अनुरूप होने चाहिए। @shareable, @external, @requires, @provides field ownership और अन्य subgraph के values पर निर्भरता बताते हैं; लापरवाही से अस्पष्ट स्वामित्व या composition errors हो सकते हैं।
Composition संयुक्त schema contract जाँचता है
अलग-अलग मान्य subgraph schemas composition के समय टकरा सकते हैं। जैसे, कई subgraph एक object field परिभाषित करें तो Federation rules संगत resolvability और types की माँग करते हैं। Input field या argument का अंतर supergraph में व्यक्त contract भी बदल सकता है। Composition failure deployment से पहले गलत संयुक्त API contract दिखा सकता है।
Apollo दस्तावेज़ subgraph schema publish करने पर GraphOS के automatic composition और Rover CLI से manual composition का वर्णन करते हैं। ये उत्पाद-विशिष्ट विकल्प हैं, हर Federation implementation की सार्वभौमिक संचालन आवश्यकता नहीं। कोई भी tool हो, बदले subgraph को deploy करने से पहले composition checks स्वचालित करें और failure सुधारने वाली टीम तय रखें।
Federation कब उपयोगी है और इसकी लागत
जब कई domain team अपनी सेवाओं की मालिक हों और स्वतंत्र release करें, लेकिन client को एक संयुक्त graph चाहिए, Federation उपयोगी हो सकता है। संगत Federation rules का पालन करने पर subgraph अलग-अलग technology उपयोग कर सकते हैं। Domain team अपने fields और resolvers संभालती हैं; केंद्रीय router client entry और request orchestration संभालता है।
इसके बदले composition और schema changes, subgraph के बीच entity key और data contract, router availability/capacity/security, network latency, distributed tracing, partial failure और टीमों की जिम्मेदारी संभालनी पड़ती है। कई सेवाओं के fields जोड़ने से backend calls और failure paths बढ़ सकते हैं। Federation backend query को अपने-आप optimize नहीं करता और data platform की जगह नहीं लेता।
यदि सेवा या टीम की सीमाएँ कम हैं और एक GraphQL server पर्याप्त है, तो monolithic schema सरल हो सकता है। GraphQL का आधिकारिक मार्गदर्शन कहता है कि Federation के लिए पर्याप्त infrastructure और केंद्रीय समर्थन चाहिए; आवश्यकता स्पष्ट होने तक monolith से शुरू किया जा सकता है।
चरणबद्ध अपनाना
**संगठन की समस्या स्पष्ट करें।** स्वतंत्र release, domain ownership या एक client API में मौजूदा बाधा दर्ज करें। केवल अधिक सेवाएँ होना Federation की आवश्यकता साबित नहीं करता।
**सीमाएँ तय करें।** Database या टीम के नाम से यांत्रिक विभाजन के बजाय domain ownership और परिवर्तन-जिम्मेदारी से subgraph बाँटें।
**एक छोटा flow मॉडल करें।** दो subgraph से गुजरने वाला प्रतिनिधि operation चुनें। जाँचें कि `@key` स्थिर है और दोनों सेवाएँ इसे एक ही तरह resolve कर सकती हैं।
**पहले composition checks और बदलाव के नियम जोड़ें।** Type compatibility, duplicate fields, entity references और API schema exposure जाँचें; failure पर deployment रोकें।
**Request और संचालन मापें।** प्रतिनिधि operation का query plan, subgraph calls, latency, errors और resources देखें। माप के बिना एक client request को तेज न मानें।
**Recovery और सुरक्षा डिज़ाइन करें।** Router authentication, subgraph के बीच trust, call limits, timeout, partial failure, observability और rollback की जिम्मेदारी तय करें।
तैयारी सूची
क्या सेवा/टीम सीमाएँ और field owners स्पष्ट हैं?
क्या client को एक API देने का वास्तविक उपयोग है?
क्या entity keys स्थिर और subgraph में समान रूप से समझे जाते हैं?
क्या deployment से पहले composition स्वचालित जाँचा और revert किया जा सकता है?
क्या router और distributed latency, errors तथा access संभालने वाली टीम है?
क्या सरल monolithic schema वही संगठनात्मक समस्या हल करेगा?
स्रोत
[GraphQL की आधिकारिक Federation मार्गदर्शिका](https://graphql.org/learn/federation/)
[Apollo Federation का परिचय](https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/federation)
[Apollo subgraph, supergraph और API schema मार्गदर्शिका](https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/schema-types)
[Apollo schema composition दस्तावेज़](https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/composition)
[Apollo Federation composition नियम](https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/reference/composition-rules)
[Apollo Federation directive संदर्भ](https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/reference/directives)