A place to cache linked articles (think custom and personal wayback machine)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 4 Jahren
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118
  1. title: GraphQL
  2. url: http://facebook.github.io/graphql/
  3. hash_url: acd301ad06de4bf928f0515efa510a91
  4. <section id="intro">
  5. <p><em>Working Draft &ndash; July 2015</em></p>
  6. <p><strong>Introduction</strong></p>
  7. <p>This is a Draft RFC Specification for GraphQL, a query language created by Facebook in 2012 for describing the capabilities and requirements of data models for client&#8208;server applications. The development of this standard started in 2015. GraphQL
  8. is a new and evolving language and is not complete. Significant enhancement will continue in future editions of this specification.</p>
  9. <p><strong>Copyright notice</strong></p>
  10. <p>Copyright (c) 2015, Facebook, Inc. All rights reserved.</p>
  11. <p>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>
  12. <ul>
  13. <li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
  14. <li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
  15. <li>Neither the name Facebook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.</li>
  16. </ul>
  17. <p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &ldquo;AS IS&rdquo; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  20. POSSIBILITY OF SUCH DAMAGE.</p>
  21. </section>
  22. <div class="spec-toc">
  23. <ol>
  24. <li><a href="#sec-Overview"><span class="spec-secnum">1</span>Overview</a></li>
  25. <li><a href="#sec-Language"><span class="spec-secnum">2</span>Language</a>
  26. <ol>
  27. <li><a href="#sec-Names"><span class="spec-secnum">2.1</span>Names</a></li>
  28. <li><a href="#sec-Language.Document"><span class="spec-secnum">2.2</span>Document</a></li>
  29. <li><a href="#sec-Language.Operations"><span class="spec-secnum">2.3</span>Operations</a></li>
  30. <li><a href="#sec-Language.Fields"><span class="spec-secnum">2.4</span>Fields</a></li>
  31. <li><a href="#sec-Field-Selections"><span class="spec-secnum">2.5</span>Field Selections</a></li>
  32. <li><a href="#sec-Language.Arguments"><span class="spec-secnum">2.6</span>Arguments</a></li>
  33. <li><a href="#sec-Field-Alias"><span class="spec-secnum">2.7</span>Field Alias</a></li>
  34. <li><a href="#sec-Input-Values"><span class="spec-secnum">2.8</span>Input Values</a></li>
  35. <li><a href="#sec-Language.Variables"><span class="spec-secnum">2.9</span>Variables</a></li>
  36. <li><a href="#sec-Language.Fragments"><span class="spec-secnum">2.10</span>Fragments</a>
  37. <ol>
  38. <li><a href="#sec-Types-on-fragments"><span class="spec-secnum">2.10.1</span>Types on fragments</a></li>
  39. <li><a href="#sec-Query-variables-in-fragments"><span class="spec-secnum">2.10.2</span>Query variables in fragments</a></li>
  40. <li><a href="#sec-Inline-fragments"><span class="spec-secnum">2.10.3</span>Inline fragments</a></li>
  41. </ol>
  42. </li>
  43. <li><a href="#sec-Language.Directives"><span class="spec-secnum">2.11</span>Directives</a>
  44. <ol>
  45. <li><a href="#sec-Fragment-Directives"><span class="spec-secnum">2.11.1</span>Fragment Directives</a></li>
  46. </ol>
  47. </li>
  48. </ol>
  49. </li>
  50. <li><a href="#sec-Type-System"><span class="spec-secnum">3</span>Type System</a>
  51. <ol>
  52. <li><a href="#sec-Type-System.Types"><span class="spec-secnum">3.1</span>Types</a>
  53. <ol>
  54. <li><a href="#sec-Scalars"><span class="spec-secnum">3.1.1</span>Scalars</a>
  55. <ol>
  56. <li><a href="#sec-Built-in-Scalars"><span class="spec-secnum">3.1.1.1</span>Built-in Scalars</a>
  57. <ol>
  58. <li><a href="#sec-Int"><span class="spec-secnum">3.1.1.1.1</span>Int</a></li>
  59. <li><a href="#sec-Float"><span class="spec-secnum">3.1.1.1.2</span>Float</a></li>
  60. <li><a href="#sec-String"><span class="spec-secnum">3.1.1.1.3</span>String</a></li>
  61. <li><a href="#sec-Boolean"><span class="spec-secnum">3.1.1.1.4</span>Boolean</a></li>
  62. <li><a href="#sec-ID"><span class="spec-secnum">3.1.1.1.5</span>ID</a></li>
  63. </ol>
  64. </li>
  65. </ol>
  66. </li>
  67. <li><a href="#sec-Objects"><span class="spec-secnum">3.1.2</span>Objects</a>
  68. <ol>
  69. <li><a href="#sec-Object-Field-Arguments"><span class="spec-secnum">3.1.2.1</span>Object Field Arguments</a></li>
  70. <li><a href="#sec-Object-Field-deprecation"><span class="spec-secnum">3.1.2.2</span>Object Field deprecation</a></li>
  71. <li><a href="#sec-Object-type-validation"><span class="spec-secnum">3.1.2.3</span>Object type validation</a></li>
  72. </ol>
  73. </li>
  74. <li><a href="#sec-Interfaces"><span class="spec-secnum">3.1.3</span>Interfaces</a>
  75. <ol>
  76. <li><a href="#sec-Interface-type-validation"><span class="spec-secnum">3.1.3.1</span>Interface type validation</a></li>
  77. </ol>
  78. </li>
  79. <li><a href="#sec-Unions"><span class="spec-secnum">3.1.4</span>Unions</a>
  80. <ol>
  81. <li><a href="#sec-Union-type-validation"><span class="spec-secnum">3.1.4.1</span>Union type validation</a></li>
  82. </ol>
  83. </li>
  84. <li><a href="#sec-Enums"><span class="spec-secnum">3.1.5</span>Enums</a></li>
  85. <li><a href="#sec-Input-Objects"><span class="spec-secnum">3.1.6</span>Input Objects</a></li>
  86. <li><a href="#sec-Lists"><span class="spec-secnum">3.1.7</span>Lists</a></li>
  87. <li><a href="#sec-Non-Null"><span class="spec-secnum">3.1.8</span>Non-Null</a></li>
  88. </ol>
  89. </li>
  90. <li><a href="#sec-Type-System.Directives"><span class="spec-secnum">3.2</span>Directives</a>
  91. <ol>
  92. <li><a href="#sec--skip"><span class="spec-secnum">3.2.1</span>@skip</a></li>
  93. <li><a href="#sec--include"><span class="spec-secnum">3.2.2</span>@include</a></li>
  94. </ol>
  95. </li>
  96. <li><a href="#sec-Starting-types"><span class="spec-secnum">3.3</span>Starting types</a></li>
  97. </ol>
  98. </li>
  99. <li><a href="#sec-Introspection"><span class="spec-secnum">4</span>Introspection</a>
  100. <ol>
  101. <li><a href="#sec-General-Principles"><span class="spec-secnum">4.1</span>General Principles</a>
  102. <ol>
  103. <li><a href="#sec-Naming-conventions"><span class="spec-secnum">4.1.1</span>Naming conventions</a></li>
  104. <li><a href="#sec-Documentation"><span class="spec-secnum">4.1.2</span>Documentation</a></li>
  105. <li><a href="#sec-Deprecation"><span class="spec-secnum">4.1.3</span>Deprecation</a></li>
  106. <li><a href="#sec-Type-Name-Introspection"><span class="spec-secnum">4.1.4</span>Type Name Introspection</a></li>
  107. </ol>
  108. </li>
  109. <li><a href="#sec-Schema-Introspection"><span class="spec-secnum">4.2</span>Schema Introspection</a>
  110. <ol>
  111. <li><a href="#sec-The-__Type-Type"><span class="spec-secnum">4.2.1</span>The &quot;__Type&quot; Type</a></li>
  112. <li><a href="#sec-Type-Kinds"><span class="spec-secnum">4.2.2</span>Type Kinds</a>
  113. <ol>
  114. <li><a href="#sec-Scalar"><span class="spec-secnum">4.2.2.1</span>Scalar</a></li>
  115. <li><a href="#sec-Object"><span class="spec-secnum">4.2.2.2</span>Object</a></li>
  116. <li><a href="#sec-Union"><span class="spec-secnum">4.2.2.3</span>Union</a></li>
  117. <li><a href="#sec-Interface"><span class="spec-secnum">4.2.2.4</span>Interface</a></li>
  118. <li><a href="#sec-Enum"><span class="spec-secnum">4.2.2.5</span>Enum</a></li>
  119. <li><a href="#sec-Input-Object"><span class="spec-secnum">4.2.2.6</span>Input Object</a></li>
  120. <li><a href="#sec-List"><span class="spec-secnum">4.2.2.7</span>List</a></li>
  121. <li><a href="#sec-Non-null"><span class="spec-secnum">4.2.2.8</span>Non-null</a></li>
  122. <li><a href="#sec-Combining-List-and-Non-Null"><span class="spec-secnum">4.2.2.9</span>Combining List and Non-Null</a></li>
  123. </ol>
  124. </li>
  125. </ol>
  126. </li>
  127. </ol>
  128. </li>
  129. <li><a href="#sec-Validation"><span class="spec-secnum">5</span>Validation</a>
  130. <ol>
  131. <li><a href="#sec-Validation.Fields"><span class="spec-secnum">5.1</span>Fields</a>
  132. <ol>
  133. <li><a href="#sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types"><span class="spec-secnum">5.1.1</span>Field Selections on Objects, Interfaces, and Unions Types</a></li>
  134. <li><a href="#sec-Field-Selection-Merging"><span class="spec-secnum">5.1.2</span>Field Selection Merging</a></li>
  135. <li><a href="#sec-Leaf-Field-Selections"><span class="spec-secnum">5.1.3</span>Leaf Field Selections</a></li>
  136. </ol>
  137. </li>
  138. <li><a href="#sec-Validation.Arguments"><span class="spec-secnum">5.2</span>Arguments</a>
  139. <ol>
  140. <li><a href="#sec-Argument-Names"><span class="spec-secnum">5.2.1</span>Argument Names</a></li>
  141. <li><a href="#sec-Argument-Values-Type-Correctness"><span class="spec-secnum">5.2.2</span>Argument Values Type Correctness</a>
  142. <ol>
  143. <li><a href="#sec-Compatible-Values"><span class="spec-secnum">5.2.2.1</span>Compatible Values</a></li>
  144. <li><a href="#sec-Required-Arguments"><span class="spec-secnum">5.2.2.2</span>Required Arguments</a></li>
  145. </ol>
  146. </li>
  147. </ol>
  148. </li>
  149. <li><a href="#sec-Validation.Fragments"><span class="spec-secnum">5.3</span>Fragments</a>
  150. <ol>
  151. <li><a href="#sec-Fragment-Declarations"><span class="spec-secnum">5.3.1</span>Fragment Declarations</a>
  152. <ol>
  153. <li><a href="#sec-Fragment-Spread-Type-Existence"><span class="spec-secnum">5.3.1.1</span>Fragment Spread Type Existence</a></li>
  154. <li><a href="#sec-Fragments-On-Composite-Types"><span class="spec-secnum">5.3.1.2</span>Fragments On Composite Types</a></li>
  155. <li><a href="#sec-Fragments-Must-Be-Used"><span class="spec-secnum">5.3.1.3</span>Fragments Must Be Used</a></li>
  156. </ol>
  157. </li>
  158. <li><a href="#sec-Fragment-Spreads"><span class="spec-secnum">5.3.2</span>Fragment Spreads</a>
  159. <ol>
  160. <li><a href="#sec-Fragment-spread-target-defined"><span class="spec-secnum">5.3.2.1</span>Fragment spread target defined</a></li>
  161. <li><a href="#sec-Fragment-spreads-must-not-form-cycles"><span class="spec-secnum">5.3.2.2</span>Fragment spreads must not form cycles</a></li>
  162. <li><a href="#sec-Fragment-spread-is-possible"><span class="spec-secnum">5.3.2.3</span>Fragment spread is possible</a>
  163. <ol>
  164. <li><a href="#sec-Object-Spreads-In-Object-Scope"><span class="spec-secnum">5.3.2.3.1</span>Object Spreads In Object Scope</a></li>
  165. <li><a href="#sec-Abstract-Spreads-in-Object-Scope"><span class="spec-secnum">5.3.2.3.2</span>Abstract Spreads in Object Scope</a></li>
  166. <li><a href="#sec-Object-Spreads-In-Abstract-Scope"><span class="spec-secnum">5.3.2.3.3</span>Object Spreads In Abstract Scope</a></li>
  167. <li><a href="#sec-Abstract-Spreads-in-Abstract-Scope"><span class="spec-secnum">5.3.2.3.4</span>Abstract Spreads in Abstract Scope</a></li>
  168. </ol>
  169. </li>
  170. </ol>
  171. </li>
  172. </ol>
  173. </li>
  174. <li><a href="#sec-Validation.Directives"><span class="spec-secnum">5.4</span>Directives</a>
  175. <ol>
  176. <li><a href="#sec-Directives-Are-Defined"><span class="spec-secnum">5.4.1</span>Directives Are Defined</a></li>
  177. </ol>
  178. </li>
  179. <li><a href="#sec-Validation.Operations"><span class="spec-secnum">5.5</span>Operations</a>
  180. <ol>
  181. <li><a href="#sec-Validation.Operations.Variables"><span class="spec-secnum">5.5.1</span>Variables</a>
  182. <ol>
  183. <li><a href="#sec-Variable-Default-Values-Are-Correctly-Typed"><span class="spec-secnum">5.5.1.1</span>Variable Default Values Are Correctly Typed</a></li>
  184. <li><a href="#sec-Variables-Are-Input-Types"><span class="spec-secnum">5.5.1.2</span>Variables Are Input Types</a></li>
  185. <li><a href="#sec-All-Variable-Uses-Defined"><span class="spec-secnum">5.5.1.3</span>All Variable Uses Defined</a></li>
  186. <li><a href="#sec-All-Variables-Used"><span class="spec-secnum">5.5.1.4</span>All Variables Used</a></li>
  187. <li><a href="#sec-All-Variable-Usages-are-Allowed"><span class="spec-secnum">5.5.1.5</span>All Variable Usages are Allowed</a></li>
  188. </ol>
  189. </li>
  190. </ol>
  191. </li>
  192. </ol>
  193. </li>
  194. <li><a href="#sec-Execution"><span class="spec-secnum">6</span>Execution</a>
  195. <ol>
  196. <li><a href="#sec-Evaluating-requests"><span class="spec-secnum">6.1</span>Evaluating requests</a></li>
  197. <li><a href="#sec-Evaluating-operations"><span class="spec-secnum">6.2</span>Evaluating operations</a></li>
  198. <li><a href="#sec-Evaluating-selection-sets"><span class="spec-secnum">6.3</span>Evaluating selection sets</a></li>
  199. <li><a href="#sec-Evaluating-a-grouped-field-set"><span class="spec-secnum">6.4</span>Evaluating a grouped field set</a>
  200. <ol>
  201. <li><a href="#sec-Field-entries"><span class="spec-secnum">6.4.1</span>Field entries</a></li>
  202. <li><a href="#sec-Normal-evaluation"><span class="spec-secnum">6.4.2</span>Normal evaluation</a></li>
  203. <li><a href="#sec-Serial-execution"><span class="spec-secnum">6.4.3</span>Serial execution</a></li>
  204. <li><a href="#sec-Error-handling"><span class="spec-secnum">6.4.4</span>Error handling</a></li>
  205. <li><a href="#sec-Nullability"><span class="spec-secnum">6.4.5</span>Nullability</a></li>
  206. </ol>
  207. </li>
  208. </ol>
  209. </li>
  210. <li><a href="#sec-Response"><span class="spec-secnum">7</span>Response</a>
  211. <ol>
  212. <li><a href="#sec-Serialization-Format"><span class="spec-secnum">7.1</span>Serialization Format</a>
  213. <ol>
  214. <li><a href="#sec-JSON-Serialization"><span class="spec-secnum">7.1.1</span>JSON Serialization</a></li>
  215. </ol>
  216. </li>
  217. <li><a href="#sec-Response-Format"><span class="spec-secnum">7.2</span>Response Format</a>
  218. <ol>
  219. <li><a href="#sec-Data"><span class="spec-secnum">7.2.1</span>Data</a></li>
  220. <li><a href="#sec-Errors"><span class="spec-secnum">7.2.2</span>Errors</a></li>
  221. </ol>
  222. </li>
  223. </ol>
  224. </li>
  225. <li><a href="#sec-Grammar"><span class="spec-secnum">8</span>Grammar</a>
  226. <ol>
  227. <li><a href="#sec-Ignored-Source"><span class="spec-secnum">8.1</span>Ignored Source</a></li>
  228. <li><a href="#sec-Tokens"><span class="spec-secnum">8.2</span>Tokens</a></li>
  229. <li><a href="#sec-Syntax"><span class="spec-secnum">8.3</span>Syntax</a>
  230. <ol>
  231. <li><a href="#sec-Syntax.Document"><span class="spec-secnum">8.3.1</span>Document</a></li>
  232. <li><a href="#sec-Syntax.Operations"><span class="spec-secnum">8.3.2</span>Operations</a></li>
  233. <li><a href="#sec-Syntax.Fragments"><span class="spec-secnum">8.3.3</span>Fragments</a></li>
  234. <li><a href="#sec-Values"><span class="spec-secnum">8.3.4</span>Values</a>
  235. <ol>
  236. <li><a href="#sec-Array-Value"><span class="spec-secnum">8.3.4.1</span>Array Value</a></li>
  237. <li><a href="#sec-Object-Value"><span class="spec-secnum">8.3.4.2</span>Object Value</a></li>
  238. </ol>
  239. </li>
  240. <li><a href="#sec-Syntax.Directives"><span class="spec-secnum">8.3.5</span>Directives</a></li>
  241. <li><a href="#sec-Syntax.Types"><span class="spec-secnum">8.3.6</span>Types</a></li>
  242. </ol>
  243. </li>
  244. </ol>
  245. </li>
  246. </ol>
  247. </div>
  248. </header>
  249. <section id="sec-Overview">
  250. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Overview">1</a></span>Overview</h2>
  251. <p>GraphQL is a query language designed to build client applications by providing an intuitive and flexible syntax and system for describing their data requirements and interactions.</p>
  252. <p>For example, this GraphQL request will receive the name of the user with id 4 from the Facebook implementation of GraphQL.</p><pre><code>{
  253. user(id: 4) {
  254. name
  255. }
  256. }
  257. </code></pre>
  258. <p>Which produces the resulting data (in JSON):</p><pre><code>{
  259. <span class="hljs-string">"user"</span>: {
  260. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark Zuckerberg"</span>
  261. }
  262. }
  263. </code></pre>
  264. <p>GraphQL is not a programming language capable of arbitrary computation, but is instead a language used to query application servers that have capabilities defined in this specification. GraphQL does not mandate a particular programming language or
  265. storage system for application servers that implement it. Instead, application servers take their capabilities and map them to a uniform language, type system, and philosophy that GraphQL encodes. This provides a unified interface friendly to product
  266. development and a powerful platform for tool&#8208;building.</p>
  267. <p>GraphQL has a number of design principles:</p>
  268. <ul>
  269. <li><strong>Hierarchical</strong>: Most product development today involves the creation and manipulation of view hierarchies. To achieve congruence with the structure of these applications, a GraphQL query itself is structured hierarchically. The query
  270. is shaped just like the data it returns. It is a natural way for clients to describe data requirements.</li>
  271. <li><strong>Product&#8208;centric</strong>: GraphQL is unapologetically driven by the requirements of views and the front&#8208;end engineers that write them. GraphQL starts with their way of thinking and requirements and build the language and runtime
  272. necessary to enable that.</li>
  273. <li><strong>Strong&#8208;typing</strong>: Every GraphQL server defines an application&#8208;specific type system. Queries are executed within the context of that type system. Given a query, tools can ensure that the query is both syntactically correct
  274. and valid within the GraphQL type system before execution, i.e. at development time, and the server can make certain guarantees about the shape and nature of the response.</li>
  275. <li><strong>Client&#8208;specified queries</strong>: Through its type system, a GraphQL server publishes the capabilities that its clients are allowed to consume. It is the client that is responsible for specifying exactly how it will consume those
  276. published capabilities. These queries are specified at field&#8208;level granularity. In the majority of client&#8208;server applications written without GraphQL, the server determines the data returned in its various scripted endpoints. A GraphQL
  277. query, on the other hand, returns exactly what a client asks for and no more.</li>
  278. <li><strong>Introspective</strong>: GraphQL is introspective. A GraphQL server&rsquo;s type system must be queryable by the GraphQL language itself, as will be described in this specification. GraphQL introspection serves as a powerful platform for
  279. building common tools and client software libraries.</li>
  280. </ul>
  281. <p>Because of these principles, GraphQL is a powerful and productive environment for building client applications. Product developers and designers building applications against working GraphQL servers -- supported with quality tools -- can quickly become
  282. productive without reading extensive documentation and with little or no formal training. To enable that experience, there must be those that build those servers and tools.</p>
  283. <p>The following formal specification serves as a reference for those builders. It describes the language and its grammar; the type system and the introspection system used to query it; and the execution and validation engines with the algorithms to
  284. power them. The goal of this specification is to provide a foundation and framework for an ecosystem of GraphQL tools, client libraries, and server implementations -- spanning both organizations and platforms -- that has yet to be built. We look
  285. forward to working with the community in order to do that. </p>
  286. </section>
  287. <section id="sec-Language">
  288. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Language">2</a></span>Language</h2>
  289. <p>Clients use the GraphQL language to make requests to a GraphQL server. We refer to these requests as documents. A document may contain operations (queries and mutations are both operations) and fragments, a common unit of composition allowing for
  290. query reuse.</p>
  291. <section id="sec-Names">
  292. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Names">2.1</a></span>Names</h3>
  293. <p>GraphQL documents are full of named things: operations, fields, arguments, directives, fragments, and variables. All names follow the same grammatical form:</p>
  294. <div class="spec-production" id="Name"><span class="spec-nt"><a href="#Name">Name</a></span>
  295. <div class="spec-rhs"><span class="spec-rx">/[_A-Za-z][_0-9A-Za-z]*/</span></div>
  296. </div>
  297. <p>Names in GraphQL are case&#8208;sensitive. That is to say <code>name</code>, <code>Name</code>, and <code>NAME</code> all refer to different names. Underscores are significant, which means <code>other_name</code> and <code>othername</code> are two
  298. different names.</p>
  299. <p>Names in GraphQL are limited to this <acronym>ASCII</acronym> subset of possible characters to support interoperation with as many other systems as possible.</p>
  300. </section>
  301. <section id="sec-Language.Document">
  302. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Document">2.2</a></span>Document</h3>
  303. <p>GraphQL documents are only executable by a server if they contain an operation. However documents which do not contain operations may still be parsed and validated to allow client to represent a single request across many documents.</p>
  304. <p>GraphQL documents may contain multiple operations, as long as they are named. When submitting a document with multiple operations to a GraphQL server, the name of the desired operation must also be provided.</p>
  305. <p>If a document contains only one query operation, that operation may be represented in the shorthand form, which omits the query keyword and query name.</p>
  306. </section>
  307. <section id="sec-Language.Operations">
  308. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Operations">2.3</a></span>Operations</h3>
  309. <p>There are two types of operations that GraphQL models:</p>
  310. <ul>
  311. <li>query &ndash; a read&#8208;only fetch.</li>
  312. <li>mutation &ndash; a write followed by a fetch.</li>
  313. </ul>
  314. <p>Each operation is represented by a custom name and a selection of fields.</p>
  315. <p><strong>Query shorthand</strong></p>
  316. <p>If a query has no variables or directives or name, the <code>query</code> keyword can be omitted. This means it must be the only query in the document.</p>
  317. <div class="spec-note">many examples below will use the query shorthand syntax.</div>
  318. </section>
  319. <section id="sec-Language.Fields">
  320. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Fields">2.4</a></span>Fields</h3>
  321. <p>A field in the top&#8208;level selection set often represents some kind of information that is globally accessible to your application and the current viewer. Some typical examples of global fields:</p><pre><code># `me` could represent the currently logged in user.
  322. query getMe {
  323. me {
  324. # ...
  325. }
  326. }
  327. # `user` represents one of many users in a graph of data.
  328. query getZuck {
  329. user(id: 4) {
  330. # ...
  331. }
  332. }
  333. </code></pre></section>
  334. <section id="sec-Field-Selections">
  335. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Field-Selections">2.5</a></span>Field Selections</h3>
  336. <p>Each field is of a specific type, and the sub&#8208;fields must always be explicitly declared via a field selection, unless it is a scalar. For example, when fetching data from some user object:</p><pre><code>query getZuck {
  337. user(id: 4) {
  338. id
  339. firstName
  340. lastName
  341. }
  342. }
  343. </code></pre>
  344. <p>Field selections can be further composed to explicitly state all subfields of nested types. All queries must specify down to scalar fields.</p><pre><code>query getZuck {
  345. user(id: 4) {
  346. id
  347. firstName
  348. lastName
  349. birthday {
  350. month
  351. day
  352. }
  353. }
  354. }
  355. </code></pre></section>
  356. <section id="sec-Language.Arguments">
  357. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Arguments">2.6</a></span>Arguments</h3>
  358. <p>Fields and directives may take arguments.</p>
  359. <p>These often map directly to function arguments within the GraphQL server implementation. We already saw arguments used in the global field above.</p>
  360. <p>In this example, we want to query a user&rsquo;s profile picture of a specific size:</p><pre><code>{
  361. user(id: 4) {
  362. id
  363. name
  364. profilePic(size: 100)
  365. }
  366. }
  367. </code></pre>
  368. <p>Many arguments can exist for a given field:</p><pre><code>{
  369. user(id: 4) {
  370. id
  371. name
  372. profilePic(width: 100, height: 50)
  373. }
  374. }
  375. </code></pre>
  376. <p><strong>Arguments are unordered</strong></p>
  377. <p>Arguments may be provided in any syntactic order and maintain identical semantic meaning.</p>
  378. <p>These two queries are semantically identical:</p><pre><code>{
  379. picture(width: 200, height: 100)
  380. }
  381. </code></pre><pre><code>{
  382. picture(height: 100, width: 200)
  383. }
  384. </code></pre></section>
  385. <section id="sec-Field-Alias">
  386. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Field-Alias">2.7</a></span>Field Alias</h3>
  387. <p>By default, the key in the response object will use the field name queried. However, you can define a different name by specifying an alias.</p>
  388. <p>In this example, we can fetch two profile pictures of different sizes and ensure the resulting object will not have duplicate keys:</p><pre><code>{
  389. user(id: 4) {
  390. id
  391. name
  392. smallPic: profilePic(size: 64)
  393. bigPic: profilePic(size: 1024)
  394. }
  395. }
  396. </code></pre>
  397. <p>Which returns the result:</p><pre><code>{
  398. <span class="hljs-string">"user"</span>: {
  399. <span class="hljs-string">"id"</span>: <span class="hljs-number">4</span>,
  400. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark"</span>,
  401. <span class="hljs-string">"smallPic"</span>: <span class="hljs-string">"https://cdn.site.io/pic-4-64.jpg"</span>,
  402. <span class="hljs-string">"bigPic"</span>: <span class="hljs-string">"https://cdn.site.io/pic-4-1024.jpg"</span>
  403. }
  404. }
  405. </code></pre>
  406. <p>Since the top level of a query is a field, it also can be given an alias:</p><pre><code>{
  407. zuck: user(id: 4) {
  408. id
  409. name
  410. }
  411. }
  412. </code></pre>
  413. <p>Returns the result:</p><pre><code>{
  414. <span class="hljs-string">"zuck"</span>: {
  415. <span class="hljs-string">"id"</span>: <span class="hljs-number">4</span>,
  416. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark Zuckerberg"</span>
  417. }
  418. }
  419. </code></pre>
  420. <p>A field&rsquo;s response key is its alias if an alias is provided, and it is the field&rsquo;s name otherwise.</p>
  421. </section>
  422. <section id="sec-Input-Values">
  423. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Input-Values">2.8</a></span>Input Values</h3>
  424. <p>Field and directive arguments accept input values. Input values can be specified as a variable or represented inline as literals. Input values can be scalars, enumerations, or input objects. List and inputs objects may also contain variables.</p>
  425. <p><strong>Int</strong></p>
  426. <p>Int is a number specified without a decimal point (ex. <code>1</code>).</p>
  427. <p><strong>Float</strong></p>
  428. <p>A Float numbers always includes a decimal point (ex. <code>1.0</code>) and may optionally also include an exponent (ex. <code>6.0221413e23</code>).</p>
  429. <p><strong>Boolean</strong></p>
  430. <p>The two keywords <code>true</code> and <code>false</code> represent the two boolean values.</p>
  431. <p><strong>String</strong></p>
  432. <p>Strings are lists of characters wrapped in double&#8208;quotes <code>&quot;</code>. (ex. <code>&quot;Hello World&quot;</code>). Whitespace is significant within a string.</p>
  433. <p><strong>Enum Value</strong></p>
  434. <p>Enum values are represented as unquoted names (ex. <code>MOBILE_WEB</code>). It is recommended that Enum values be &ldquo;all caps&rdquo;. Enum values are only used in contexts where the precise enumeration type is known. Therefore it&rsquo;s not
  435. necessary to use the enumeration type name in the literal.</p>
  436. <p><strong>List</strong></p>
  437. <p>Lists are ordered sequences of values wrapped in square&#8208;brackets <code>[ ]</code>. The values of a List literal may be any value literal or variable (ex. <code>[1, 2, 3]</code>).</p>
  438. <p>Commas are optional throughout GraphQL so trailing commas are allowed and repeated commas do not represent missing values.</p>
  439. <p><strong>Input Object</strong></p>
  440. <p>Input object literals are unordered lists of keyed input values wrapped in curly&#8208;braces <code>{ }</code>. The values of an object literal may be any input value literal or variable (ex. <code>{ name: &quot;Hello world&quot;, score: 1.0 }</code>).
  441. We refer to literal representation of input objects as &ldquo;object literals.&rdquo;</p>
  442. </section>
  443. <section id="sec-Language.Variables">
  444. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Variables">2.9</a></span>Variables</h3>
  445. <p>A GraphQL query can be parameterized with variables, maximizing query reuse, and avoiding costly string building in clients at runtime.</p>
  446. <p>Variables must be defined at the top of an operation and have global scope.</p>
  447. <p>In this example, we want to fetch a profile picture size based on the size of a particular device:</p><pre><code>query getZuckProfile($devicePicSize: Int) {
  448. user(id: 4) {
  449. id
  450. name
  451. profilePic(size: $devicePicSize)
  452. }
  453. }
  454. </code></pre>
  455. <p>Values for those variables are provided along with a GraphQL query, so they may be substituted during execution. If providing JSON for the variables values, we could run this query and request profilePic of size 60 width:</p><pre><code>{
  456. <span class="hljs-string">"devicePicSize"</span>: <span class="hljs-number">60</span>
  457. }
  458. </code></pre></section>
  459. <section id="sec-Language.Fragments">
  460. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Fragments">2.10</a></span>Fragments</h3>
  461. <p>Fragments allow for reuse of repeated portions of a query. It is the unit of composition in GraphQL.</p>
  462. <p>For example, if we wanted to fetch some common information about mutual friends as well as friends of some user:</p><pre><code>query noFragments {
  463. user(id: 4) {
  464. friends(first: 10) {
  465. id
  466. name
  467. profilePic(size: 50)
  468. }
  469. mutualFriends(first: 10) {
  470. id
  471. name
  472. profilePic(size: 50)
  473. }
  474. }
  475. }
  476. </code></pre>
  477. <p>The repeated fields could be extracted into a fragment and composed by a parent fragment or query.</p><pre><code>query withFragments {
  478. user(id: 4) {
  479. friends(first: 10) {
  480. ...friendFields
  481. }
  482. mutualFriends(first: 10) {
  483. ...friendFields
  484. }
  485. }
  486. }
  487. fragment friendFields on User {
  488. id
  489. name
  490. profilePic(size: 50)
  491. }
  492. </code></pre>
  493. <p>Fragments are consumed by using the spread operator (<code>...</code>). All fields selected by the fragment will be added to the query field selection at the same level as the fragment invocation. This happens through multiple levels of fragment
  494. spreads.</p>
  495. <p>For example:</p><pre><code>query withNestedFragments {
  496. user(id: 4) {
  497. friends(first: 10) {
  498. ...friendFields
  499. }
  500. mutualFriends(first: 10) {
  501. ...friendFields
  502. }
  503. }
  504. }
  505. fragment friendFields on User {
  506. id
  507. name
  508. ...standardProfilePic
  509. }
  510. fragment standardProfilePic on User {
  511. profilePic(size: 50)
  512. }
  513. </code></pre>
  514. <p>The queries <code>noFragments</code>, <code>withFragments</code>, and <code>withNestedFragments</code> all produce the same response object.</p>
  515. <section id="sec-Types-on-fragments">
  516. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Types-on-fragments">2.10.1</a></span>Types on fragments</h4>
  517. <p>Fragments must specify the type they apply to. In this example, <code>friendFields</code> can be used in the context of querying a <code>User</code>.</p>
  518. <p>Fragments cannot be specified on any input value (scalar, enumeration, or input object).</p>
  519. <p>Fragments can be specified on object types, interfaces, and unions.</p>
  520. <p>Selections within fragments only return values when concrete type of the object it is operating on matches the type of the fragment.</p>
  521. <p>For example in this query on the Facebook data model:</p><pre><code>query FragmentTyping {
  522. profiles(handles: [&quot;zuck&quot;, &quot;cocacola&quot;]) {
  523. handle
  524. ...userFragment
  525. ...pageFragment
  526. }
  527. }
  528. fragment userFragment on User {
  529. friends {
  530. count
  531. }
  532. }
  533. fragment pageFragment on Page {
  534. likers {
  535. count
  536. }
  537. }
  538. </code></pre>
  539. <p>The <code>profiles</code> root field returns a list where each element could be a <code>Page</code> or a <code>User</code>. When the object in the <code>profiles</code> result is a <code>User</code>, <code>friends</code> will be present and <code>likers</code> will not. Conversely when the result is a <code>Page</code>, <code>likers</code> will be present and <code>friends</code> will not.</p><pre><code>{
  540. <span class="hljs-string">"profiles"</span> : [
  541. {
  542. <span class="hljs-string">"handle"</span> : <span class="hljs-string">"zuck"</span>,
  543. <span class="hljs-string">"friends"</span> : { <span class="hljs-string">"count"</span> : <span class="hljs-number">1234</span> }
  544. },
  545. {
  546. <span class="hljs-string">"handle"</span> : <span class="hljs-string">"cocacola"</span>,
  547. <span class="hljs-string">"likers"</span> : { <span class="hljs-string">"count"</span> : <span class="hljs-number">90234512</span> }
  548. }
  549. ]
  550. }
  551. </code></pre></section>
  552. <section id="sec-Query-variables-in-fragments">
  553. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Query-variables-in-fragments">2.10.2</a></span>Query variables in fragments</h4>
  554. <p>Query variables can be used within fragments. Query variables have global scope with a given operation, so a variable used within a fragment must be declared in any top&#8208;level operation that transitively consumes that fragment. If a variable
  555. is referenced in a fragment and is included by an operation that does not define that variable, the operation cannot be executed.</p>
  556. </section>
  557. <section id="sec-Inline-fragments">
  558. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Inline-fragments">2.10.3</a></span>Inline fragments</h4>
  559. <p>Fragments can be defined inline to query. This is done to conditionally execute fields based on their runtime type. This feature of standard fragment inclusion was demonstrated in the <code>query FragmentTyping</code> example. We could accomplish
  560. the same thing using inline fragments.</p><pre><code>query inlineFragmentTyping {
  561. profiles(handles: [&quot;zuck&quot;, &quot;cocacola&quot;]) {
  562. handle
  563. ... on User {
  564. friends {
  565. count
  566. }
  567. }
  568. ... on Page {
  569. likers {
  570. count
  571. }
  572. }
  573. }
  574. }
  575. </code></pre></section>
  576. </section>
  577. <section id="sec-Language.Directives">
  578. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Language.Directives">2.11</a></span>Directives</h3>
  579. <p>In some cases, you need to provide options to alter GraphQL&rsquo;s execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to
  580. the executor.</p>
  581. <p>Directives have a name along with a list of arguments which may accept values of any input type.</p>
  582. <p>Directives can be used to describe additional information for fields, fragments, and operations.</p>
  583. <p>As future versions of GraphQL adopts new configurable execution capabilities, they may be exposed via directives.</p>
  584. <section id="sec-Fragment-Directives">
  585. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-Directives">2.11.1</a></span>Fragment Directives</h4>
  586. <p>Fragments may include directives to alter their behavior. At runtime, the directives provided on a fragment spread override those described on the definition.</p>
  587. <p>For example, the following query:</p><pre><code>query hasConditionalFragment($condition: Boolean) {
  588. ...maybeFragment @include(if: $condition)
  589. }
  590. fragment maybeFragment on Query {
  591. me {
  592. name
  593. }
  594. }
  595. </code></pre>
  596. <p>Will have identical runtime behavior as</p><pre><code>query hasConditionalFragment($condition: Boolean) {
  597. ...maybeFragment
  598. }
  599. fragment maybeFragment on Query @include(if: $condition) {
  600. me {
  601. name
  602. }
  603. }
  604. </code></pre>
  605. <div class="spec-algo" id="FragmentSpreadDirectives()"><span class="spec-call"><a href="#FragmentSpreadDirectives()">FragmentSpreadDirectives</a>(<var>fragmentSpread</var>)</span>
  606. <ol>
  607. <li>Let <var>directives</var> be the set of directives on <var>fragmentSpread</var></li>
  608. <li>Let <var>fragmentDefinition</var> be the FragmentDefinition in the document named <var>fragmentSpread</var> refers to.</li>
  609. <li>For each <var>directive</var> in directives on <var>fragmentDefinition</var>
  610. <ol>
  611. <li>If <var>directives</var> does not contain a directive named <var>directive</var>.</li>
  612. <li>Add <var>directive</var> into <var>directives</var></li>
  613. </ol>
  614. </li>
  615. <li>Return <var>directives</var> </li>
  616. </ol>
  617. </div>
  618. </section>
  619. </section>
  620. </section>
  621. <section id="sec-Type-System">
  622. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Type-System">3</a></span>Type System</h2>
  623. <p>The GraphQL Type system describes the capabilities of a GraphQL server and is used to determine if a query is valid. The type system also describes the input types of query variables to determine if values provided at runtime are valid.</p>
  624. <p>A GraphQL server&rsquo;s capabilities are referred to as that server&rsquo;s &ldquo;schema&rdquo;. A schema is defined in terms of the types and directives it supports.</p>
  625. <p>A given GraphQL schema must itself be internally valid. This section describes the rules for this validation process where relevant.</p>
  626. <p>A GraphQL schema is represented by a root type for each kind of operation: query and mutation; this determines the place in the type system where those operations begin.</p>
  627. <p>All types within a GraphQL schema must have unique names. No two provided types may have the same name. No provided type may have a name which conflicts with any built in types (including Scalar and Introspection types).</p>
  628. <p>All directives within a GraphQL schema must have unique names. A directive and a type may share the same name, since there is no ambiguity between them.</p>
  629. <section id="sec-Type-System.Types">
  630. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Type-System.Types">3.1</a></span>Types</h3>
  631. <p>The fundamental unit of any GraphQL Schema is the type. There are eight kinds of types in GraphQL.</p>
  632. <p>The most basic type is a <code>Scalar</code>. A scalar represents a primitive value, like a string or an integer. Oftentimes, the possible responses for a scalar field are enumerable. GraphQL offers an <code>Enum</code> type in those cases, where
  633. the type specifies the space of valid responses.</p>
  634. <p>Scalars and Enums form the leaves in response trees; the intermediate levels are <code>Object</code> types, which define a set of fields, where each field is another type in the system, allowing the definition of arbitrary type hierarchies.</p>
  635. <p>GraphQL supports two abstract types: interfaces and unions.</p>
  636. <p>An <code>Interface</code> defines a list of fields; <code>Object</code> types that implement that interface are guaranteed to implement those fields. Whenever the type system claims it will return an interface, it will return a valid implementing
  637. type.</p>
  638. <p>A <code>Union</code> defines a list of possible types; similar to interfaces, whenever the type system claims a union will be returned, one of the possible types will be returned.</p>
  639. <p>All of the types so far are assumed to be both nullable and singular: e.g. a scalar string returns either null or a singular string. The type system might want to define that it returns a list of other types; the <code>List</code> type is provided
  640. for this reason, and wraps another type. Similarly, the <code>Non-Null</code> type wraps another type, and denotes that the result will never be null. These two types are referred to as &ldquo;wrapping types&rdquo;; non&#8208;wrapping types are
  641. referred to as &ldquo;base types&rdquo;. A wrapping type has an underlying &ldquo;base type&rdquo;, found by continually unwrapping the type until a base type is found.</p>
  642. <p>Finally, oftentimes it is useful to provide complex structs as inputs to GraphQL queries; the <code>Input Object</code> type allows the schema to define exactly what data is expected from the client in these queries.</p>
  643. <section id="sec-Scalars">
  644. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Scalars">3.1.1</a></span>Scalars</h4>
  645. <p>As expected by the name, a scalar represents a primitive value in GraphQL. GraphQL responses take the form of a hierarchical tree; the leaves on these trees are GraphQL scalars.</p>
  646. <p>All GraphQL scalars are representable as strings, though depending on the response format being used, there may be a more appropriate primitive for the given scalar type, and server should use those types when appropriate.</p>
  647. <p>GraphQL provides a number of built&#8208;in scalars, but type systems can add additional scalars with semantic meaning. For example, a GraphQL system could define a scalar called <code>Time</code> which, while serialized as a string, promises
  648. to conform to ISO&#8208;8601. When querying a field of type <code>Time</code>, you can then rely on the ability to parse the result with an ISO&#8208;8601 parser and use a client&#8208;specific primitive for time. Another example of a potentially
  649. useful custom scalar is <code>Url</code>, which serializes as a string, but is guaranteed by the server to be a valid URL.</p>
  650. <p><strong>Result Coercion</strong></p>
  651. <p>A GraphQL server, when preparing a field of a given scalar type, must uphold the contract the scalar type describes, either by coercing the value or producing an error.</p>
  652. <p>For example, a GraphQL server could be preparing a field with the scalar type <code>Int</code> and encounter a floating&#8208;point number. Since the server must not break the contract by yielding a non&#8208;integer, the server should truncate
  653. the fractional value and only yield the integer value. If the server encountered a boolean <code>true</code> value, it should return <code>1</code>. If the server encountered a string, it may attempt to parse the string for a base&#8208;10 integer
  654. value. If the server encounters some value that cannot be reasonably coerced to an <code>Int</code>, then it must raise a field error.</p>
  655. <p>Since this coercion behavior is not observable to clients of the GraphQL server, the precise rules of coercion are left to the implementation. The only requirement is that the server must yield values which adhere to the expected Scalar type.</p>
  656. <p><strong>Input Coercion</strong></p>
  657. <p>If a GraphQL server expects a scalar type as input to an argument, coercion is observable and the rules must be well defined. If an input value does not match a coercion rule, a query error must be raised.</p>
  658. <p>GraphQL has different constant literals to represent integer and floating&#8208;point input values, and coercion rules may apply differently depending on which type of input value is encountered. GraphQL may be parameterized by query variables,
  659. the values of which are often serialized when sent over a transport like HTTP. Since some common serializations (ex. JSON) do not discriminate between integer and floating&#8208;point values, they are interpreted as an integer input value if
  660. they have an empty fractional part (ex. <code>1.0</code>) and otherwise as floating&#8208;point input value.</p>
  661. <section id="sec-Built-in-Scalars">
  662. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Built-in-Scalars">3.1.1.1</a></span>Built-in Scalars</h5>
  663. <p>GraphQL provides a basic set of well&#8208;defined Scalar types. A GraphQL server should support all of these types, and a GraphQL server which provide a type by these names must adhere to the behavior described below.</p>
  664. <section id="sec-Int">
  665. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Int">3.1.1.1.1</a></span>Int</h6>
  666. <p>The Int scalar type represents a signed 32&#8208;bit numeric non&#8208;fractional values. Response formats that support a 32&#8208;bit integer or a number type should use that type to represent this scalar.</p>
  667. <p><strong>Result Coercion</strong></p>
  668. <p>GraphQL servers should coerce non&#8208;int raw values to Int when possible otherwise they must raise a field error. Examples of this may include returning <code>1</code> for the floating&#8208;point number <code>1.0</code>, or <code>2</code> for the string <code>&quot;2&quot;</code>.</p>
  669. <p><strong>Input Coercion</strong></p>
  670. <p>When expected as an input type, only integer input values are accepted. All other input values, including strings with numeric content, must raise a query error indicating an incorrect type. If the integer input value represents a value less
  671. than -2<sup>31</sup> or greater than or equal to 2<sup>31</sup>, a query error should be raised.</p>
  672. <div class="spec-note">Numeric integer values larger than 32&#8208;bit should either use String or a custom&#8208;defined Scalar type, as not all platforms and transports support encoding integer numbers larger than 32&#8208;bit.</div>
  673. </section>
  674. <section id="sec-Float">
  675. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Float">3.1.1.1.2</a></span>Float</h6>
  676. <p>The Float scalar type represents signed double&#8208;precision fractional values as specified by <a href="http://en.wikipedia.org/wiki/IEEE_floating_point">IEEE 754</a>. Response formats that support an appropriate double&#8208;precision number
  677. type should use that type to represent this scalar.</p>
  678. <p><strong>Result Coercion</strong></p>
  679. <p>GraphQL servers should coerce non&#8208;floating&#8208;point raw values to Float when possible otherwise they must raise a field error. Examples of this may include returning <code>1.0</code> for the integer number <code>1</code>, or <code>2.0</code> for the string <code>&quot;2&quot;</code>.</p>
  680. <p><strong>Input Coercion</strong></p>
  681. <p>When expected as an input type, both integer and float input values are accepted. Integer input values are coerced to Float by adding an empty fractional part, for example <code>1.0</code> for the integer input value <code>1</code>. All other
  682. input values, including strings with numeric content, must raise a query error indicating an incorrect type. If the integer input value represents a value not representable by IEEE 754, a query error should be raised.</p>
  683. </section>
  684. <section id="sec-String">
  685. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-String">3.1.1.1.3</a></span>String</h6>
  686. <p>The String scalar type represents textual data, represented as UTF&#8208;8 character sequences. The String type is most often used by GraphQL to represent free&#8208;form human&#8208;readable text. All response formats must support string
  687. representations, and that representation must be used here.</p>
  688. <p><strong>Result Coercion</strong></p>
  689. <p>GraphQL servers should coerce non&#8208;string raw values to String when possible otherwise they must raise a field error. Examples of this may include returning the string <code>&quot;true&quot;</code> for a boolean true value, or the string
  690. <code>&quot;1&quot;</code> for the integer <code>1</code>.</p>
  691. <p><strong>Input Coercion</strong></p>
  692. <p>When expected as an input type, only valid UTF&#8208;8 string input values are accepted. All other input values must raise a query error indicating an incorrect type.</p>
  693. </section>
  694. <section id="sec-Boolean">
  695. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Boolean">3.1.1.1.4</a></span>Boolean</h6>
  696. <p>The Boolean scalar type represents <code>true</code> or <code>false</code>. Response formats should use a built&#8208;in boolean type if supported; otherwise, they should use their representation of the integers <code>1</code> and <code>0</code>.</p>
  697. <p><strong>Result Coercion</strong></p>
  698. <p>GraphQL servers should coerce non&#8208;boolean raw values to Boolean when possible otherwise they must raise a field error. Examples of this may include returning <code>true</code> for any non&#8208;zero number.</p>
  699. <p><strong>Input Coercion</strong></p>
  700. <p>When expected as an input type, only boolean input values are accepted. All other input values must raise a query error indicating an incorrect type.</p>
  701. </section>
  702. <section id="sec-ID">
  703. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-ID">3.1.1.1.5</a></span>ID</h6>
  704. <p>The ID scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type is serialized in the same way as a <code>String</code>; however, it is not intended to be human&#8208;readable. While it
  705. is often numeric, it should always serialize as a <code>String</code>.</p>
  706. <p><strong>Result Coercion</strong></p>
  707. <p>GraphQL is agnostic to ID format, and serializes to string to ensure consistency across many formats ID could represent, from small auto&#8208;increment numbers, to large 128&#8208;bit random numbers, to base64 encoded values, or string values
  708. of a format like <a href="http://en.wikipedia.org/wiki/Globally_unique_identifier">GUID</a>.</p>
  709. <p>GraphQL servers should coerce as appropriate given the ID formats they expect, when coercion is not possible they must raise a field error.</p>
  710. <p><strong>Input Coercion</strong></p>
  711. <p>When expected as an input type, any string (such as <code>&quot;4&quot;</code>) or integer (such as <code>4</code>) input value should be coerced to ID as appropriate for the ID formats a given GraphQL server expects. Any other input value,
  712. including float input values (such as <code>4.0</code>), must raise a query error indicating an incorrect type.</p>
  713. </section>
  714. </section>
  715. </section>
  716. <section id="sec-Objects">
  717. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Objects">3.1.2</a></span>Objects</h4>
  718. <p>GraphQL queries are hierarchical and composed, describing a tree of information. While Scalar types describe the leaf values of these hierarchical queries, Objects describe the intermediate levels.</p>
  719. <p>GraphQL Objects represent a list of named fields, each of which yield a value of a specific type. Object values are serialized as unordered maps, where the queried field names (or aliases) are the keys and the result of evaluating the field is
  720. the value.</p>
  721. <p>For example, a type <code>Person</code> could be described as:</p><pre><code>type Person {
  722. name: String
  723. age: Int
  724. picture: Url
  725. }
  726. </code></pre>
  727. <p>Where <code>name</code> is a field that will yield a <code>String</code> value, and <code>age</code> is a field that will yield an <code>Int</code> value, and <code>picture</code> a field that will yield a <code>Url</code> value.</p>
  728. <p>A query of an object value must select at least one field. This selection of fields will yield an unordered map containing exactly the subset of the object queried. Only fields that are declared on the object type may validly be queried on that
  729. object.</p>
  730. <p>For example, selecting all the fields of <code>Person</code>:</p><pre><code>{
  731. name
  732. age
  733. picture
  734. }
  735. </code></pre>
  736. <p>Would yield the object:</p><pre><code>{
  737. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark Zuckerberg"</span>,
  738. <span class="hljs-string">"age"</span>: <span class="hljs-number">30</span>,
  739. <span class="hljs-string">"picture"</span>: <span class="hljs-string">"http://some.cdn/picture.jpg"</span>
  740. }
  741. </code></pre>
  742. <p>While selecting a subset of fields:</p><pre><code>{
  743. name
  744. age
  745. }
  746. </code></pre>
  747. <p>Must only yield exactly that subset:</p><pre><code>{
  748. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark Zuckerberg"</span>,
  749. <span class="hljs-string">"age"</span>: <span class="hljs-number">30</span>
  750. }
  751. </code></pre>
  752. <p>A field of an Object type may be a Scalar, Enum, another Object type, an Interface, or a Union. Additionally, it may be any wrapping type whose underlying base type is one of those five.</p>
  753. <p>For example, the <code>Person</code> type might include a <code>relationship</code>:</p><pre><code>type Person {
  754. name: String
  755. age: Int
  756. picture: Url
  757. relationship: Person
  758. }
  759. </code></pre>
  760. <p>Valid queries must supply a nested field set for a field that returns an object, so this query is not valid:</p><pre class="spec-counter-example"><code>{
  761. name
  762. relationship
  763. }
  764. </code></pre>
  765. <p>However, this example is valid:</p><pre><code>{
  766. name
  767. relationship {
  768. name
  769. }
  770. }
  771. </code></pre>
  772. <p>And will yield the subset of each object type queried:</p><pre><code>{
  773. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark Zuckerberg"</span>,
  774. <span class="hljs-string">"relationship"</span>: {
  775. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Priscilla Chan"</span>
  776. }
  777. }
  778. </code></pre>
  779. <p><strong>Result Coercion</strong></p>
  780. <p>Determining the result of coercing an object is the heart of the GraphQL executor, so this is covered in that section of the spec.</p>
  781. <p><strong>Input Coercion</strong></p>
  782. <p>Objects are never valid inputs.</p>
  783. <section id="sec-Object-Field-Arguments">
  784. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Object-Field-Arguments">3.1.2.1</a></span>Object Field Arguments</h5>
  785. <p>Object fields are conceptually functions which yield values. Occasionally object fields can accept arguments to further specify the return value. Object field arguments are defined as a list of all possible argument names and their expected
  786. input types.</p>
  787. <p>For example, a <code>Person</code> type with a <code>picture</code> field could accept an argument to determine what size of an image to return.</p><pre><code>type Person {
  788. name: String
  789. picture(size: Int): Url
  790. }
  791. </code></pre>
  792. <p>GraphQL queries can optionally specify arguments to their fields to provide these arguments.</p>
  793. <p>This example query:</p><pre><code>{
  794. name
  795. picture(size: 600)
  796. }
  797. </code></pre>
  798. <p>May yield the result:</p><pre><code>{
  799. <span class="hljs-string">"name"</span>: <span class="hljs-string">"Mark Zuckerberg"</span>,
  800. <span class="hljs-string">"picture"</span>: <span class="hljs-string">"http://some.cdn/picture_600.jpg"</span>
  801. }
  802. </code></pre>
  803. <p>The type of an object field argument can be any Input type.</p>
  804. </section>
  805. <section id="sec-Object-Field-deprecation">
  806. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Object-Field-deprecation">3.1.2.2</a></span>Object Field deprecation</h5>
  807. <p>Fields in an object may be marked as deprecated as deemed necessary by the application. It is still legal to query for these fields (to ensure existing clients are not broken by the change), but the fields should be appropriately treated in
  808. documentation and tooling.</p>
  809. </section>
  810. <section id="sec-Object-type-validation">
  811. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Object-type-validation">3.1.2.3</a></span>Object type validation</h5>
  812. <p>Object types have the potential to be invalid if incorrectly defined. This set of rules must be adhered to by every Object type in a GraphQL schema.</p>
  813. <ol>
  814. <li>The fields of an Object type must have unique names within that Object type; no two fields may share the same name.</li>
  815. <li>An object type must be a super&#8208;set of all interfaces it implements.
  816. <ol>
  817. <li>The object type must include a field of the same name for every field defined in an interface.
  818. <ol>
  819. <li>The object field must include an argument of the same name for every argument defined by the interface field.
  820. <ol>
  821. <li>The object field argument must accept the same type (invariant) as the interface field argument.</li>
  822. </ol>
  823. </li>
  824. <li>The object field must be of a type which is equal to the interface field.</li>
  825. </ol>
  826. </li>
  827. </ol>
  828. </li>
  829. </ol>
  830. </section>
  831. </section>
  832. <section id="sec-Interfaces">
  833. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Interfaces">3.1.3</a></span>Interfaces</h4>
  834. <p>GraphQL Interfaces represent a list of named fields and their arguments. GraphQL object can then implement an interface, which guarantees that they will contain the specified fields.</p>
  835. <p>Fields on a GraphQL interface have the same rules as fields on a GraphQL object; their type can be Scalar, Object, Enum, Interface, or Union, or any wrapping type whose base type is one of those five.</p>
  836. <p>For example, an interface may describe a required field and types such as <code>Person</code> or <code>Business</code> may then implement this interface.</p><pre><code>interface NamedEntity {
  837. name: String
  838. }
  839. type Person : NamedEntity {
  840. name: String
  841. age: Int
  842. }
  843. type Business : NamedEntity {
  844. name: String
  845. employeeCount: Int
  846. }
  847. </code></pre>
  848. <p>Fields which yield an interface are useful when one of many Object types are expected, but some fields should be guaranteed.</p>
  849. <p>To continue the example, a <code>Contact</code> might refer to <code>NamedEntity</code>.</p><pre><code>type Contact {
  850. entity: NamedEntity
  851. phoneNumber: String
  852. address: String
  853. }
  854. </code></pre>
  855. <p>This allows us to write a query for a <code>Contact</code> that can select the common fields.</p><pre><code>{
  856. entity {
  857. name
  858. }
  859. phoneNumber
  860. }
  861. </code></pre>
  862. <p>When querying for fields on an interface type, only those fields declared on the interface may be queried. In the above example, <code>entity</code> returns a <code>NamedEntity</code>, and <code>name</code> is defined on <code>NamedEntity</code>,
  863. so it is valid. However, the following would not be a valid query:</p><pre class="spec-counter-example"><code>{
  864. entity {
  865. name
  866. age
  867. }
  868. phoneNumber
  869. }
  870. </code></pre>
  871. <p>because <code>entity</code> refers to a <code>NamedEntity</code>, and <code>age</code> is not defined on that interface. Querying for <code>age</code> is only valid when the result of <code>entity</code> is a <code>Person</code>; the query can
  872. express this using a fragment or an inline fragment:</p><pre><code>{
  873. entity {
  874. name
  875. ... on Person {
  876. age
  877. }
  878. },
  879. phoneNumber
  880. }
  881. </code></pre>
  882. <p><strong>Result Coercion</strong></p>
  883. <p>The interface type should have some way of determining which object a given result corresponds to. Once it has done so, the result coercion of the interface is the same as the result coercion of the object.</p>
  884. <p><strong>Input Coercion</strong></p>
  885. <p>Interfaces are never valid inputs.</p>
  886. <section id="sec-Interface-type-validation">
  887. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Interface-type-validation">3.1.3.1</a></span>Interface type validation</h5>
  888. <p>Interface types have the potential to be invalid if incorrectly defined.</p>
  889. <ol>
  890. <li>The fields of an Interface type must have unique names within that Interface type; no two fields may share the same name.</li>
  891. </ol>
  892. </section>
  893. </section>
  894. <section id="sec-Unions">
  895. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Unions">3.1.4</a></span>Unions</h4>
  896. <p>GraphQL Unions represent an object that could be one of a list of GraphQL Object types, but provides for no guaranteed fields between those types. They also differ from interfaces in that Object types declare what interfaces they implement, but
  897. are not aware of what unions contain them.</p>
  898. <p>With interfaces and objects, only those fields defined on the type can be queried directly; to query other fields on an interface, typed fragments must be used. This is the same as for unions, but unions do not define any fields, so <strong>no</strong> fields may be queried on this type without the use of typed fragments.</p>
  899. <p>For example, we might have the following type system:</p><pre><code>Union SearchResult = Photo | Person
  900. type Person {
  901. name: String
  902. age: Int
  903. }
  904. type Photo {
  905. height: Int
  906. width: Int
  907. }
  908. type SearchQuery {
  909. firstSearchResult: SearchResult
  910. }
  911. </code></pre>
  912. <p>When querying the <code>firstSearchResult</code> field of type <code>SearchQuery</code>, the query would ask for all fields inside of a fragment indicating the appropriate type. If the query wanted the name if the result was a Person, and the
  913. height if it was a photo, the following query is invalid, because the union itself defines no fields:</p><pre class="spec-counter-example"><code>{
  914. firstSearchResult {
  915. name
  916. height
  917. }
  918. }
  919. </code></pre>
  920. <p>Instead, the query would be:</p><pre><code>{
  921. firstSearchResult {
  922. ... on Person {
  923. name
  924. }
  925. ... on Photo {
  926. height
  927. }
  928. }
  929. }
  930. </code></pre>
  931. <p><strong>Result Coercion</strong></p>
  932. <p>The union type should have some way of determining which object a given result corresponds to. Once it has done so, the result coercion of the union is the same as the result coercion of the object.</p>
  933. <p><strong>Input Coercion</strong></p>
  934. <p>Unions are never valid inputs.</p>
  935. <section id="sec-Union-type-validation">
  936. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Union-type-validation">3.1.4.1</a></span>Union type validation</h5>
  937. <p>Union types have the potential to be invalid if incorrectly defined.</p>
  938. <ol>
  939. <li>The member types of an Union type must all be Object base types; Scalar, Interface and Union types may not be member types of a Union. Similarly, wrapping types may not be member types of a Union.</li>
  940. <li>A Union type must define two or more member types.</li>
  941. </ol>
  942. </section>
  943. </section>
  944. <section id="sec-Enums">
  945. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Enums">3.1.5</a></span>Enums</h4>
  946. <p>GraphQL Enums are a variant on the Scalar type, which represents one of a finite set of possible values.</p>
  947. <p>GraphQL Enums are not references for a numeric value, but are unique values in their own right. They serialize as a string: the name of the represented value.</p>
  948. <p><strong>Result Coercion</strong></p>
  949. <p>GraphQL servers must return one of the defined set of possible values, if a reasonable coercion is not possible they must raise a field error.</p>
  950. <p><strong>Input Coercion</strong></p>
  951. <p>GraphQL has a constant literal to represent enum input values. GraphQL string literals must not be accepted as an enum input and instead raise a query error.</p>
  952. <p>Query variable transport serializations which have a different representation for non&#8208;string symbolic values (for example, <a href="https://github.com/edn-format/edn">EDN</a>) should only allow such values as enum input values. Otherwise,
  953. for most transport serializations that do not, strings may be interpreted as the enum input value with the same name.</p>
  954. </section>
  955. <section id="sec-Input-Objects">
  956. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Input-Objects">3.1.6</a></span>Input Objects</h4>
  957. <p>Fields can define arguments that the client passes up with the query, to configure their behavior. These inputs can be Strings or Enums, but they sometimes need to be more complex than this.</p>
  958. <p>The <code>Object</code> type defined above is inappropriate for re&#8208;use here, because <code>Object</code>s can contain fields that express circular references or references to interfaces and unions, neither of which is appropriate for use
  959. as an input argument. For this reason, input objects have a separate type in the system.</p>
  960. <p>An <code>Input Object</code> defines a set of input fields; the input fields are either scalars, enums, or other input objects. This allows arguments to accept arbitrarily complex structs.</p>
  961. <p><strong>Result Coercion</strong></p>
  962. <p>An input object is never a valid result.</p>
  963. <p><strong>Input Coercion</strong></p>
  964. <p>The input to an input object should be an unordered map, otherwise an error should be thrown. The result of the coercion is an unordered map, with an entry for each input field, whose key is the name of the input field. The value of an entry in
  965. the coerced map is the result of input coercing the value of the entry in the input with the same key; if the input does not have a corresponding entry, the value is the result of coercing null. The input coercion above should be performed according
  966. to the input coercion rules of the type declared by the input field.</p>
  967. </section>
  968. <section id="sec-Lists">
  969. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Lists">3.1.7</a></span>Lists</h4>
  970. <p>A GraphQL list is a special collection type which declares the type of each item in the List (referred to as the <em>item type</em> of the list). List values are serialized as ordered lists, where each item in the array is serialized as per the
  971. item type. To denote that a field uses a List type the item type is wrapped in square brackets like this: <code>pets: [Pet]</code>.</p>
  972. <p><strong>Result Coercion</strong></p>
  973. <p>GraphQL servers must return an ordered list as the result of a list type. Each item in the list must be the result of a result coercion of the item type. If a reasonable coercion is not possible they must raise a field error. In particular, if
  974. a non&#8208;list is returned, the coercion should fail, as this indicates a mismatch in expectations between the type system and the implementation.</p>
  975. <p><strong>Input Coercion</strong></p>
  976. <p>When expected as an input, list values are accepted only when each item in the list can be accepted by the list&rsquo;s item type.</p>
  977. <p>If the value passed as an input to a list type is <em>not</em> as list, it should be coerced as though the input was a list of size one, where the value passed is the only item in the list. This is to allow inputs that accept a &ldquo;var args&rdquo;
  978. to declare their input type as a list; if only one argument is passed (a common case), the client can just pass that value rather than constructing the list.</p>
  979. </section>
  980. <section id="sec-Non-Null">
  981. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Non-Null">3.1.8</a></span>Non-Null</h4>
  982. <p>By default, all types in GraphQL are nullable; the <span class="spec-keyword">null</span> value is a valid response for all of the above types. To declare a type that disallows null, the GraphQL Non&#8208;Null type can be used. This type declares
  983. an underlying type, and this type acts identically to that underlying type, with the exception that <code>null</code> is not a valid response for the wrapping type. A trailing exclamation mark is used to denote a field that uses a Non&#8208;Null
  984. type like this: <code>name: String!</code>.</p>
  985. <p><strong>Result Coercion</strong></p>
  986. <p>In all of the above result coercion, <code>null</code> was considered a valid value. To coerce the result of a Non Null type, the result coercion of the underlying type should be performed. If that result was not <code>null</code>, then the result
  987. of coercing the Non Null type is that result. If that result was <code>null</code>, then an error should be raised.</p>
  988. <p><strong>Input Coercion</strong></p>
  989. <div class="spec-note">that <code>null</code> is not a valid keyword in GraphQL, so a query cannot look like:</div><pre class="spec-counter-example"><code>{
  990. field(arg: null)
  991. }
  992. </code></pre>
  993. <p>to indicate that the argument is null. Instead, an argument would be null only if it is passed a variable that is then not set:</p><pre><code>{
  994. field(arg: $var)
  995. }
  996. </code></pre>
  997. <p>Hence, if the value for a non&#8208;null is hard&#8208;coded in the query, it is always coerced using the input coercion for the wrapped type.</p>
  998. <p>When a non&#8208;null input has its value set using a variable, the coerced value should be <code>null</code> if the provided value is <code>null</code>-like in the provided representation, or if the provided value is omitted. Otherwise, the coerced
  999. value is the result of running the wrapped type&rsquo;s input coercion on the provided value.</p>
  1000. </section>
  1001. </section>
  1002. <section id="sec-Type-System.Directives">
  1003. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Type-System.Directives">3.2</a></span>Directives</h3>
  1004. <p>A GraphQL schema includes a list of the directives the execution engine supports.</p>
  1005. <p>GraphQL implementations should provide the <code>@skip</code> and <code>@include</code> directives.</p>
  1006. <section id="sec--skip">
  1007. <h4><span class="spec-secnum" title="link to this section"><a href="#sec--skip">3.2.1</a></span>@skip</h4>
  1008. <p>The <code>@skip</code> directive may be provided for fields or fragments, and allows for conditional exclusion during execution as described by the if argument.</p>
  1009. <p>In this example <code>experimentalField</code> will be queried only if the <code>$someTest</code> is provided a <code>false</code> value.</p><pre><code>query myQuery($someTest: Boolean) {
  1010. experimentalField @skip(if: $someTest)
  1011. }
  1012. </code></pre></section>
  1013. <section id="sec--include">
  1014. <h4><span class="spec-secnum" title="link to this section"><a href="#sec--include">3.2.2</a></span>@include</h4>
  1015. <p>The <code>@include</code> directive may be provided for fields or fragments, and allows for conditional inclusion during execution as described by the if argument.</p>
  1016. <p>In this example <code>experimentalField</code> will be queried only if the <code>$someTest</code> is provided a <code>true</code> value.</p><pre><code>query myQuery($someTest: Boolean) {
  1017. experimentalField @include(if: $someTest)
  1018. }
  1019. </code></pre>
  1020. <p>The <code>@skip</code> directive has precedence over the <code>@include</code> directive should both be provided in the same context.</p>
  1021. </section>
  1022. </section>
  1023. <section id="sec-Starting-types">
  1024. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Starting-types">3.3</a></span>Starting types</h3>
  1025. <p>A GraphQL schema includes types, indicating where query and mutation operations start. This provides the initial entry points into the type system. The query type must always be provided, and is an Object base type. The mutation type is optional;
  1026. if it is null, that means the system does not support mutations. If it is provided, it must be an object base type.</p>
  1027. <p>The fields on the query type indicate what fields are available at the top level of a GraphQL query. For example, a basic GraphQL query like this one:</p><pre><code>query getMe {
  1028. me
  1029. }
  1030. </code></pre>
  1031. <p>Is valid when the type provided for the query starting type has a field named &ldquo;me&rdquo;. Similarly</p><pre><code>mutation setName {
  1032. setName(name: &quot;Zuck&quot;) {
  1033. newName
  1034. }
  1035. }
  1036. </code></pre>
  1037. <p>Is valid when the type provided for the mutation starting type is not null, and has a field named &ldquo;setName&rdquo; with a string argument named &ldquo;name&rdquo;. </p>
  1038. </section>
  1039. </section>
  1040. <section id="sec-Introspection">
  1041. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Introspection">4</a></span>Introspection</h2>
  1042. <p>A GraphQL server supports introspection over its schema. This schema is queried using GraphQL itself, creating a powerful platform for tool&#8208;building.</p>
  1043. <p>Take an example query for a trivial app. In this case there is a User type with three fields: id, user, and birthday.</p>
  1044. <p>For example, given a server with the following type definition:</p><pre><code>type User {
  1045. id: String
  1046. name: String
  1047. birthday: Date
  1048. }
  1049. </code></pre>
  1050. <p>The query</p><pre><code>{
  1051. __type(name: &quot;User&quot;) {
  1052. name
  1053. fields {
  1054. name
  1055. type {
  1056. name
  1057. }
  1058. }
  1059. }
  1060. }
  1061. </code></pre>
  1062. <p>would return</p><pre><code>{
  1063. <span class="hljs-string">"__type"</span>: {
  1064. <span class="hljs-string">"name"</span> : <span class="hljs-string">"User"</span>,
  1065. <span class="hljs-string">"fields"</span>: [
  1066. {
  1067. <span class="hljs-string">"name"</span>: <span class="hljs-string">"id"</span>,
  1068. <span class="hljs-string">"type"</span>: { <span class="hljs-string">"name"</span>: <span class="hljs-string">"String"</span> }
  1069. },
  1070. {
  1071. <span class="hljs-string">"name"</span>: <span class="hljs-string">"name"</span>,
  1072. <span class="hljs-string">"type"</span>: { <span class="hljs-string">"name"</span>: <span class="hljs-string">"String"</span> }
  1073. },
  1074. {
  1075. <span class="hljs-string">"name"</span>: <span class="hljs-string">"birthday"</span>,
  1076. <span class="hljs-string">"type"</span>: { <span class="hljs-string">"name"</span>: <span class="hljs-string">"Date"</span> }
  1077. },
  1078. ]
  1079. }
  1080. }
  1081. </code></pre>
  1082. <section id="sec-General-Principles">
  1083. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-General-Principles">4.1</a></span>General Principles</h3>
  1084. <section id="sec-Naming-conventions">
  1085. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Naming-conventions">4.1.1</a></span>Naming conventions</h4>
  1086. <p>Types and fields required by the GraphQL introspection system that are used in the same context as user&#8208;defined type and fields are prefixed with two underscores. This in order to avoid naming collisions with user&#8208;defined GraphQL types.
  1087. Conversely, GraphQL type system authors must not define any types, fields, arguments, or any other type system artifact with two leading underscores.</p>
  1088. </section>
  1089. <section id="sec-Documentation">
  1090. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Documentation">4.1.2</a></span>Documentation</h4>
  1091. <p>All types in the introspection system provide a <code>description</code> field of type <code>String</code> to allow type designers to publish documentation in addition to capabilities. A GraphQL server may return the <code>description</code> field
  1092. using Markdown syntax. Therefore it is recommended that any tool that displays description use a Markdown renderer.</p>
  1093. </section>
  1094. <section id="sec-Deprecation">
  1095. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Deprecation">4.1.3</a></span>Deprecation</h4>
  1096. <p>To support the management of backwards compatibility, GraphQL fields and enum values can indicate whether or not they are deprecated (<code>isDeprecated: Boolean</code>) and a description of why it is deprecated (<code>deprecationReason: String</code>).</p>
  1097. <p>Tools built using GraphQL introspection should respect deprecation by discouraging deprecated use through information hiding or developer&#8208;facing warnings.</p>
  1098. </section>
  1099. <section id="sec-Type-Name-Introspection">
  1100. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Type-Name-Introspection">4.1.4</a></span>Type Name Introspection</h4>
  1101. <p>GraphQL supports type name introspection at any point within a query by the meta field <code>__typename: String!</code> when querying against any Object, Interface, or Union. It returns the name of the object type currently being queried.</p>
  1102. <p>This is most often used when querying against Interface or Union types to identify which actual type of the possible types has been returned.</p>
  1103. <p>This field is implicit and does not appear in the fields list in any defined type.</p>
  1104. </section>
  1105. </section>
  1106. <section id="sec-Schema-Introspection">
  1107. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Schema-Introspection">4.2</a></span>Schema Introspection</h3>
  1108. <p>The schema introspection system is accessible from the meta&#8208;fields <code>__schema</code> and <code>__type</code> which are accessible from the type of the root of a query operation.</p><pre><code>__schema : __Schema!
  1109. __type(name: String) : __Type
  1110. </code></pre>
  1111. <p>These fields are implicit and do not appear in the fields list in the root type of the query operation.</p>
  1112. <p>The schema of the GraphQL schema introspection system:</p><pre><code>type __Schema {
  1113. types: [__Type!]!
  1114. queryType: __Type!
  1115. mutationType: __Type
  1116. directives: [__Directive!]!
  1117. }
  1118. type __Type {
  1119. kind: __TypeKind!
  1120. name: String
  1121. description: String
  1122. # OBJECT and INTERFACE only
  1123. fields(includeDeprecated: Boolean = false): [__Field!]
  1124. # OBJECT only
  1125. interfaces: [__Type!]
  1126. # INTERFACE and UNION only
  1127. possibleTypes: [__Type!]
  1128. # ENUM only
  1129. enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
  1130. # INPUT_OBJECT only
  1131. inputFields: [__InputValue!]
  1132. # NON_NULL and LIST only
  1133. ofType: __Type
  1134. }
  1135. type __Field {
  1136. name: String!
  1137. description: String
  1138. args: [__InputValue!]!
  1139. type: __Type!
  1140. isDeprecated: Boolean!
  1141. deprecationReason: String
  1142. }
  1143. type __InputValue {
  1144. name: String!
  1145. description: String
  1146. type: __Type!
  1147. defaultValue: String
  1148. }
  1149. type __EnumValue {
  1150. name: String!
  1151. description: String
  1152. isDeprecated: Boolean!
  1153. deprecationReason: String
  1154. }
  1155. enum __TypeKind {
  1156. SCALAR
  1157. OBJECT
  1158. INTERFACE
  1159. UNION
  1160. ENUM
  1161. INPUT_OBJECT
  1162. LIST
  1163. NON_NULL
  1164. }
  1165. type __Directive {
  1166. name: String!
  1167. description: String
  1168. args: [__InputValue!]!
  1169. onOperation: Boolean!
  1170. onFragment: Boolean!
  1171. onField: Boolean!
  1172. }
  1173. </code></pre>
  1174. <section id="sec-The-__Type-Type">
  1175. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-The-__Type-Type">4.2.1</a></span>The &quot;__Type&quot; Type</h4>
  1176. <p><code>__Type</code> is at the core of the type introspection system. It represents scalars, interfaces, object types, unions, enums in the system.</p>
  1177. <p><code>__Type</code> also represents type modifiers, which are used to modify a type that it refers to (<code>ofType: __Type</code>). This is how we represent lists, non&#8208;nullable types, and the combinations thereof.</p>
  1178. </section>
  1179. <section id="sec-Type-Kinds">
  1180. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Type-Kinds">4.2.2</a></span>Type Kinds</h4>
  1181. <p>There are several different kinds of type. In each kind, different fields are actually valid. These kinds are listed in the <code>__TypeKind</code> enumeration.</p>
  1182. <section id="sec-Scalar">
  1183. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Scalar">4.2.2.1</a></span>Scalar</h5>
  1184. <p>Represents scalar types such as Int, String, and Boolean. Scalars cannot have fields.</p>
  1185. <p>A GraphQL type designer should describe the data format and scalar coercion rules in the description field of any scalar.</p>
  1186. <p>Fields</p>
  1187. <ul>
  1188. <li><code>kind</code> must return <code>__TypeKind.SCALAR</code>.</li>
  1189. <li><code>name</code> must return a String.</li>
  1190. <li><code>description</code> may return a String or <span class="spec-keyword">null</span>.</li>
  1191. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1192. </ul>
  1193. </section>
  1194. <section id="sec-Object">
  1195. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Object">4.2.2.2</a></span>Object</h5>
  1196. <p>Object types represent concrete instantiations of sets of fields. The introspection types (e.g. <code>__Type</code>, <code>__Field</code>, etc) are examples of objects.</p>
  1197. <p>Fields</p>
  1198. <ul>
  1199. <li><code>kind</code> must return <code>__TypeKind.OBJECT</code>.</li>
  1200. <li><code>name</code> must return a String.</li>
  1201. <li><code>description</code> may return a String or <span class="spec-keyword">null</span>.</li>
  1202. <li><code>fields</code>: The set of fields query&#8208;able on this type.
  1203. <ul>
  1204. <li>Accepts the argument <code>includeDeprecated</code> which defaults to <span class="spec-keyword">false</span>. If <span class="spec-keyword">true</span>, deprecated fields are also returned.</li>
  1205. </ul>
  1206. </li>
  1207. <li><code>interfaces</code>: The set of interfaces that an object implements.</li>
  1208. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1209. </ul>
  1210. </section>
  1211. <section id="sec-Union">
  1212. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Union">4.2.2.3</a></span>Union</h5>
  1213. <p>Unions are an abstract types where no common fields are declared. The possible types of a union are explicitly listed out in <code>possibleTypes</code>. Types can be made parts of unions without modification of that type.</p>
  1214. <p>Fields</p>
  1215. <ul>
  1216. <li><code>kind</code> must return <code>__TypeKind.UNION</code>.</li>
  1217. <li><code>name</code> must return a String.</li>
  1218. <li><code>description</code> may return a String or <span class="spec-keyword">null</span>.</li>
  1219. <li><code>possibleTypes</code> returns the list of types that can be represented within this union. They must be object types.</li>
  1220. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1221. </ul>
  1222. </section>
  1223. <section id="sec-Interface">
  1224. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Interface">4.2.2.4</a></span>Interface</h5>
  1225. <p>Interfaces is an abstract type where there are common fields declared. Any type that implements an interface must define all the fields with names and types exactly matching. The implementations of this interface are explicitly listed out in
  1226. <code>possibleTypes</code>.</p>
  1227. <p>Fields</p>
  1228. <ul>
  1229. <li><code>kind</code> must return <code>__TypeKind.INTERFACE</code>.</li>
  1230. <li><code>name</code> must return a String.</li>
  1231. <li><code>description</code> may return a String or <span class="spec-keyword">null</span>.</li>
  1232. <li><code>fields</code>: The set of fields required by this interface.
  1233. <ul>
  1234. <li>Accepts the argument <code>includeDeprecated</code> which defaults to <span class="spec-keyword">false</span>. If <span class="spec-keyword">true</span>, deprecated fields are also returned.</li>
  1235. </ul>
  1236. </li>
  1237. <li><code>possibleTypes</code> returns the list of types that implement this interface. They must be object types.</li>
  1238. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1239. </ul>
  1240. </section>
  1241. <section id="sec-Enum">
  1242. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Enum">4.2.2.5</a></span>Enum</h5>
  1243. <p>Enums are special scalars that can only have a defined set of values.</p>
  1244. <p>Fields</p>
  1245. <ul>
  1246. <li><code>kind</code> must return <code>__TypeKind.ENUM</code>.</li>
  1247. <li><code>name</code> must return a String.</li>
  1248. <li><code>description</code> may return a String or <span class="spec-keyword">null</span>.</li>
  1249. <li><code>enumValues</code>: The list of <code>EnumValue</code>. There must be at least one and they must have unique names.
  1250. <ul>
  1251. <li>Accepts the argument <code>includeDeprecated</code> which defaults to <span class="spec-keyword">false</span>. If <span class="spec-keyword">true</span>, deprecated enum values are also returned.</li>
  1252. </ul>
  1253. </li>
  1254. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1255. </ul>
  1256. </section>
  1257. <section id="sec-Input-Object">
  1258. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Input-Object">4.2.2.6</a></span>Input Object</h5>
  1259. <p>Input objects are composite types used as inputs into queries defined as a list of named input values.</p>
  1260. <p>For example the input object <code>Point</code> could be defined as:</p><pre><code>type Point {
  1261. x: Int
  1262. y: Int
  1263. }
  1264. </code></pre>
  1265. <p>Fields</p>
  1266. <ul>
  1267. <li><code>kind</code> must return <code>__TypeKind.INPUT_OBJECT</code>.</li>
  1268. <li><code>name</code> must return a String.</li>
  1269. <li><code>description</code> may return a String or <span class="spec-keyword">null</span>.</li>
  1270. <li><code>inputFields</code>: a list of <code>InputValue</code>.</li>
  1271. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1272. </ul>
  1273. </section>
  1274. <section id="sec-List">
  1275. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-List">4.2.2.7</a></span>List</h5>
  1276. <p>Lists represent sequences of values in GraphQL. A List type is a type modifier: it wraps another type instance in the <code>ofType</code> field, which defines the type of each item in the list.</p>
  1277. <p>Fields</p>
  1278. <ul>
  1279. <li><code>kind</code> must return <code>__TypeKind.LIST</code>.</li>
  1280. <li><code>ofType</code>: Any type.</li>
  1281. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1282. </ul>
  1283. </section>
  1284. <section id="sec-Non-null">
  1285. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Non-null">4.2.2.8</a></span>Non-null</h5>
  1286. <p>GraphQL types are nullable. The value <span class="spec-keyword">null</span> is a valid response for field type.</p>
  1287. <p>A Non&#8208;null type is a type modifier: it wraps another type instance in the <code>ofType</code> field. Non&#8208;null types do not allow <span class="spec-keyword">null</span> as a response, and indicate required inputs for arguments and
  1288. input object fields.</p>
  1289. <ul>
  1290. <li><code>kind</code> must return <code>__TypeKind.NON_NULL</code>.</li>
  1291. <li><code>ofType</code>: Any type except Non&#8208;null.</li>
  1292. <li>All other fields must return <span class="spec-keyword">null</span>.</li>
  1293. </ul>
  1294. </section>
  1295. <section id="sec-Combining-List-and-Non-Null">
  1296. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Combining-List-and-Non-Null">4.2.2.9</a></span>Combining List and Non-Null</h5>
  1297. <p>List and Non&#8208;Null can compose, representing more complex types.</p>
  1298. <p>If the modified type of a List is Non&#8208;Null, then that List may not contain any <span class="spec-keyword">null</span> items.</p>
  1299. <p>If the modified type of a Non&#8208;Null is List, then <span class="spec-keyword">null</span> is not accepted, however an empty list is accepted.</p>
  1300. <p>If the modified type of a List is a List, then each item in the first List is another List of the second List&rsquo;s type.</p>
  1301. <p>A Non&#8208;Null type cannot modify another Non&#8208;Null type. </p>
  1302. </section>
  1303. </section>
  1304. </section>
  1305. </section>
  1306. <section id="sec-Validation">
  1307. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Validation">5</a></span>Validation</h2>
  1308. <p>GraphQL does not just verify if a request is syntactically correct.</p>
  1309. <p>Prior to execution, it can also verify that a request is valid within the context of a given GraphQL schema. Validation is primarily targeted at development&#8208;time tooling. Any client&#8208;side tooling should return errors and not allow the formulation
  1310. of queries known to violate the type system at a given point in time.</p>
  1311. <p>Total request validation on the server&#8208;side during execution is optional. As schemas and systems change over time existing clients may end up emitting queries that are no longer valid given the current type system. Servers (as described in the
  1312. Execution section of this spec) attempt to satisfy as much as the request as possible and continue to execute in the presence of type system errors rather than cease execution completely.</p>
  1313. <p>For this section of this schema, we will assume the following type system in order to demonstrate examples:</p><pre><code>enum DogCommand { SIT, DOWN, HEEL }
  1314. type Dog : Pet {
  1315. name: String!
  1316. nickname: String
  1317. barkVolume: Int
  1318. doesKnowCommand(dogCommand: DogCommand!) : Boolean!
  1319. isHousetrained(atOtherHomes: Boolean): Boolean!
  1320. }
  1321. interface Sentient {
  1322. name: String!
  1323. }
  1324. interface Pet {
  1325. name: String!
  1326. }
  1327. type Alien : Sentient {
  1328. name: String!
  1329. homePlanet: String
  1330. }
  1331. type Human : Sentient {
  1332. name: String!
  1333. }
  1334. type Cat : Pet {
  1335. name: String!
  1336. nickname: String
  1337. meowVolume: Int
  1338. }
  1339. union CatOrDog = Cat | Dog
  1340. union DogOrHuman = Dog | Human
  1341. union HumanOrAlien = Human | Alien
  1342. </code></pre>
  1343. <section id="sec-Validation.Fields">
  1344. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Validation.Fields">5.1</a></span>Fields</h3>
  1345. <section id="sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types">
  1346. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Field-Selections-on-Objects-Interfaces-and-Unions-Types">5.1.1</a></span>Field Selections on Objects, Interfaces, and Unions Types</h4>
  1347. <p><strong> Formal Specification </strong></p>
  1348. <ul>
  1349. <li>For each <var>selection</var> in the document.</li>
  1350. <li>Let <var>fieldName</var> be the target field of <var>selection</var></li>
  1351. <li><var>fieldName</var> must be defined on type in scope</li>
  1352. </ul>
  1353. <p><strong> Explanatory Text </strong></p>
  1354. <p>The target field of a field selection must defined on the scoped type of the selection set. There are no limitations on alias names.</p>
  1355. <p>For example the following fragment would not pass validation:</p><pre class="spec-counter-example"><code>fragment fieldNotDefined on Dog {
  1356. meowVolume
  1357. }
  1358. fragment aliasedLyingFieldTargetNotDefined on Dog {
  1359. barkVolume: kawVolume
  1360. }
  1361. </code></pre>
  1362. <p>For interfaces, direct field selection can only be done on fields. Fields of concrete implementors is not relevant to the validity of the given interface&#8208;typed selection set.</p>
  1363. <p>For example, the following is valid:</p><pre><code>fragment interfaceFieldSelection on Pet {
  1364. name
  1365. }
  1366. </code></pre>
  1367. <p>and the following is invalid:</p><pre class="spec-counter-example"><code>fragment definedOnImplementorsButNotInterface on Pet {
  1368. nickname
  1369. }
  1370. </code></pre>
  1371. <p>Because fields are not declared on unions, direct field selection on union&#8208;typed selection set. This is true even if concrete implementors of the union define the fieldName.</p>
  1372. <p>For example the following is invalid</p><pre class="spec-counter-example"><code>fragment directFieldSelectionOnUnion on CatOrDog {
  1373. directField
  1374. }
  1375. fragment definedOnImplementorsQueriedOnUnion on CatOrDog {
  1376. name
  1377. }
  1378. </code></pre></section>
  1379. <section id="sec-Field-Selection-Merging">
  1380. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Field-Selection-Merging">5.1.2</a></span>Field Selection Merging</h4>
  1381. <p><strong> Formal Specification </strong></p>
  1382. <ul>
  1383. <li>Let <var>set</var> be any selection set defined in the GraphQL document</li>
  1384. <li>Let <var>setForKey</var> be the set of selections with a given response key in <var>set</var></li>
  1385. <li>All members of <var>setForKey</var> must:
  1386. <ul>
  1387. <li>Have identical target fields</li>
  1388. <li>Have identical sets of arguments.</li>
  1389. <li>Have identical sets of directives.</li>
  1390. </ul>
  1391. </li>
  1392. </ul>
  1393. <p><strong> Explanatory Text </strong></p>
  1394. <p>Selection names are de&#8208;duplicated and merged for validation, but the target field, arguments, and directives must all be identical.</p>
  1395. <p>For human&#8208;curated GraphQL, this rules seem a bit counterintuitive since it appears to be clear developer error. However in the presence of nested fragments or machine&#8208;generated GraphQL, requiring unique selections is a burdensome limitation
  1396. on tool authors.</p>
  1397. <p>The following selections correctly merge:</p><pre><code>fragment mergeIdenticalFields on Dog {
  1398. name
  1399. name
  1400. }
  1401. fragment mergeIdenticalAliasesAndFields on Dog {
  1402. otherName: name
  1403. otherName: name
  1404. }
  1405. </code></pre>
  1406. <p>The following is not able to merge:</p><pre class="spec-counter-example"><code>fragment conflictingBecauseAlias on Dog {
  1407. name: nickname
  1408. name
  1409. }
  1410. </code></pre>
  1411. <p>Identical arguments are also merged if they have identical arguments. Both values and variables can be correctly merged.</p>
  1412. <p>For example the following correctly merge:</p><pre><code>fragment mergeIdenticalFieldsWithIdenticalArgs on Dog {
  1413. doesKnowCommand(dogCommand: SIT)
  1414. doesKnowCommand(dogCommand: SIT)
  1415. }
  1416. fragment mergeIdenticalFieldsWithIdenticalValues on Dog {
  1417. doesKnowCommand(dogCommand: $dogCommand)
  1418. doesKnowCommand(dogCommand: $dogCommand)
  1419. }
  1420. </code></pre>
  1421. <p>The following do not correctly merge:</p><pre class="spec-counter-example"><code>fragment conflictingArgsOnValues on Dog {
  1422. doesKnowCommand(dogCommand: SIT)
  1423. doesKnowCommand(dogCommand: HEEL)
  1424. }
  1425. fragment conflictingArgsValueAndVar on Dog {
  1426. doesKnowCommand(dogCommand: SIT)
  1427. doesKnowCommand(dogCommand: $dogCommand)
  1428. }
  1429. fragment conflictingArgsWithVars on Dog {
  1430. doesKnowCommand(dogCommand: $varOne)
  1431. doesKnowCommand(dogCommand: $varTwo)
  1432. }
  1433. </code></pre>
  1434. <p>The same logic applies to directives. The set of directives on each selection with the same response key in a given scope must be identical.</p>
  1435. <p>The following is valid:</p><pre><code>fragment mergeSameFieldsWithSameDirectives on Dog {
  1436. name @include(if: true)
  1437. name @include(if: true)
  1438. }
  1439. </code></pre>
  1440. <p>and the following is invalid:</p><pre class="spec-counter-example"><code>fragment conflictingDirectiveArgs on Dog {
  1441. name @include(if: true)
  1442. name @include(if: false)
  1443. }
  1444. </code></pre></section>
  1445. <section id="sec-Leaf-Field-Selections">
  1446. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Leaf-Field-Selections">5.1.3</a></span>Leaf Field Selections</h4>
  1447. <p><strong> Formal Specification </strong></p>
  1448. <ul>
  1449. <li>For each <var>selection</var> in the document</li>
  1450. <li>Let <var>selectionType</var> be the result type of <var>selection</var></li>
  1451. <li>If <var>selectionType</var> is a scalar:
  1452. <ul>
  1453. <li>The subselection set of that selection must be empty</li>
  1454. </ul>
  1455. </li>
  1456. <li>If <var>selectionType</var> is an interface, union, or object
  1457. <ul>
  1458. <li>The subselection set of that selection must NOT BE empty</li>
  1459. </ul>
  1460. </li>
  1461. </ul>
  1462. <p><strong> Explanatory Text </strong></p>
  1463. <p>Field selections on scalars are never allowed: scalars are the leaf nodes of any GraphQL query.</p>
  1464. <p>The following is valid.</p><pre><code>fragment scalarSelection: Dog {
  1465. barkVolume
  1466. }
  1467. </code></pre>
  1468. <p>The following is invalid.</p><pre class="spec-counter-example"><code>fragment scalarSelectionsNotAllowedOnBoolean : Dog {
  1469. barkVolume {
  1470. sinceWhen
  1471. }
  1472. }
  1473. </code></pre>
  1474. <p>Conversely the leaf field selections of GraphQL queries must be scalars. Leaf selections on objects, interfaces, and unions without subfields are disallowed.</p>
  1475. <p>Let&rsquo;s assume the following query root type of the schema:</p><pre><code>type QueryRoot {
  1476. human: Human
  1477. pet: Pet
  1478. catOrDog: CatOrDog
  1479. }
  1480. </code></pre>
  1481. <p>The following examples are invalid</p><pre class="spec-counter-example"><code>query directQueryOnObjectWithoutSubFields {
  1482. human
  1483. }
  1484. query directQueryOnInterfaceWithoutSubFields {
  1485. pet
  1486. }
  1487. query directQueryOnUnionWithoutSubFields {
  1488. catOrDog
  1489. }
  1490. </code></pre></section>
  1491. </section>
  1492. <section id="sec-Validation.Arguments">
  1493. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Validation.Arguments">5.2</a></span>Arguments</h3>
  1494. <p>Arguments are provided to both fields and directives. The following validation rules apply in both cases.</p>
  1495. <section id="sec-Argument-Names">
  1496. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Argument-Names">5.2.1</a></span>Argument Names</h4>
  1497. <p><strong> Formal Specification </strong></p>
  1498. <ul>
  1499. <li>For each <var>argument</var> in the document</li>
  1500. <li>Let <var>argumentName</var> be the Name of <var>argument</var>.</li>
  1501. <li>Let <var>argumentDefinition</var> be the argument definition provided by the parent field or definition named <var>argumentName</var>.</li>
  1502. <li><var>argumentDefinition</var> must exist.</li>
  1503. </ul>
  1504. <p><strong> Explanatory Text </strong></p>
  1505. <p>Every argument provided to a field or directive must be defined in the set of possible arguments of that field or directive.</p>
  1506. <p>For example the following are valid:</p><pre><code>fragment argOnRequiredArg on Dog {
  1507. doesKnowCommand(dogCommand: SIT)
  1508. }
  1509. fragment argOnOptional on Dog {
  1510. isHousetrained(atOtherHomes: true) @include(if: true)
  1511. }
  1512. </code></pre>
  1513. <p>the following is invalid since <code>command</code> is not defined on <code>DogCommand</code>.</p><pre class="spec-counter-example"><code>fragment invalidArgName on Dog {
  1514. doesKnowCommand(command: CLEAN_UP_HOUSE)
  1515. }
  1516. </code></pre>
  1517. <p>and this is also invalid as <code>unless</code> is not defined on <code>@include</code>.</p><pre class="spec-counter-example"><code>fragment invalidArgName on Dog {
  1518. isHousetrained(atOtherHomes: true) @include(unless: false)
  1519. }
  1520. </code></pre>
  1521. <p>In order to explore more complicated argument examples, let&rsquo;s add the following to our type system:</p><pre><code>type Arguments {
  1522. multipleReqs(x: Int!, y: Int!)
  1523. booleanArgField(booleanArg: Boolean)
  1524. floatArgField(floatArg: Float)
  1525. intArgField(intArg: Int)
  1526. nonNullBooleanArgField(nonNullBooleanArg: Boolean!)
  1527. }
  1528. </code></pre>
  1529. <p>Order does not matter in arguments. Therefore both the following example are valid.</p><pre><code>fragment multipleArgs on Arguments {
  1530. multipleReqs(x: 1, y: 2)
  1531. }
  1532. fragment multipleArgsReverseOrder on Arguments {
  1533. multipleReqs(y: 1, x: 2)
  1534. }
  1535. </code></pre></section>
  1536. <section id="sec-Argument-Values-Type-Correctness">
  1537. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Argument-Values-Type-Correctness">5.2.2</a></span>Argument Values Type Correctness</h4>
  1538. <section id="sec-Compatible-Values">
  1539. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Compatible-Values">5.2.2.1</a></span>Compatible Values</h5>
  1540. <p><strong> Formal Specification </strong></p>
  1541. <ul>
  1542. <li>For each <var>argument</var> in the document</li>
  1543. <li>Let <var>value</var> be the Value of <var>argument</var></li>
  1544. <li>If <var>value</var> is not a Variable
  1545. <ul>
  1546. <li>Let <var>argumentName</var> be the Name of <var>argument</var>.</li>
  1547. <li>Let <var>argumentDefinition</var> be the argument definition provided by the parent field or definition named <var>argumentName</var>.</li>
  1548. <li>Let <var>type</var> be the type expected by <var>argumentDefinition</var>.</li>
  1549. <li>The type of <var>literalArgument</var> must be coercible to <var>type</var>.</li>
  1550. </ul>
  1551. </li>
  1552. </ul>
  1553. <p><strong> Explanatory Text </strong></p>
  1554. <p>Literal values must be compatible with the type defined by the argument they are being provided to, as per the coercion rules defined in the Type System chapter.</p>
  1555. <p>For example, an Int can be coerced into a Float.</p><pre><code>fragment goodBooleanArg on Arguments {
  1556. booleanArgField(booleanArg: true)
  1557. }
  1558. fragment coercedIntIntoFloatArg on Arguments {
  1559. floatArgField(floatArg: 1)
  1560. }
  1561. </code></pre>
  1562. <p>An incoercible conversion, is string to int. Therefore, the following example is invalid.</p><pre class="spec-counter-example"><code>fragment stringIntoInt on Arguments {
  1563. intArgField(intArg: &quot;3&quot;)
  1564. }
  1565. </code></pre></section>
  1566. <section id="sec-Required-Arguments">
  1567. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Required-Arguments">5.2.2.2</a></span>Required Arguments</h5>
  1568. <ul>
  1569. <li>For each Field or Directive in the document.</li>
  1570. <li>Let <var>arguments</var> be the arguments provided by the Field or Directive.</li>
  1571. <li>Let <var>argumentDefinitions</var> be the set of argument definitions of that Field or Directive.</li>
  1572. <li>For each <var>definition</var> in <var>argumentDefinitions</var>
  1573. <ul>
  1574. <li>Let <var>type</var> be the expected type of <var>definition</var></li>
  1575. <li>If <var>type</var> is Non&#8208;Null
  1576. <ul>
  1577. <li>Let <var>argumentName</var> be the name of <var>definition</var></li>
  1578. <li>Let <var>argument</var> be the argument in <var>arguments</var> named <var>argumentName</var></li>
  1579. <li><var>argument</var> must exist.</li>
  1580. </ul>
  1581. </li>
  1582. </ul>
  1583. </li>
  1584. </ul>
  1585. <p><strong> Explanatory Text </strong></p>
  1586. <p>Arguments can be required. Arguments are required if the type of the argument is non&#8208;null. If it is not non&#8208;null, the argument is optional.</p>
  1587. <p>For example the following are valid:</p><pre><code>fragment goodBooleanArg on Arguments {
  1588. booleanArgField(booleanArg: true)
  1589. }
  1590. fragment goodNonNullArg on Arguments {
  1591. nonNullBooleanArgField(nonNullBooleanArg: true)
  1592. }
  1593. </code></pre>
  1594. <p>The argument can be omitted from a field with a nullable argument.</p>
  1595. <p>Therefore the following query is valid:</p><pre><code>fragment goodBooleanArgDefault on Arguments {
  1596. booleanArgField
  1597. }
  1598. </code></pre>
  1599. <p>but this is not valid on a non&#8208;null argument.</p><pre class="spec-counter-example"><code>fragment missingRequiredArg on Arguments {
  1600. notNullBooleanArgField
  1601. }
  1602. </code></pre></section>
  1603. </section>
  1604. </section>
  1605. <section id="sec-Validation.Fragments">
  1606. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Validation.Fragments">5.3</a></span>Fragments</h3>
  1607. <section id="sec-Fragment-Declarations">
  1608. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-Declarations">5.3.1</a></span>Fragment Declarations</h4>
  1609. <section id="sec-Fragment-Spread-Type-Existence">
  1610. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-Spread-Type-Existence">5.3.1.1</a></span>Fragment Spread Type Existence</h5>
  1611. <p><strong> Formal Specification </strong></p>
  1612. <ul>
  1613. <li>For each named spread <var>namedSpread</var> in the document</li>
  1614. <li>Let <var>fragment</var> be the target of <var>namedSpread</var></li>
  1615. <li>The target type of <var>fragment</var> must be defined in the schema</li>
  1616. </ul>
  1617. <p><strong> Explanatory Text </strong></p>
  1618. <p>Fragments must be specified on types that exist in the schema. This applies for both named and inline fragments. If they are not defined in the schema, the query does not validate.</p>
  1619. <p>For example the following fragments are valid:</p><pre><code>fragment correctType on Dog {
  1620. name
  1621. }
  1622. fragment inlineFragment on Dog {
  1623. ... on Dog {
  1624. name
  1625. }
  1626. }
  1627. </code></pre>
  1628. <p>and the following do not validate:</p><pre class="spec-counter-example"><code>fragment notOnExistingType on NotInSchema {
  1629. name
  1630. }
  1631. fragment inlineNotExistingType on Dog {
  1632. ... on NotInSchema {
  1633. name
  1634. }
  1635. }
  1636. </code></pre></section>
  1637. <section id="sec-Fragments-On-Composite-Types">
  1638. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Fragments-On-Composite-Types">5.3.1.2</a></span>Fragments On Composite Types</h5>
  1639. <p><strong> Formal Specification </strong></p>
  1640. <ul>
  1641. <li>For each <var>fragment</var> defined in the document.</li>
  1642. <li>The target type of fragment must be have kind <span class="spec-nt">UNION</span>, <span class="spec-nt">INTERFACE</span>, or <span class="spec-nt">OBJECT</span>.</li>
  1643. </ul>
  1644. <p><strong> Explanatory Text </strong></p>
  1645. <p>Fragments can only be declared on unions, interfaces, and objects. They are invalid on scalars. They can only be applied on non&#8208;leaf fields. This rule applies to both inline and named fragments.</p>
  1646. <p>The following fragment declarations are valid:</p><pre><code>fragment fragOnObject on Dog {
  1647. name
  1648. }
  1649. fragment fragOnInterface on Pet {
  1650. name
  1651. }
  1652. fragment fragOnUnion on CatOrDog {
  1653. ... on Dog {
  1654. name
  1655. }
  1656. }
  1657. </code></pre>
  1658. <p>and the following are invalid:</p><pre class="spec-counter-example"><code>fragment fragOnScalar on Int {
  1659. something
  1660. }
  1661. fragment inlineFragOnScalar on Dog {
  1662. ... on Boolean {
  1663. somethingElse
  1664. }
  1665. }
  1666. </code></pre></section>
  1667. <section id="sec-Fragments-Must-Be-Used">
  1668. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Fragments-Must-Be-Used">5.3.1.3</a></span>Fragments Must Be Used</h5>
  1669. <p><strong> Formal Specification </strong></p>
  1670. <ul>
  1671. <li>For each <var>fragment</var> defined in the document.</li>
  1672. <li><var>fragment</var> must be be the target of at least one spread in the document</li>
  1673. </ul>
  1674. <p><strong> Explanatory Text </strong></p>
  1675. <p>Defined fragments must be used within a query document.</p>
  1676. <p>For example the following is an invalid query document:</p><pre class="spec-counter-example"><code>fragment nameFragment on Dog { # unused
  1677. name
  1678. }
  1679. {
  1680. dog {
  1681. name
  1682. }
  1683. }
  1684. </code></pre></section>
  1685. </section>
  1686. <section id="sec-Fragment-Spreads">
  1687. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-Spreads">5.3.2</a></span>Fragment Spreads</h4>
  1688. <p>Field selection is also determined by spreading fragments into one another. The selection set of the target fragment is unioned with the selection set at the level at which the target fragment is referenced.</p>
  1689. <section id="sec-Fragment-spread-target-defined">
  1690. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-spread-target-defined">5.3.2.1</a></span>Fragment spread target defined</h5>
  1691. <p><strong> Formal Specification </strong></p>
  1692. <ul>
  1693. <li>For every <var>namedSpread</var> in the document.</li>
  1694. <li>Let <var>fragment</var> be the target of <var>namedSpread</var></li>
  1695. <li><var>fragment</var> must be defined in the document</li>
  1696. </ul>
  1697. <p><strong> Explanatory Text </strong></p>
  1698. <p>Named fragment spreads must refer to fragments defined within the document. If the target of a spread is not defined, this is an error:</p><pre class="spec-counter-example"><code>{
  1699. dog {
  1700. ...undefinedFragment
  1701. }
  1702. }
  1703. </code></pre></section>
  1704. <section id="sec-Fragment-spreads-must-not-form-cycles">
  1705. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-spreads-must-not-form-cycles">5.3.2.2</a></span>Fragment spreads must not form cycles</h5>
  1706. <p><strong> Formal Specification </strong></p>
  1707. <ul>
  1708. <li>For each <var>fragmentDefinition</var> in the document</li>
  1709. <li>Let <var>visited</var> be the empty set.</li>
  1710. <li><span class="spec-call">DetectCycles(<var>fragmentDefinition</var>, <var>visited</var>)</span></li>
  1711. </ul>
  1712. <p><span class="spec-call">DetectCycles(<var>fragmentDefinition</var>, <var>visited</var>)</span> :</p>
  1713. <ul>
  1714. <li>Let <var>spreads</var> be all fragment spread descendants of <var>fragmentDefinition</var></li>
  1715. <li>For each <var>spread</var> in <var>spreads</var>
  1716. <ul>
  1717. <li><var>visited</var> must not contain <var>spread</var></li>
  1718. <li>Let <var>nextVisited</var> be the set including <var>spread</var> and members of <var>visited</var></li>
  1719. <li>Let <var>nextFragmentDefinition</var> be the target of <var>spread</var></li>
  1720. <li><span class="spec-call">DetectCycles(<var>nextFragmentDefinition</var>, <var>nextVisited</var>)</span></li>
  1721. </ul>
  1722. </li>
  1723. </ul>
  1724. <p><strong> Explanatory Text </strong></p>
  1725. <p>The graph of fragment spreads must not form any cycles including spreading itself. Otherwise an operation could infinitely spread or infinitely execute on cycles in the underlying data.</p>
  1726. <p>This invalidates fragments that would result in an infinite spread:</p><pre class="spec-counter-example"><code>{
  1727. dog {
  1728. ...nameFragment
  1729. }
  1730. }
  1731. fragment nameFragment on Dog {
  1732. name
  1733. ...barkVolumeFragment
  1734. }
  1735. fragment barkVolumeFragment on Dog {
  1736. barkVolume
  1737. ...nameFragment
  1738. }
  1739. </code></pre>
  1740. <p>If the above fragments were inlined, this would result in the infinitely large:</p><pre class="spec-counter-example"><code>{
  1741. dog {
  1742. name
  1743. barkVolume
  1744. name
  1745. barkVolume
  1746. name
  1747. barkVolume
  1748. name
  1749. # forever...
  1750. }
  1751. }
  1752. </code></pre>
  1753. <p>This also invalidates fragments that would result in an infinite recursion when executed against cyclic data:</p><pre class="spec-counter-example"><code>{
  1754. dog {
  1755. ...dogFragment
  1756. }
  1757. }
  1758. fragment dogFragment on Dog {
  1759. name
  1760. owner {
  1761. ...ownerFragment
  1762. }
  1763. }
  1764. fragment ownerFragment on Dog {
  1765. name
  1766. pets {
  1767. ...dogFragment
  1768. }
  1769. }
  1770. </code></pre></section>
  1771. <section id="sec-Fragment-spread-is-possible">
  1772. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Fragment-spread-is-possible">5.3.2.3</a></span>Fragment spread is possible</h5>
  1773. <p><strong> Formal Specification </strong></p>
  1774. <ul>
  1775. <li>For each <var>spread</var> (named or inline) in defined in the document.</li>
  1776. <li>Let <var>fragment</var> be the target of <var>spread</var></li>
  1777. <li>Let <var>fragmentType</var> be the type condition of <var>fragment</var></li>
  1778. <li>Let <var>parentType</var> be the type of the selection set containing <var>spread</var></li>
  1779. <li>Let <var>applicableTypes</var> be the intersection of <span class="spec-call"><a href="#GetPossibleTypes()">GetPossibleTypes</a>(<var>fragmentType</var>)</span> and <span class="spec-call"><a href="#GetPossibleTypes()">GetPossibleTypes</a>(<var>parentType</var>)</span></li>
  1780. <li><var>applicableTypes</var> must not be empty.</li>
  1781. </ul>
  1782. <div class="spec-algo" id="GetPossibleTypes()"><span class="spec-call"><a href="#GetPossibleTypes()">GetPossibleTypes</a>(<var>type</var>)</span>
  1783. <ol>
  1784. <li>If <var>type</var> is an object type, return a set containing <var>type</var></li>
  1785. <li>If <var>type</var> is an interface type, return the set of types implementing <var>type</var></li>
  1786. <li>If <var>type</var> is a union type, return the set of possible types of <var>type</var></li>
  1787. </ol>
  1788. </div>
  1789. <p><strong> Explanatory Text </strong></p>
  1790. <p>Fragments are declared on a type and will only apply when the runtime object type matches the type condition. They also are spread within the context of a parent type. A fragment spread is only valid if its type condition could ever apply within
  1791. the parent type.</p>
  1792. <p>and the following valid fragments:</p>
  1793. <section id="sec-Object-Spreads-In-Object-Scope">
  1794. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Object-Spreads-In-Object-Scope">5.3.2.3.1</a></span>Object Spreads In Object Scope</h6>
  1795. <p>In the scope of a object type, the only valid object type fragment spread is one that applies to the same type that is in scope.</p>
  1796. <p>For example</p><pre><code>fragment dogFragment on Dog {
  1797. ... on Dog {
  1798. barkVolume
  1799. }
  1800. }
  1801. </code></pre>
  1802. <p>and the following is invalid</p><pre class="spec-counter-example"><code>fragment catInDogFragmentInvalid on Dog {
  1803. ... on Cat {
  1804. meowVolume
  1805. }
  1806. }
  1807. </code></pre></section>
  1808. <section id="sec-Abstract-Spreads-in-Object-Scope">
  1809. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Abstract-Spreads-in-Object-Scope">5.3.2.3.2</a></span>Abstract Spreads in Object Scope</h6>
  1810. <p>In scope of an object type, unions or interface spreads can be used if the object type implements the interface or is a member of the union.</p>
  1811. <p>For example</p><pre><code>fragment petNameFragment on Pet {
  1812. name
  1813. }
  1814. fragment interfaceWithinObjectFragment on Dog {
  1815. ...petNameFragment
  1816. }
  1817. </code></pre>
  1818. <p>is valid because <span class="spec-nt">Dog</span> implements Pet.</p>
  1819. <p>Likewise</p><pre><code>fragment catOrDogNameFragment on CatOrDog {
  1820. ... on Cat {
  1821. meowVolume
  1822. }
  1823. }
  1824. fragment unionWithObjectFragment on Dog {
  1825. ...CatOrDogFragment
  1826. }
  1827. </code></pre>
  1828. <p>is valid because <span class="spec-nt">Dog</span> is a member of the <span class="spec-nt">CatOrDog</span> union. It is worth noting that if one inspected the contents of the <span class="spec-nt">CatOrDogNameFragment</span> you could note
  1829. that the no valid results would ever be returned. However we do not specify this as invalid because we only consider the fragment declaration, not its body.</p>
  1830. </section>
  1831. <section id="sec-Object-Spreads-In-Abstract-Scope">
  1832. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Object-Spreads-In-Abstract-Scope">5.3.2.3.3</a></span>Object Spreads In Abstract Scope</h6>
  1833. <p>Union or interface spreads can be used within the context of an object type fragment, but only if the object type is one of the possible types of the that interface or union.</p>
  1834. <p>For example, the following fragments are valid:</p><pre><code>fragment petFragment on Pet {
  1835. name
  1836. ... on Dog {
  1837. barkVolume
  1838. }
  1839. }
  1840. fragment catOrDogFragment on CatOrDog {
  1841. ... on Cat {
  1842. meowVolume
  1843. }
  1844. }
  1845. </code></pre>
  1846. <p><var>petFragment</var> is valid because <span class="spec-nt">Dog</span> implements the interface <span class="spec-nt">Pet</span>. <var>catOrDogFragment</var> is valid because <span class="spec-nt">Cat</span> is a member of the <span class="spec-nt">CatOrDog</span> union.</p>
  1847. <p>By contrast the following fragments are invalid:</p><pre class="spec-counter-example"><code>fragment sentientFragment on Sentient {
  1848. ... on Dog {
  1849. barkVolume
  1850. }
  1851. }
  1852. fragment humanOrAlienFragment on HumanOrAlien {
  1853. ... on Cat {
  1854. meowVolume
  1855. }
  1856. }
  1857. </code></pre>
  1858. <p><span class="spec-nt">Dog</span> does not implement the interface <span class="spec-nt">Sentient</span> and therefore <var>sentientFragment</var> can never return meaningful results. Therefore the fragment is invalid. Likewise <span class="spec-nt">Cat</span> is not a member of the union <span class="spec-nt">HumanOrAlien</span>, and it can also never return meaningful results, making it invalid.</p>
  1859. </section>
  1860. <section id="sec-Abstract-Spreads-in-Abstract-Scope">
  1861. <h6><span class="spec-secnum" title="link to this section"><a href="#sec-Abstract-Spreads-in-Abstract-Scope">5.3.2.3.4</a></span>Abstract Spreads in Abstract Scope</h6>
  1862. <p>Union or interfaces fragments can be used within each other. As long as there exists at least <em>one</em> object type that exists in the intersection of the possible types of the scope and the spread, the spread is considered valid.</p>
  1863. <p>So for example</p><pre><code>fragment unionWithInterface on Pet {
  1864. ...dogOrHumanFragment
  1865. }
  1866. fragment dogOrHumanFragment on DogOrHuman {
  1867. ... on Dog {
  1868. barkVolume
  1869. }
  1870. }
  1871. </code></pre>
  1872. <p>is consider valid because <span class="spec-nt">Dog</span> implements interface <span class="spec-nt">Pet</span> and is a member of <span class="spec-nt">DogOrHuman</span>.</p>
  1873. <p>However</p><pre class="spec-counter-example"><code>fragment nonIntersectingInterfaces on Pet {
  1874. ...sentientFragment
  1875. }
  1876. fragment sentientFragment on Sentient {
  1877. name
  1878. }
  1879. </code></pre>
  1880. <p>is not valid because there exists no type that implements both <span class="spec-nt">Pet</span> and <span class="spec-nt">Sentient</span>.</p>
  1881. </section>
  1882. </section>
  1883. </section>
  1884. </section>
  1885. <section id="sec-Validation.Directives">
  1886. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Validation.Directives">5.4</a></span>Directives</h3>
  1887. <section id="sec-Directives-Are-Defined">
  1888. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Directives-Are-Defined">5.4.1</a></span>Directives Are Defined</h4>
  1889. <p><strong> Formal Specification </strong></p>
  1890. <ul>
  1891. <li>For every <var>directive</var> in a document.</li>
  1892. <li>Let <var>directiveName</var> be the name of <var>directive</var>.</li>
  1893. <li>Let <var>directiveDefinition</var> be the directive named <var>directiveName</var>.</li>
  1894. <li><var>directiveDefinition</var> must exist.</li>
  1895. </ul>
  1896. <p><strong> Explanatory Text </strong></p>
  1897. <p>GraphQL servers define what directives they support. For each usage of a directive, the directive must be available on that server.</p>
  1898. </section>
  1899. </section>
  1900. <section id="sec-Validation.Operations">
  1901. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Validation.Operations">5.5</a></span>Operations</h3>
  1902. <section id="sec-Validation.Operations.Variables">
  1903. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Validation.Operations.Variables">5.5.1</a></span>Variables</h4>
  1904. <section id="sec-Variable-Default-Values-Are-Correctly-Typed">
  1905. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Variable-Default-Values-Are-Correctly-Typed">5.5.1.1</a></span>Variable Default Values Are Correctly Typed</h5>
  1906. <p><strong> Formal Specification </strong></p>
  1907. <ul>
  1908. <li>For every <var>operation</var> in a document</li>
  1909. <li>For every <var>variable</var> on each <var>operation</var>
  1910. <ul>
  1911. <li>Let <var>variableType</var> be the type of <var>variable</var></li>
  1912. <li>If <var>variableType</var> is non&#8208;null it cannot have a default value</li>
  1913. <li>If <var>variable</var> has a default value it must be of the same types or able to be coerced to <var>variableType</var></li>
  1914. </ul>
  1915. </li>
  1916. </ul>
  1917. <p><strong> Explanatory Text </strong></p>
  1918. <p>Variable defined by operations are allowed to define default values if the type of that variable not non&#8208;null.</p>
  1919. <p>For example the following query will pass validation.</p><pre><code>query houseTrainedQuery($atOtherHomes: Boolean = true) {
  1920. dog {
  1921. isHousetrained(atOtherHomes: $atOtherHomes)
  1922. }
  1923. }
  1924. </code></pre>
  1925. <p>However if the variable is defined as non&#8208;null, default values are unreachable. Therefore queries such as the following fail validation</p><pre class="spec-counter-example"><code>query houseTrainedQuery($atOtherHomes: Boolean! = true) {
  1926. dog {
  1927. isHousetrained(atOtherHomes: $atOtherHomes)
  1928. }
  1929. }
  1930. </code></pre>
  1931. <p>Default values must be compatible with the types of variables. Types much match or they must be coercible to the type.</p>
  1932. <p>Non&#8208;matching types fail, such as in the following example:</p><pre class="spec-counter-example"><code>query houseTrainedQuery($atOtherHomes: Boolean = &quot;true&quot;) {
  1933. dog {
  1934. isHousetrained(atOtherHomes: $atOtherHomes)
  1935. }
  1936. }
  1937. </code></pre>
  1938. <p>However if a type is coercible the query will pass validation.</p>
  1939. <p>For example:</p><pre><code>query intToFloatQuery($floatVar: Float = 1) {
  1940. arguments {
  1941. floatArgField(floatArg: $floatVar)
  1942. }
  1943. }
  1944. </code></pre></section>
  1945. <section id="sec-Variables-Are-Input-Types">
  1946. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Variables-Are-Input-Types">5.5.1.2</a></span>Variables Are Input Types</h5>
  1947. <p><strong> Formal Specification </strong></p>
  1948. <ul>
  1949. <li>For every <var>operation</var> in a <var>document</var></li>
  1950. <li>For every <var>variable</var> on each <var>operation</var>
  1951. <ul>
  1952. <li>Let <var>variableType</var> be the type of <var>variable</var></li>
  1953. <li><var>variableType</var> must of kind <span class="spec-nt">SCALAR</span>, <span class="spec-nt">ENUM</span> or <span class="spec-nt">INPUT_OBJECT</span></li>
  1954. </ul>
  1955. </li>
  1956. </ul>
  1957. <p><strong> Explanatory Text </strong></p>
  1958. <p>Variables can only be scalars, enums, input objects, or lists and non&#8208;null variants of those types. These are known as input types. Object, unions, and interfaces cannot be used as inputs.</p>
  1959. <p>The following queries are valid:</p><pre><code>query takesBoolean($atOtherHomes: Boolean) {
  1960. # ...
  1961. }
  1962. query takesComplexInput($complexInput: ComplexInput) {
  1963. # ...
  1964. }
  1965. query TakesListOfBooleanBang($booleans: [Boolean!]) {
  1966. # ...
  1967. }
  1968. </code></pre>
  1969. <p>The following queries are invalid:</p><pre class="spec-counter-example"><code>query takesCat($cat: Cat) {
  1970. # ...
  1971. }
  1972. query takesDogBang($dog: Dog!) {
  1973. # ...
  1974. }
  1975. query takesListOfPet($pets: [Pet]) {
  1976. # ...
  1977. }
  1978. query takesCatOrDog($catOrDog: CatOrDog) {
  1979. # ...
  1980. }
  1981. </code></pre></section>
  1982. <section id="sec-All-Variable-Uses-Defined">
  1983. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-All-Variable-Uses-Defined">5.5.1.3</a></span>All Variable Uses Defined</h5>
  1984. <p><strong> Formal Specification </strong></p>
  1985. <ul>
  1986. <li>For each <var>operation</var> in a document
  1987. <ul>
  1988. <li>For each <var>variableUsage</var> in scope, variable must be operation&rsquo;s variable list.</li>
  1989. <li>Let <var>fragments</var> be every fragment reference by that operation transitively</li>
  1990. <li>For each <var>fragment</var> in <var>fragments</var>
  1991. <ul>
  1992. <li>For each <var>variableUsage</var> in scope of <var>fragment</var>, variable must be <var>operation</var>&lsquo;s variable list.</li>
  1993. </ul>
  1994. </li>
  1995. </ul>
  1996. </li>
  1997. </ul>
  1998. <p><strong> Explanatory Text </strong></p>
  1999. <p>Variables are scoped on a per&#8208;operation basis. That means that any variable used within the context of a operation must be defined at the top level of that operation</p>
  2000. <p>For example:</p><pre><code>query variableIsDefined($atOtherHomes: Boolean) {
  2001. dog {
  2002. isHousetrained(atOtherHomes: $booleanArg)
  2003. }
  2004. }
  2005. </code></pre>
  2006. <p>is valid. $<var>atOtherHomes</var> is defined by the operation.</p>
  2007. <p>By contrast the following query is invalid:</p><pre class="spec-counter-example"><code>query variableIsNotDefined {
  2008. dog {
  2009. isHousetrained(atOtherHomes: $atOtherHomes)
  2010. }
  2011. }
  2012. </code></pre>
  2013. <p>$<var>atOtherHomes</var> is not defined by the operation.</p>
  2014. <p>Fragments complicate this rule. Any fragment transitively included by an operation has access to the variables defined by that operation. Fragments can appear within multiple operations and therefore variable usages must correspond to variable
  2015. definitions in all of those operations.</p>
  2016. <p>For example the following is valid:</p><pre><code>query variableIsDefinedUsedInSingleFragment($atOtherHomes: Boolean) {
  2017. dog {
  2018. ...isHousetrainedFragment
  2019. }
  2020. }
  2021. fragment isHousetrainedFragment on Dog {
  2022. isHousetrained(atOtherHomes: $atOtherHomes}
  2023. }
  2024. </code></pre>
  2025. <p>since <var>isHousetrainedFragment</var> is used within the context of the operation <var>variableIsDefinedUsedInSingleFragment</var> and the variable is defined by that operation.</p>
  2026. <p>On the contrary is a fragment is included within an operation that does not define a referenced variable, this is a validation error.</p><pre class="spec-counter-example"><code>query variableIsNotDefinedUsedInSingleFragment {
  2027. dog {
  2028. ...isHousetrainedFragment
  2029. }
  2030. }
  2031. fragment isHousetrainedFragment on Dog {
  2032. isHousetrained(atOtherHomes: $atOtherHomes}
  2033. }
  2034. </code></pre>
  2035. <p>This applies transitively as well, so the following also fails:</p><pre class="spec-counter-example"><code>query variableIsNotDefinedUsedInNestedFragment {
  2036. dog {
  2037. ...outerHousetrainedFragment
  2038. }
  2039. }
  2040. fragment outerHousetrainedFragment on Dog {
  2041. ...isHousetrainedFragment
  2042. }
  2043. fragment isHousetrainedFragment on Dog {
  2044. isHousetrained(atOtherHomes: $atOtherHomes}
  2045. }
  2046. </code></pre>
  2047. <p>Variables must be defined in all operations in which a fragment is used.</p><pre><code>query housetrainedQueryOne($atOtherHomes: Boolean) {
  2048. dog {
  2049. ...isHousetrainedFragment
  2050. }
  2051. }
  2052. query housetrainedQueryTwo($atOtherHomes: Boolean) {
  2053. dog {
  2054. ...isHousetrainedFragment
  2055. }
  2056. }
  2057. fragment isHousetrainedFragment on Dog {
  2058. isHousetrained(atOtherHomes: $atOtherHomes}
  2059. }
  2060. </code></pre>
  2061. <p>However the following does not validate:</p><pre class="spec-counter-example"><code>query housetrainedQueryOne($atOtherHomes: Boolean) {
  2062. dog {
  2063. ...isHousetrainedFragment
  2064. }
  2065. }
  2066. query housetrainedQueryTwoNotDefined {
  2067. dog {
  2068. ...isHousetrainedFragment
  2069. }
  2070. }
  2071. fragment isHousetrainedFragment on Dog {
  2072. isHousetrained(atOtherHomes: $atOtherHomes)
  2073. }
  2074. </code></pre>
  2075. <p>This is because <var>housetrainedQueryTwoNotDefined</var> does not define a variable $<var>atOtherHomes</var> but that variable is used by <var>isHousetrainedFragment</var> which is included in that operation.</p>
  2076. </section>
  2077. <section id="sec-All-Variables-Used">
  2078. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-All-Variables-Used">5.5.1.4</a></span>All Variables Used</h5>
  2079. <p><strong> Formal Specification </strong></p>
  2080. <ul>
  2081. <li>For every <var>operation</var> in the document.</li>
  2082. <li>Let <var>variables</var> be the variables defined by that <var>operation</var></li>
  2083. <li>Each <var>variable</var> in <var>variables</var> must be used at least once in either the operation scope itself or any fragment transitively referenced by that operation.</li>
  2084. </ul>
  2085. <p><strong> Explanatory Text </strong></p>
  2086. <p>All variables defined by an operation must be used in that operation or a fragment transitively included by that operation. Unused variables cause a validation error.</p>
  2087. <p>For example the following is invalid:</p><pre class="spec-counter-example"><code>query variableUnused($atOtherHomes: Boolean) {
  2088. dog {
  2089. isHousetrained
  2090. }
  2091. }
  2092. </code></pre>
  2093. <p>because $<var>atOtherHomes</var> in not referenced.</p>
  2094. <p>These rules apply to transitive fragment spreads as well:</p><pre><code>query variableUsedInFragment($atOtherHomes: Boolean) {
  2095. dog {
  2096. ...isHousetrainedFragment
  2097. }
  2098. }
  2099. fragment isHousetrainedFragment on Dog {
  2100. isHousetrained(atOtherHomes: $atOtherHomes)
  2101. }
  2102. </code></pre>
  2103. <p>The above is valid since $<var>atOtherHomes</var> is used in <var>isHousetrainedFragment</var> which is included by <var>variableUsedInFragment</var>.</p>
  2104. <p>If that fragment did not have a reference to $<var>atOtherHomes</var> it would be not valid:</p><pre class="spec-counter-example"><code>query variableNotUsedWithinFragment($atOtherHomes: Boolean) {
  2105. ...isHousetrainedWithoutVariableFragment
  2106. }
  2107. fragment isHousetrainedWithoutVariableFragment on Dog {
  2108. isHousetrained
  2109. }
  2110. </code></pre>
  2111. <p>All operations in a document must use all of their variables.</p>
  2112. <p>As a result, the following document does not validate.</p><pre class="spec-counter-example"><code>query queryWithUsedVar($atOtherHomes: Boolean) {
  2113. dog {
  2114. ...isHousetrainedFragment
  2115. }
  2116. }
  2117. query queryWithExtraVar($atOtherHomes: Boolean, $extra: Int) {
  2118. dog {
  2119. ...isHousetrainedFragment
  2120. }
  2121. }
  2122. fragment isHousetrainedFragment on Dog {
  2123. isHousetrained(atOtherHomes: $atOtherHomes)
  2124. }
  2125. </code></pre>
  2126. <p>This document is not valid because <var>queryWithExtraVar</var> defines an extraneous variable.</p>
  2127. </section>
  2128. <section id="sec-All-Variable-Usages-are-Allowed">
  2129. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-All-Variable-Usages-are-Allowed">5.5.1.5</a></span>All Variable Usages are Allowed</h5>
  2130. <p><strong> Formal Specification </strong></p>
  2131. <ul>
  2132. <li>For each <var>operation</var> in <var>document</var></li>
  2133. <li>Let <var>variableUsages</var> be all usages transitively included in the <var>operation</var></li>
  2134. <li>For each <var>variableUsage</var> in <var>variableUsages</var>
  2135. <ul>
  2136. <li>Let <var>variableType</var> be the type of variable definition in the operation</li>
  2137. <li>Let <var>argumentType</var> be the type of the argument the variable is passed to.</li>
  2138. <li>Let <var>hasDefault</var> be true if the variable definition defines a default.</li>
  2139. <li>AreTypesCompatible(<var>argumentType</var>, <var>variableType</var>, <var>hasDefault</var>) must be true</li>
  2140. </ul>
  2141. </li>
  2142. <li>AreTypesCompatible(<var>argumentType</var>, <var>variableType</var>, <var>hasDefault</var>):
  2143. <ul>
  2144. <li>If <var>hasDefault</var> is true, treat the <var>variableType</var> as non&#8208;null.</li>
  2145. <li>If inner type of <var>argumentType</var> and <var>variableType</var> be different, return false</li>
  2146. <li>If <var>argumentType</var> and <var>variableType</var> have different list dimensions, return false</li>
  2147. <li>If any list level of <var>variableType</var> is not non&#8208;null, and the corresponding level in <var>argument</var> is non&#8208;null, the types are not compatible.</li>
  2148. </ul>
  2149. </li>
  2150. </ul>
  2151. <p><strong> Explanatory Text </strong></p>
  2152. <p>Variable usages must be compatible with the arguments they are passed to.</p>
  2153. <p>Validation failures occur when variables are used in the context of types that are complete mismatches, or if a nullable type in a variable is passed to a not&#8208;null argument type.</p>
  2154. <p>Types must match:</p><pre class="spec-counter-example"><code>query intCannotGoIntoBoolean($intArg: Int) {
  2155. arguments {
  2156. booleanArgField(booleanArg: $intArg)
  2157. }
  2158. }
  2159. </code></pre>
  2160. <p>$<var>intArg</var> typed as <span class="spec-nt">Int</span> cannot be used as a argument to <var>booleanArg</var>, typed as <span class="spec-nt">Boolean</span>.</p>
  2161. <p>List cardinality must also be the same. For example, lists cannot be passed into singular values.</p><pre class="spec-counter-example"><code>query booleanListCannotGoIntoBoolean($booleanListArg: [Boolean]) {
  2162. arguments {
  2163. booleanArgField(booleanArg: $booleanListArg)
  2164. }
  2165. }
  2166. </code></pre>
  2167. <p>Nullability must also be respected. In general a nullable variable cannot be passed to a non&#8208;null argument.</p><pre class="spec-counter-example"><code>query booleanArgQuery($booleanArg: Boolean) {
  2168. arguments {
  2169. nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
  2170. }
  2171. }
  2172. </code></pre>
  2173. <p>A notable exception is when default arguments are provided. They are, in effect, treated as non&#8208;nulls.</p><pre><code>query booleanArgQueryWithDefault($booleanArg: Boolean = true) {
  2174. arguments {
  2175. nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
  2176. }
  2177. }
  2178. </code></pre>
  2179. <p>For list types, the same rules around nullability apply to both outer types and inner types. A nullable list cannot be passed to a non&#8208;null list, and a lists of nullable values cannot be passed to a list of non&#8208;null values.</p><pre><code>query nonNullListToList($nonNullBooleanList: ![Boolean]) {
  2180. arguments {
  2181. booleanListArgField(booleanListArg: $nonNullBooleanList)
  2182. }
  2183. }
  2184. </code></pre>
  2185. <p>However a nullable list could not be passed to a non&#8208;null list.</p><pre class="spec-counter-example"><code>query listToNonNullList($booleanList: [Boolean]) {
  2186. arguments {
  2187. nonNullBooleanListField(nonNullBooleanListArg: $booleanList)
  2188. }
  2189. }
  2190. </code></pre>
  2191. <p>This would fail validation because a <code>[T]</code> cannot be passed to a <code>[T]!</code>.</p>
  2192. <p>Similarly a <code>[T]</code> cannot be passed to a <code>[T!]</code>. </p>
  2193. </section>
  2194. </section>
  2195. </section>
  2196. </section>
  2197. <section id="sec-Execution">
  2198. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Execution">6</a></span>Execution</h2>
  2199. <p>This section describes how GraphQL generates a response from a request.</p>
  2200. <section id="sec-Evaluating-requests">
  2201. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Evaluating-requests">6.1</a></span>Evaluating requests</h3>
  2202. <p>To evaluate a request, the executor must have a parsed <code>Document</code> (as defined in the &ldquo;Query Language&rdquo; part of this spec) and a selected operation name to run.</p>
  2203. <p>The executor should find the <code>Operation</code> in the <code>Document</code> with the given operation name. If no such operation exists, the executor should throw an error. If the operation is found, then the result of evaluating the request
  2204. should be the result of evaluating the operation according to the &ldquo;Evaluating operations&rdquo; section.</p>
  2205. </section>
  2206. <section id="sec-Evaluating-operations">
  2207. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Evaluating-operations">6.2</a></span>Evaluating operations</h3>
  2208. <p>The type system, as described in the &ldquo;Type System&rdquo; part of the spec, must provide a &ldquo;Query Root&rdquo; and a &ldquo;Mutation Root&rdquo; object.</p>
  2209. <p>If the operation is a mutation, the result of the operation is the result of evaluating the mutation&rsquo;s top level selection set on the &ldquo;Mutation Root&rdquo; object. This selection set should be evaluated serially.</p>
  2210. <p>If the operation is a query, the result of the operation is the result of evaluating the query&rsquo;s top level selection set on the &ldquo;Query Root&rdquo; object.</p>
  2211. </section>
  2212. <section id="sec-Evaluating-selection-sets">
  2213. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Evaluating-selection-sets">6.3</a></span>Evaluating selection sets</h3>
  2214. <p>To evaluate a selection set, the executor needs to know the object on which it is evaluating the set and whether it is being evaluated serially.</p>
  2215. <p>If the selection set is being evaluated on the <code>null</code> object, then the result of evaluating the selection set is <code>null</code>.</p>
  2216. <p>Otherwise, the selection set is turned into a grouped field set; each entry in the grouped field set is a list of fields that share a responseKey.</p>
  2217. <p>The selection set is converted to a grouped field set by calling <code>CollectFields</code>, initializing <code>visitedFragments</code> to an empty list.</p>
  2218. <div class="spec-algo" id="CollectFields()"><span class="spec-call"><a href="#CollectFields()">CollectFields</a>(<var>objectType</var>, <var>selectionSet</var>, <var>visitedFragments</var>)</span>
  2219. <ol>
  2220. <li>Initialize <var>groupedFields</var> to an empty list of lists.</li>
  2221. <li>For each <var>selection</var> in <var>selectionSet</var>;
  2222. <ol>
  2223. <li>If <var>selection</var> provides the directive <code>@skip</code>, let <var>skipDirective</var> be that directive.
  2224. <ol>
  2225. <li>If <var>skipDirective</var>&lsquo;s <var>if</var> argument is <span class="spec-keyword">true</span>, continue with the next <var>selection</var> in <var>selectionSet</var>.</li>
  2226. </ol>
  2227. </li>
  2228. <li>If <var>selection</var> provides the directive <code>@include</code>, let <var>includeDirective</var> be that directive.
  2229. <ol>
  2230. <li>If <var>includeDirective</var>&lsquo;s <var>if</var> argument is <span class="spec-keyword">false</span>, continue with the next <var>selection</var> in <var>selectionSet</var>.</li>
  2231. </ol>
  2232. </li>
  2233. <li>If <var>selection</var> is a Field:
  2234. <ol>
  2235. <li>Let <var>responseKey</var> be the response key of <var>selection</var>.</li>
  2236. <li>Let <var>groupForResponseKey</var> be the list in <var>groupedFields</var> for <var>responseKey</var>; if no such list exists, create it as an empty list.</li>
  2237. <li>Append <var>selection</var> to the <var>groupForResponseKey</var>.</li>
  2238. </ol>
  2239. </li>
  2240. <li>If <var>selection</var> is a FragmentSpread:
  2241. <ol>
  2242. <li>Let <var>fragmentSpreadName</var> be the name of <var>selection</var>.</li>
  2243. <li>If <var>fragmentSpreadName</var> is in <var>visitedFragments</var>, continue with the next <var>selection</var> in <var>selectionSet</var>.</li>
  2244. <li>Add <var>fragmentSpreadName</var> to <var>visitedFragments</var>.</li>
  2245. <li>Let <var>fragment</var> be the Fragment in the current Document whose name is <var>fragmentSpreadName</var>.</li>
  2246. <li>If no such <var>fragment</var> exists, continue with the next <var>selection</var> in <var>selectionSet</var>.</li>
  2247. <li>Let <var>fragmentType</var> be the type condition on <var>fragment</var>.</li>
  2248. <li>If <span class="spec-call"><a href="#doesFragmentTypeApply()">doesFragmentTypeApply</a>(<var>objectType</var>, <var>fragmentType</var>)</span> is false, continue with the next <var>selection</var> in <var>selectionSet</var>.</li>
  2249. <li>Let <var>fragmentSelectionSet</var> be the top&#8208;level selection set of <var>fragment</var>.</li>
  2250. <li>Let <var>fragmentGroupedFields</var> be the result of calling <span class="spec-call"><a href="#CollectFields()">CollectFields</a>(<var>objectType</var>, <var>fragmentSelectionSet</var>)</span>.</li>
  2251. <li>For each <var>fragmentGroup</var> in <var>fragmentGroupedFields</var>:
  2252. <ol>
  2253. <li>Let <var>responseKey</var> be the response key shared by all fields in <var>fragmentGroup</var></li>
  2254. <li>Let <var>groupForResponseKey</var> be the list in <var>groupedFields</var> for <var>responseKey</var>; if no such list exists, create it as an empty list.</li>
  2255. <li>Append all items in <var>fragmentGroup</var> to <var>groupForResponseKey</var>.</li>
  2256. </ol>
  2257. </li>
  2258. </ol>
  2259. </li>
  2260. <li>If <var>selection</var> is an inline fragment:
  2261. <ol>
  2262. <li>Let <var>fragmentType</var> be the type condition on <var>selection</var>.</li>
  2263. <li>If <span class="spec-call"><a href="#doesFragmentTypeApply()">doesFragmentTypeApply</a>(<var>objectType</var>, <var>fragmentType</var>)</span> is false, continue with the next <var>selection</var> in <var>selectionSet</var>.</li>
  2264. <li>Let <var>fragmentSelectionSet</var> be the top&#8208;level selection set of <var>selection</var>.</li>
  2265. <li>Let <var>fragmentGroupedFields</var> be the result of calling <span class="spec-call"><a href="#CollectFields()">CollectFields</a>(<var>objectType</var>, <var>fragmentSelectionSet</var>)</span>.</li>
  2266. <li>For each <var>fragmentGroup</var> in <var>fragmentGroupedFields</var>:
  2267. <ol>
  2268. <li>Let <var>responseKey</var> be the response key shared by all fields in <var>fragmentGroup</var></li>
  2269. <li>Let <var>groupForResponseKey</var> be the list in <var>groupedFields</var> for <var>responseKey</var>; if no such list exists, create it as an empty list.</li>
  2270. <li>Append all items in <var>fragmentGroup</var> to <var>groupForResponseKey</var>.</li>
  2271. </ol>
  2272. </li>
  2273. </ol>
  2274. </li>
  2275. </ol>
  2276. </li>
  2277. <li>Return <var>groupedFields</var>.</li>
  2278. </ol>
  2279. </div>
  2280. <div class="spec-algo" id="doesFragmentTypeApply()"><span class="spec-call"><a href="#doesFragmentTypeApply()">doesFragmentTypeApply</a>(<var>objectType</var>, <var>fragmentType</var>)</span>
  2281. <ol>
  2282. <li>If <var>fragmentType</var> is an Object Type, return <span class="spec-keyword">true</span> if <var>objectType</var> is <var>fragmentType</var>, otherwise return <span class="spec-keyword">false</span>.</li>
  2283. <li>If <var>fragmentType</var> is an Interface Type, return <span class="spec-keyword">true</span> if <var>objectType</var> is an implementation of <var>fragmentType</var>, otherwise return <span class="spec-keyword">false</span>.</li>
  2284. <li>If <var>fragmentType</var> is a Union, return <span class="spec-keyword">true</span> if <var>objectType</var> is a possible type of <var>fragmentType</var>, otherwise return <span class="spec-keyword">false</span>.</li>
  2285. </ol>
  2286. </div>
  2287. <p>The result of evaluating the selection set is the result of evaluating the corresponding grouped field set. The corresponding grouped field set should be evaluated serially if the selection set is being evaluated serially, otherwise it should be
  2288. evaluated normally.</p>
  2289. </section>
  2290. <section id="sec-Evaluating-a-grouped-field-set">
  2291. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Evaluating-a-grouped-field-set">6.4</a></span>Evaluating a grouped field set</h3>
  2292. <p>The result of evaluating a grouped field set will be an unordered map. There will be an entry in this map for every item in the grouped field set.</p>
  2293. <section id="sec-Field-entries">
  2294. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Field-entries">6.4.1</a></span>Field entries</h4>
  2295. <p>Each item in the grouped field set can potentially create an entry in the result map. That entry in the result map is the result is the result of calling <code>GetFieldEntry</code> on the corresponding item in the grouped field set. <code>GetFieldEntry</code> can return <code>null</code>, which indicates that there should be no entry in the result map for this item. Note that this is distinct from returning an entry with a string key and a null value, which indicates that an entry in the result should
  2296. be added for that key, and its value should be null.</p>
  2297. <p><code>GetFieldEntry</code> assumes the existence of two functions that are not defined in this section of the spec. It is expected that the type system provides these methods:</p>
  2298. <ul>
  2299. <li><code>ResolveFieldOnObject</code>, which takes an object type, a field, and an object, and returns the result of resolving that field on the object.</li>
  2300. <li><code>GetFieldTypeFromObjectType</code>, which takes an object type and a field, and returns that field&rsquo;s type on the object type, or <code>null</code> if the field is not valid on the object type.</li>
  2301. </ul>
  2302. <div class="spec-algo" id="GetFieldEntry()"><span class="spec-call"><a href="#GetFieldEntry()">GetFieldEntry</a>(<var>objectType</var>, <var>object</var>, <var>fields</var>)</span>
  2303. <ol>
  2304. <li>Let <var>firstField</var> be the first entry in the ordered list <var>fields</var>. Note that <var>fields</var> is never empty, as the entry in the grouped field set would not exist if there were no fields.</li>
  2305. <li>Let <var>responseKey</var> be the response key of <var>firstField</var>.</li>
  2306. <li>Let <var>fieldType</var> be the result of calling <span class="spec-call"><a href="#GetFieldTypeFromObjectType()">GetFieldTypeFromObjectType</a>(<var>objectType</var>, <var>firstField</var>)</span>.</li>
  2307. <li>If <var>fieldType</var> is <span class="spec-keyword">null</span>, return <span class="spec-keyword">null</span>, indicating that no entry exists in the result map.</li>
  2308. <li>Let <var>resolvedObject</var> be <span class="spec-call"><a href="#ResolveFieldOnObject()">ResolveFieldOnObject</a>(<var>objectType</var>, <var>object</var>, <var>fieldEntry</var>)</span>.</li>
  2309. <li>If <var>resolvedObject</var> is <span class="spec-keyword">null</span>, return <span class="spec-call">tuple(<var>responseKey</var>, <span class="spec-keyword">null</span>)</span>, indicating that an entry exists in the result map whose value
  2310. is <code>null</code>.</li>
  2311. <li>Let <var>subSelectionSet</var> be the result of calling <span class="spec-call"><a href="#MergeSelectionSets()">MergeSelectionSets</a>(<var>fields</var>)</span>.</li>
  2312. <li>Let <var>responseValue</var> be the result of calling <span class="spec-call"><a href="#CompleteValue()">CompleteValue</a>(<var>fieldType</var>, <var>resolvedObject</var>, <var>subSelectionSet</var>)</span>.</li>
  2313. <li>Return <span class="spec-call">tuple(<var>responseKey</var>, <var>responseValue</var>)</span>.</li>
  2314. </ol>
  2315. </div>
  2316. <div class="spec-algo" id="GetFieldTypeFromObjectType()"><span class="spec-call"><a href="#GetFieldTypeFromObjectType()">GetFieldTypeFromObjectType</a>(<var>objectType</var>, <var>firstField</var>)</span>
  2317. <ol>
  2318. <li>Call the method provided by the type system for determining the field type on a given object type.</li>
  2319. </ol>
  2320. </div>
  2321. <div class="spec-algo" id="ResolveFieldOnObject()"><span class="spec-call"><a href="#ResolveFieldOnObject()">ResolveFieldOnObject</a>(<var>objectType</var>, <var>object</var>, <var>firstField</var>)</span>
  2322. <ol>
  2323. <li>Call the method provided by the type system for determining the resolution of a field on a given object.</li>
  2324. </ol>
  2325. </div>
  2326. <div class="spec-algo" id="MergeSelectionSets()"><span class="spec-call"><a href="#MergeSelectionSets()">MergeSelectionSets</a>(<var>fields</var>)</span>
  2327. <ol>
  2328. <li>Let <var>selectionSet</var> be an empty list.</li>
  2329. <li>For each <var>field</var> in <var>fields</var>:
  2330. <ol>
  2331. <li>Let <var>fieldSelectionSet</var> be the selection set of <var>field</var>.</li>
  2332. <li>If <var>fieldSelectionSet</var> is null or empty, continue to the next field.</li>
  2333. <li>Append all selections in <var>fieldSelectionSet</var> to <var>selectionSet</var>.</li>
  2334. </ol>
  2335. </li>
  2336. <li>Return <var>selectionSet</var>.</li>
  2337. </ol>
  2338. </div>
  2339. <div class="spec-algo" id="CompleteValue()"><span class="spec-call"><a href="#CompleteValue()">CompleteValue</a>(<var>fieldType</var>, <var>result</var>, <var>subSelectionSet</var>)</span>
  2340. <ol>
  2341. <li>If the <var>fieldType</var> is a Non&#8208;Null type:
  2342. <ol>
  2343. <li>Let <var>innerType</var> be the inner type of <var>fieldType</var>.</li>
  2344. <li>Let <var>completedResult</var> be the result of calling <span class="spec-call"><a href="#CompleteValue()">CompleteValue</a>(<var>innerType</var>, <var>result</var>)</span>.</li>
  2345. <li>If <var>completedResult</var> is <span class="spec-keyword">null</span>, throw a field error.</li>
  2346. <li>Return <var>completedResult</var>.</li>
  2347. </ol>
  2348. </li>
  2349. <li>If <var>result</var> is <span class="spec-keyword">null</span> or a value similar to <span class="spec-keyword">null</span> such as <span class="spec-keyword">undefined</span> or <span class="spec-nt">NaN</span>, return <span class="spec-keyword">null</span>.</li>
  2350. <li>If <var>fieldType</var> is a List type:
  2351. <ol>
  2352. <li>If <var>result</var> is not a collection of values, throw a field error.</li>
  2353. <li>Let <var>innerType</var> be the inner type of <var>fieldType</var>.</li>
  2354. <li>Return a list where each item is the result of calling <span class="spec-call"><a href="#CompleteValue()">CompleteValue</a>(<var>innerType</var>, <var>resultItem</var>)</span>, where <var>resultItem</var> is each item in <var>result</var>.</li>
  2355. </ol>
  2356. </li>
  2357. <li>If <var>fieldType</var> is a Scalar or Enum type:
  2358. <ol>
  2359. <li>Return the result of &ldquo;coercing&rdquo; <var>result</var>, ensuring it is a legal value of <var>fieldType</var>, otherwise <span class="spec-keyword">null</span>.</li>
  2360. </ol>
  2361. </li>
  2362. <li>If <var>fieldType</var> is an Object, Interface, or Union type:
  2363. <ol>
  2364. <li>Return the result of evaluating <var>subSelectionSet</var> on <var>fieldType</var> normally.</li>
  2365. </ol>
  2366. </li>
  2367. </ol>
  2368. </div>
  2369. </section>
  2370. <section id="sec-Normal-evaluation">
  2371. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Normal-evaluation">6.4.2</a></span>Normal evaluation</h4>
  2372. <p>When evaluating a grouped field set without a serial execution order requirement, the executor can determine the entries in the result map in whatever order it chooses. Because the resolution of fields other than top&#8208;level mutation fields
  2373. is always side effect&ndash;free and idempotent, the execution order must not affect the result, and hence the server has the freedom to evaluate the field entries in whatever order it deems optimal.</p>
  2374. <p>For example, given the following grouped field set to be evaluated normally:</p><pre><code>{
  2375. birthday {
  2376. month
  2377. }
  2378. address {
  2379. street
  2380. }
  2381. }
  2382. </code></pre>
  2383. <p>A valid GraphQL executor can resolve the four fields in whatever order it chose.</p>
  2384. </section>
  2385. <section id="sec-Serial-execution">
  2386. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Serial-execution">6.4.3</a></span>Serial execution</h4>
  2387. <p>Observe that based on the above sections, the only time an executor will run in serial execution order is on the top level selection set of a mutation operation and on its corresponding grouped field set.</p>
  2388. <p>When evaluating a grouped field set serially, the executor must consider each entry from the grouped field set in the order provided in the grouped field set. It must determine the corresponding entry in the result map for each item to completion
  2389. before it continues on to the next item in the grouped field set:</p>
  2390. <p>For example, given the following selection set to be evaluated serially:</p><pre><code>{
  2391. changeBirthday(birthday: $newBirthday) {
  2392. month
  2393. }
  2394. changeAddress(address: $newAddress) {
  2395. street
  2396. }
  2397. }
  2398. </code></pre>
  2399. <p>The executor must, in serial:</p>
  2400. <ul>
  2401. <li>Run <code>getFieldEntry</code> for <code>changeBirthday</code>, which during <code>CompleteValue</code> will evaluate the <code>{ month }</code> sub&#8208;selection set normally.</li>
  2402. <li>Run <code>getFieldEntry</code> for <code>changeAddress</code>, which during <code>CompleteValue</code> will evaluate the <code>{ street }</code> sub&#8208;selection set normally.</li>
  2403. </ul>
  2404. <p>As an illustrative example, let&rsquo;s assume we have a mutation field <code>changeTheNumber</code> that returns an object containing one field, <code>theNumber</code>. If we execute the following selection set serially:</p><pre><code>{
  2405. first: changeTheNumber(newNumber: 1) {
  2406. theNumber
  2407. }
  2408. second: changeTheNumber(newNumber: 3) {
  2409. theNumber
  2410. }
  2411. third: changeTheNumber(newNumber: 2) {
  2412. theNumber
  2413. }
  2414. }
  2415. </code></pre>
  2416. <p>The executor will evaluate the following serially:</p>
  2417. <ul>
  2418. <li>Resolve the <code>changeTheNumber(newNumber: 1)</code> field</li>
  2419. <li>Evaluate the <code>{ theNumber }</code> sub&#8208;selection set of <code>first</code> normally</li>
  2420. <li>Resolve the <code>changeTheNumber(newNumber: 3)</code> field</li>
  2421. <li>Evaluate the <code>{ theNumber }</code> sub&#8208;selection set of <code>second</code> normally</li>
  2422. <li>Resolve the <code>changeTheNumber(newNumber: 2)</code> field</li>
  2423. <li>Evaluate the <code>{ theNumber }</code> sub&#8208;selection set of <code>third</code> normally</li>
  2424. </ul>
  2425. <p>A correct executor must generate the following result for that selection set:</p><pre><code>{
  2426. <span class="hljs-string">"first"</span>: {
  2427. <span class="hljs-string">"theNumber"</span>: <span class="hljs-number">1</span>
  2428. },
  2429. <span class="hljs-string">"second"</span>: {
  2430. <span class="hljs-string">"theNumber"</span>: <span class="hljs-number">3</span>
  2431. },
  2432. <span class="hljs-string">"third"</span>: {
  2433. <span class="hljs-string">"theNumber"</span>: <span class="hljs-number">2</span>
  2434. }
  2435. }
  2436. </code></pre></section>
  2437. <section id="sec-Error-handling">
  2438. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Error-handling">6.4.4</a></span>Error handling</h4>
  2439. <p>If an error occurs when resolving a field, it should be treated as though the field returned null, and an error must be added to the &ldquo;errors&rdquo; list in the response.</p>
  2440. </section>
  2441. <section id="sec-Nullability">
  2442. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Nullability">6.4.5</a></span>Nullability</h4>
  2443. <p>If the result of resolving a field is null (either because the function to resolve the field returned null or because an error occurred), and that field is marked as being non&#8208;null in the type system, then the result of evaluating the entire
  2444. field set that contains this field is now null.</p>
  2445. <p>If the field was null because of an error, then the error has already been logged, and the &ldquo;errors&rdquo; list in the response must not be affected.</p>
  2446. <p>If the field resolution function returned null, and the field was non&#8208;null, then no error has been logged, so an appropriate error must be added to the &ldquo;errors&rdquo; list. </p>
  2447. </section>
  2448. </section>
  2449. </section>
  2450. <section id="sec-Response">
  2451. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Response">7</a></span>Response</h2>
  2452. <p>When a GraphQL server receives a request, it must return a well&#8208;formed response. The server&rsquo;s response describes the result of executing the requested operation if successful, and describes any errors encountered during the request.</p>
  2453. <p>A response may contain both a partial response as well as encountered errors in the case that an error occurred on a field which was replaced with null.</p>
  2454. <section id="sec-Serialization-Format">
  2455. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Serialization-Format">7.1</a></span>Serialization Format</h3>
  2456. <p>GraphQL does not require a specific serialization format. However, clients should use a serialization format that supports the major primitives in the GraphQL response. In particular, the serialization format must support representations of the
  2457. following four primitives:</p>
  2458. <ul>
  2459. <li>Map</li>
  2460. <li>List</li>
  2461. <li>String</li>
  2462. <li>Null</li>
  2463. </ul>
  2464. <p>A serialization format may support the following primitives, however, strings may be used as a substitute for those primitives.</p>
  2465. <ul>
  2466. <li>Boolean</li>
  2467. <li>Int</li>
  2468. <li>Float</li>
  2469. <li>Enum Value</li>
  2470. </ul>
  2471. <section id="sec-JSON-Serialization">
  2472. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-JSON-Serialization">7.1.1</a></span>JSON Serialization</h4>
  2473. <p>JSON is the preferred serialization format for GraphQL, though as noted above, GraphQL does not require a specific serialization format. For consistency and ease of notation, examples of the response are given in JSON throughout the spec. In particular,
  2474. in our JSON examples, we will represent primitives using the following JSON concepts:</p>
  2475. <table>
  2476. <thead>
  2477. <tr>
  2478. <th>GraphQL Value </th>
  2479. <th>JSON Value </th>
  2480. </tr>
  2481. </thead>
  2482. <tbody>
  2483. <tr>
  2484. <td>Map </td>
  2485. <td>Object </td>
  2486. </tr>
  2487. <tr>
  2488. <td>List </td>
  2489. <td>Array </td>
  2490. </tr>
  2491. <tr>
  2492. <td>Null </td>
  2493. <td><span class="spec-keyword">null</span> </td>
  2494. </tr>
  2495. <tr>
  2496. <td>String </td>
  2497. <td>String </td>
  2498. </tr>
  2499. <tr>
  2500. <td>Boolean </td>
  2501. <td><span class="spec-keyword">true</span> or <span class="spec-keyword">false</span> </td>
  2502. </tr>
  2503. <tr>
  2504. <td>Int </td>
  2505. <td>Number </td>
  2506. </tr>
  2507. <tr>
  2508. <td>Float </td>
  2509. <td>Number </td>
  2510. </tr>
  2511. <tr>
  2512. <td>Enum Value </td>
  2513. <td>String </td>
  2514. </tr>
  2515. </tbody>
  2516. </table>
  2517. </section>
  2518. </section>
  2519. <section id="sec-Response-Format">
  2520. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Response-Format">7.2</a></span>Response Format</h3>
  2521. <p>A response to a GraphQL operation must be a map.</p>
  2522. <p>If the operation included execution, the response map must contain an entry with key <code>data</code>. The value of this entry is described in the &ldquo;Data&rdquo; section. If the operation failed before execution, due to a syntax error, missing
  2523. information, or validation error, this entry must not be present.</p>
  2524. <p>If the operation encountered any errors, the response map must contain an entry with key <code>errors</code>. The value of this entry is described in the &ldquo;Errors&rdquo; section. If the operation completed without encountering any errors, this
  2525. entry must not be present.</p>
  2526. <p>The response map may also contain an entry with key <code>extensions</code>. This entry, if set, must have a map as its value. This entry is reserved for implementors to extend the protocol however they see fit, and hence there are no additional
  2527. restrictions on its contents.</p>
  2528. <p>To ensure future changes to the protocol do not break existing servers and clients, the top level response map must not contain any entries other than the three described above.</p>
  2529. <section id="sec-Data">
  2530. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Data">7.2.1</a></span>Data</h4>
  2531. <p>The <code>data</code> entry in the response will be the result of the execution of the requested operation. If the operation was a query, this output will be an object of the schema&rsquo;s query root type; if the operation was a mutation, this
  2532. output will be an object of the schema&rsquo;s mutation root type.</p>
  2533. <p>If an error was encountered before execution begins, the <code>data</code> entry should not be present in the result.</p>
  2534. <p>If an error was encountered during the execution that prevented a valid response, the <code>data</code> entry in the response should be <code>null</code>.</p>
  2535. </section>
  2536. <section id="sec-Errors">
  2537. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Errors">7.2.2</a></span>Errors</h4>
  2538. <p>The <code>errors</code> entry in the response is a non&#8208;empty list of errors, where each error is a map.</p>
  2539. <p>If no errors were encountered during the requested operation, the <code>errors</code> entry should not be present in the result.</p>
  2540. <p>Every error must contain an entry with the key <code>message</code> with a string description of the error intended for the developer as a guide to understand and correct the error.</p>
  2541. <p>If an error can be associated to a particular point in the requested GraphQL document, it should contain an entry with the key <code>locations</code> with a list of locations, where each location is a map with the keys <code>line</code> and <code>column</code>,
  2542. both positive numbers starting from <code>1</code> which describe the beginning of an associated syntax element.</p>
  2543. <p>GraphQL servers may provide additional entries to error as they choose to produce more helpful or machine&#8208;readable errors, however future versions of the spec may describe additional entries to errors.</p>
  2544. <p>If the <code>data</code> entry in the response is <code>null</code> or not present, the <code>errors</code> entry in the response must not be empty. It must contain at least one error. The errors it contains should indicate why no data was able
  2545. to be returned.</p>
  2546. <p>If the <code>data</code> entry in the response is not <code>null</code>, the <code>errors</code> entry in the response may contain any errors that occurred during execution. If errors occurred during execution, it should contain those errors.
  2547. </p>
  2548. </section>
  2549. </section>
  2550. </section>
  2551. <section id="sec-Grammar">
  2552. <h2><span class="spec-secnum" title="link to this section"><a href="#sec-Grammar">8</a></span>Grammar</h2>
  2553. <p>A GraphQL document is defined in a syntactic grammar where terminal symbols are tokens. Tokens are defined in a lexical grammar which matches patterns of source characters. The result of parsing a sequence of source UTF&#8208;8 characters produces
  2554. a GraphQL AST.</p>
  2555. <p>Symbols are defined (ex. Symbol :) as either one sequence of symbols or a list of possible sequences of symbols, either as a bulleted list or using the &ldquo;one of&rdquo; short hand.</p>
  2556. <p>A subscript suffix &ldquo;<span class="spec-nt optional">Symbol<span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2557. </span>&rdquo; is shorthand for two possible sequences, one including that symbol and one excluding it.</p>
  2558. <p>As an example:</p>
  2559. <div class="spec-production" id="Sentence"><span class="spec-nt"><a href="#Sentence">Sentence</a></span>
  2560. <div class="spec-rhs"><span class="spec-nt">Noun</span><span class="spec-nt">Verb</span><span class="spec-nt optional">Adverb<span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2561. </span>
  2562. </div>
  2563. </div>
  2564. <p>is shorthand for</p>
  2565. <div class="spec-production" id="Sentence"><span class="spec-nt"><a href="#Sentence">Sentence</a></span>
  2566. <div class="spec-rhs"><span class="spec-nt">Noun</span><span class="spec-nt">Verb</span></div>
  2567. <div class="spec-rhs"><span class="spec-nt">Noun</span><span class="spec-nt">Verb</span><span class="spec-nt">Adverb</span></div>
  2568. </div>
  2569. <p>A subscript suffix &ldquo;<span class="spec-nt list">Symbol<span class="spec-mods"><span class="spec-mod list">list</span></span>
  2570. </span>&rdquo; is shorthand for a list of one or more of that symbol.</p>
  2571. <p>As an example:</p>
  2572. <div class="spec-production" id="Book"><span class="spec-nt"><a href="#Book">Book</a></span>
  2573. <div class="spec-rhs"><span class="spec-nt">Cover</span><span class="spec-nt list">Page<span class="spec-mods"><span class="spec-mod list">list</span></span>
  2574. </span><span class="spec-nt">Cover</span></div>
  2575. </div>
  2576. <p>is shorthand for</p>
  2577. <div class="spec-production" id="Book"><span class="spec-nt"><a href="#Book">Book</a></span>
  2578. <div class="spec-rhs"><span class="spec-nt">Cover</span><span class="spec-nt"><a href="#Page_list">Page_list</a></span><span class="spec-nt">Cover</span></div>
  2579. </div>
  2580. <div class="spec-production" id="Page_list"><span class="spec-nt"><a href="#Page_list">Page_list</a></span>
  2581. <div class="spec-rhs"><span class="spec-nt">Page</span></div>
  2582. <div class="spec-rhs"><span class="spec-nt"><a href="#Page_list">Page_list</a></span><span class="spec-nt">Page</span></div>
  2583. </div>
  2584. <p>A symbol definition subscript suffix parameter in braces &ldquo;<span class="spec-nt">Symbol<span class="spec-mods"><span class="spec-params"><span class="spec-param">Param</span></span>
  2585. </span>
  2586. </span>&rdquo; is shorthand for two symbol definitions, one appended with that parameter name, the other without. The same subscript suffix on a symbol is shorthand for that variant of the definition. If the parameter starts with &ldquo;?&rdquo;, that
  2587. form of the symbol is used if in a symbol definition with the same parameter. Some possible sequences can be included or excluded conditionally when respectively prefixed with &ldquo;[+Param]&rdquo; and &ldquo;[~Param]&rdquo;.</p>
  2588. <p>As an example:</p>
  2589. <div class="spec-production" id="Example"><span class="spec-nt"><a href="#Example">Example</a><span class="spec-mods"><span class="spec-params"><span class="spec-param">Param</span></span>
  2590. </span>
  2591. </span>
  2592. <div class="spec-rhs"><span class="spec-nt">A</span></div>
  2593. <div class="spec-rhs"><span class="spec-nt">B<span class="spec-mods"><span class="spec-params"><span class="spec-param">Param</span></span>
  2594. </span>
  2595. </span>
  2596. </div>
  2597. <div class="spec-rhs"><span class="spec-nt">C<span class="spec-mods"><span class="spec-params"><span class="spec-param conditional">Param</span></span>
  2598. </span>
  2599. </span>
  2600. </div>
  2601. <div class="spec-rhs"><span class="spec-condition">Param</span><span class="spec-nt">D</span></div>
  2602. <div class="spec-rhs"><span class="spec-condition not">Param</span><span class="spec-nt">E</span></div>
  2603. </div>
  2604. <p>is shorthand for</p>
  2605. <div class="spec-production" id="Example"><span class="spec-nt"><a href="#Example">Example</a></span>
  2606. <div class="spec-rhs"><span class="spec-nt">A</span></div>
  2607. <div class="spec-rhs"><span class="spec-nt">B_param</span></div>
  2608. <div class="spec-rhs"><span class="spec-nt">C</span></div>
  2609. <div class="spec-rhs"><span class="spec-nt">E</span></div>
  2610. </div>
  2611. <div class="spec-production" id="Example_param"><span class="spec-nt"><a href="#Example_param">Example_param</a></span>
  2612. <div class="spec-rhs"><span class="spec-nt">A</span></div>
  2613. <div class="spec-rhs"><span class="spec-nt">B_param</span></div>
  2614. <div class="spec-rhs"><span class="spec-nt">C_param</span></div>
  2615. <div class="spec-rhs"><span class="spec-nt">D</span></div>
  2616. </div>
  2617. <section id="sec-Ignored-Source">
  2618. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Ignored-Source">8.1</a></span>Ignored Source</h3>
  2619. <p>Before and after every lexical token may be any amount of ignored source characters including whitespace and comments. No ignored regions of a source document are significant, however ignored source characters may appear within a lexical token,
  2620. for example a <span class="spec-nt">String</span> may contain whitespace.</p>
  2621. <p><strong>Ignoring commas</strong></p>
  2622. <p>GraphQL ignores the comma (<span class="spec-t">,</span>) character. This ensures that the absence or presence of a comma does not meaningfully alter the interpreted syntax of the document, as this can be a common user&#8208;error in other languages.
  2623. It also allows for the stylistic use of either trailing commas or line&#8208;terminators as delimiters which are often desired for legibility and maintainability of source code. The use of commas, whitespace, and line&#8208;terminators is encouraged
  2624. only when they improve the legibility of GraphQL documents.</p>
  2625. <p>GraphQL ignores these character sequences:</p>
  2626. <div class="spec-production" id="Ignored"><span class="spec-nt"><a href="#Ignored">Ignored</a></span>
  2627. <div class="spec-rhs"><span class="spec-nt"><a href="#WhiteSpace">WhiteSpace</a></span></div>
  2628. <div class="spec-rhs"><span class="spec-nt"><a href="#LineTerminator">LineTerminator</a></span></div>
  2629. <div class="spec-rhs"><span class="spec-nt"><a href="#Comment">Comment</a></span></div>
  2630. <div class="spec-rhs"><span class="spec-t">,</span></div>
  2631. </div>
  2632. <div class="spec-production" id="WhiteSpace"><span class="spec-nt"><a href="#WhiteSpace">WhiteSpace</a></span>
  2633. <div class="spec-rhs"><span class="spec-prose">Horizontal Tab (U+0009)</span></div>
  2634. <div class="spec-rhs"><span class="spec-prose">Vertical Tab (U+000B)</span></div>
  2635. <div class="spec-rhs"><span class="spec-prose">Form Feed (U+000C)</span></div>
  2636. <div class="spec-rhs"><span class="spec-prose">Space (U+0020)</span></div>
  2637. <div class="spec-rhs"><span class="spec-prose">No-break Space (U+00A0)</span></div>
  2638. </div>
  2639. <div class="spec-production" id="LineTerminator"><span class="spec-nt"><a href="#LineTerminator">LineTerminator</a></span>
  2640. <div class="spec-rhs"><span class="spec-prose">New Line (U+000A)</span></div>
  2641. <div class="spec-rhs"><span class="spec-prose">Carriage Return (U+000D)</span></div>
  2642. <div class="spec-rhs"><span class="spec-prose">Line Separator (U+2028)</span></div>
  2643. <div class="spec-rhs"><span class="spec-prose">Paragraph Separator (U+2029)</span></div>
  2644. </div>
  2645. <div class="spec-production" id="Comment"><span class="spec-nt"><a href="#Comment">Comment</a></span>
  2646. <div class="spec-rhs"><span class="spec-t">#</span><span class="spec-nt list optional"><a href="#CommentChar">CommentChar</a><span class="spec-mods"><span class="spec-mod list">list</span><span class="spec-mod optional">opt</span></span>
  2647. </span>
  2648. </div>
  2649. </div>
  2650. <div class="spec-production" id="CommentChar"><span class="spec-nt"><a href="#CommentChar">CommentChar</a></span>
  2651. <div class="spec-rhs"><span class="spec-constrained"><span class="spec-prose">Any character</span><span class="spec-butnot"><span class="spec-nt"><a href="#LineTerminator">LineTerminator</a></span></span>
  2652. </span>
  2653. </div>
  2654. </div>
  2655. </section>
  2656. <section id="sec-Tokens">
  2657. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Tokens">8.2</a></span>Tokens</h3>
  2658. <p>A GraphQL document is comprised of several kinds of source tokens defined here in a lexical grammar. This lexical grammar defines patterns of source characters by specifying character patterns in <span class="spec-t">monospace</span> or as
  2659. <span
  2660. class="spec-rx">/regular_expressions/</span>. Non&#8208;terminal patterns are defined as <span class="spec-nt">Italics</span>.</p>
  2661. <p>No characters are ignored while parsing a given token, for example no whitespace is allowed between the characters defining a <span class="spec-nt"><a href="#FloatValue">FloatValue</a></span>, however ignored characters are skipped before and after
  2662. each well&#8208;formed Token.</p>
  2663. <p>Tokens are later used as terminal symbols in GraphQL&rsquo;s syntactic grammar.</p>
  2664. <p>The GraphQL document syntactic grammar is defined in terms of these lexical tokens:</p>
  2665. <div class="spec-production" id="Token"><span class="spec-nt"><a href="#Token">Token</a></span>
  2666. <div class="spec-rhs"><span class="spec-nt"><a href="#Punctuator">Punctuator</a></span></div>
  2667. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span></div>
  2668. <div class="spec-rhs"><span class="spec-nt"><a href="#IntValue">IntValue</a></span></div>
  2669. <div class="spec-rhs"><span class="spec-nt"><a href="#FloatValue">FloatValue</a></span></div>
  2670. <div class="spec-rhs"><span class="spec-nt"><a href="#StringValue">StringValue</a></span></div>
  2671. </div>
  2672. <div class="spec-production oneof" id="Punctuator"><span class="spec-nt"><a href="#Punctuator">Punctuator</a></span>
  2673. <table>
  2674. <tr>
  2675. <td class="spec-rhs"><span class="spec-t">!</span></td>
  2676. <td class="spec-rhs"><span class="spec-t">$</span></td>
  2677. <td class="spec-rhs"><span class="spec-t">(</span></td>
  2678. <td class="spec-rhs"><span class="spec-t">)</span></td>
  2679. <td class="spec-rhs"><span class="spec-t">...</span></td>
  2680. <td class="spec-rhs"><span class="spec-t">:</span></td>
  2681. <td class="spec-rhs"><span class="spec-t">=</span></td>
  2682. <td class="spec-rhs"><span class="spec-t">@</span></td>
  2683. <td class="spec-rhs"><span class="spec-t">[</span></td>
  2684. <td class="spec-rhs"><span class="spec-t">]</span></td>
  2685. <td class="spec-rhs"><span class="spec-t">{</span></td>
  2686. <td class="spec-rhs"><span class="spec-t">|</span></td>
  2687. <td class="spec-rhs"><span class="spec-t">}</span></td>
  2688. </tr>
  2689. </table>
  2690. </div>
  2691. <div class="spec-production" id="Name"><span class="spec-nt"><a href="#Name">Name</a></span>
  2692. <div class="spec-rhs"><span class="spec-rx">/[_A-Za-z][_0-9A-Za-z]*/</span></div>
  2693. </div>
  2694. <div class="spec-production" id="IntValue"><span class="spec-nt"><a href="#IntValue">IntValue</a></span>
  2695. <div class="spec-rhs"><span class="spec-nt optional"><a href="#Sign">Sign</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2696. </span><span class="spec-nt"><a href="#IntegerPart">IntegerPart</a></span></div>
  2697. </div>
  2698. <div class="spec-production" id="FloatValue"><span class="spec-nt"><a href="#FloatValue">FloatValue</a></span>
  2699. <div class="spec-rhs"><span class="spec-nt optional"><a href="#Sign">Sign</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2700. </span><span class="spec-nt"><a href="#IntegerPart">IntegerPart</a></span><span class="spec-t">.</span><span class="spec-nt list"><a href="#Digit">Digit</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2701. </span><span class="spec-nt optional"><a href="#ExponentPart">ExponentPart</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2702. </span>
  2703. </div>
  2704. </div>
  2705. <div class="spec-production" id="Sign"><span class="spec-nt"><a href="#Sign">Sign</a></span>
  2706. <div class="spec-rhs"><span class="spec-t">-</span></div>
  2707. </div>
  2708. <div class="spec-production" id="IntegerPart"><span class="spec-nt"><a href="#IntegerPart">IntegerPart</a></span>
  2709. <div class="spec-rhs"><span class="spec-t">0</span></div>
  2710. <div class="spec-rhs"><span class="spec-nt"><a href="#NonZeroDigit">NonZeroDigit</a></span></div>
  2711. <div class="spec-rhs"><span class="spec-nt"><a href="#NonZeroDigit">NonZeroDigit</a></span><span class="spec-nt list"><a href="#Digit">Digit</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2712. </span>
  2713. </div>
  2714. </div>
  2715. <div class="spec-production" id="ExponentPart"><span class="spec-nt"><a href="#ExponentPart">ExponentPart</a></span>
  2716. <div class="spec-rhs"><span class="spec-t">e</span><span class="spec-nt optional"><a href="#Sign">Sign</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2717. </span><span class="spec-nt list"><a href="#Digit">Digit</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2718. </span>
  2719. </div>
  2720. </div>
  2721. <div class="spec-production oneof" id="NonZeroDigit"><span class="spec-nt"><a href="#NonZeroDigit">NonZeroDigit</a></span>
  2722. <table>
  2723. <tr>
  2724. <td class="spec-rhs"><span class="spec-t">1</span></td>
  2725. <td class="spec-rhs"><span class="spec-t">2</span></td>
  2726. <td class="spec-rhs"><span class="spec-t">3</span></td>
  2727. <td class="spec-rhs"><span class="spec-t">4</span></td>
  2728. <td class="spec-rhs"><span class="spec-t">5</span></td>
  2729. <td class="spec-rhs"><span class="spec-t">6</span></td>
  2730. <td class="spec-rhs"><span class="spec-t">7</span></td>
  2731. <td class="spec-rhs"><span class="spec-t">8</span></td>
  2732. <td class="spec-rhs"><span class="spec-t">9</span></td>
  2733. </tr>
  2734. </table>
  2735. </div>
  2736. <div class="spec-production" id="Digit"><span class="spec-nt"><a href="#Digit">Digit</a></span>
  2737. <div class="spec-rhs"><span class="spec-t">0</span></div>
  2738. <div class="spec-rhs"><span class="spec-nt"><a href="#NonZeroDigit">NonZeroDigit</a></span></div>
  2739. </div>
  2740. <div class="spec-production" id="StringValue"><span class="spec-nt"><a href="#StringValue">StringValue</a></span>
  2741. <div class="spec-rhs"><span class="spec-t">&quot;&quot;</span></div>
  2742. <div class="spec-rhs"><span class="spec-t">&quot;</span><span class="spec-nt list"><a href="#StringCharacter">StringCharacter</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2743. </span><span class="spec-t">&quot;</span></div>
  2744. </div>
  2745. <div class="spec-production" id="StringCharacter"><span class="spec-nt"><a href="#StringCharacter">StringCharacter</a></span>
  2746. <div class="spec-rhs"><span class="spec-constrained"><span class="spec-prose">Any character</span><span class="spec-butnot"><span class="spec-t">&quot;</span><span class="spec-t">\</span><span class="spec-nt"><a href="#LineTerminator">LineTerminator</a></span></span>
  2747. </span>
  2748. </div>
  2749. <div class="spec-rhs"><span class="spec-t">\</span><span class="spec-nt"><a href="#EscapedUnicode">EscapedUnicode</a></span></div>
  2750. <div class="spec-rhs"><span class="spec-t">\</span><span class="spec-nt"><a href="#EscapedCharacter">EscapedCharacter</a></span></div>
  2751. </div>
  2752. <div class="spec-production" id="EscapedUnicode"><span class="spec-nt"><a href="#EscapedUnicode">EscapedUnicode</a></span>
  2753. <div class="spec-rhs"><span class="spec-t">u</span><span class="spec-rx">/[0-9A-Fa-f]{4}/</span></div>
  2754. </div>
  2755. <div class="spec-production oneof" id="EscapedCharacter"><span class="spec-nt"><a href="#EscapedCharacter">EscapedCharacter</a></span>
  2756. <table>
  2757. <tr>
  2758. <td class="spec-rhs"><span class="spec-t">&quot;</span></td>
  2759. <td class="spec-rhs"><span class="spec-t">\</span></td>
  2760. <td class="spec-rhs"><span class="spec-t">/</span></td>
  2761. <td class="spec-rhs"><span class="spec-t">b</span></td>
  2762. <td class="spec-rhs"><span class="spec-t">f</span></td>
  2763. <td class="spec-rhs"><span class="spec-t">n</span></td>
  2764. <td class="spec-rhs"><span class="spec-t">r</span></td>
  2765. <td class="spec-rhs"><span class="spec-t">t</span></td>
  2766. </tr>
  2767. </table>
  2768. </div>
  2769. </section>
  2770. <section id="sec-Syntax">
  2771. <h3><span class="spec-secnum" title="link to this section"><a href="#sec-Syntax">8.3</a></span>Syntax</h3>
  2772. <p>A GraphQL document is defined in a syntactic grammar where terminal symbols are expressed as either an italicized token (ex. <span class="spec-nt"><a href="#Document">Document</a></span>) or as monospaced short&#8208;hand for a <span class="spec-nt"><a href="#Punctuator">Punctuator</a></span> (ex. <span class="spec-t">:</span>) or short&#8208;hand for a <span class="spec-nt"><a href="#Name">Name</a></span> (ex. <span class="spec-t">query</span>).</p>
  2773. <p>Since whitespace, comments, and other ignored source is skipped between each well&#8208;formed token, this ignored source can appear at any point between the terminal tokens in the syntactic grammars defined below. However GraphQL source documents
  2774. are encouraged to use ignored source only to improve legibility.</p>
  2775. <section id="sec-Syntax.Document">
  2776. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Syntax.Document">8.3.1</a></span>Document</h4>
  2777. <p>A GraphQL document describes a complete file or request string. A document contains multiple definitions including an Operation.</p>
  2778. <div class="spec-production" id="Document"><span class="spec-nt"><a href="#Document">Document</a></span>
  2779. <div class="spec-rhs"><span class="spec-nt list"><a href="#Definition">Definition</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2780. </span>
  2781. </div>
  2782. </div>
  2783. <div class="spec-production" id="Definition"><span class="spec-nt"><a href="#Definition">Definition</a></span>
  2784. <div class="spec-rhs"><span class="spec-nt"><a href="#OperationDefinition">OperationDefinition</a></span></div>
  2785. <div class="spec-rhs"><span class="spec-nt"><a href="#FragmentDefinition">FragmentDefinition</a></span></div>
  2786. </div>
  2787. </section>
  2788. <section id="sec-Syntax.Operations">
  2789. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Syntax.Operations">8.3.2</a></span>Operations</h4>
  2790. <p>An operation describes some type of request to GraphQL. The most common operation is a <code>query</code>, a read&#8208;only request for data from GraphQL. A short&#8208;hand syntax exists for a query operation.</p>
  2791. <div class="spec-production"
  2792. id="OperationDefinition"><span class="spec-nt"><a href="#OperationDefinition">OperationDefinition</a></span>
  2793. <div class="spec-rhs"><span class="spec-nt"><a href="#SelectionSet">SelectionSet</a></span></div>
  2794. <div class="spec-rhs"><span class="spec-nt"><a href="#OperationType">OperationType</a></span><span class="spec-nt"><a href="#Name">Name</a></span><span class="spec-nt optional"><a href="#VariableDefinitions">VariableDefinitions</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2795. </span><span class="spec-nt optional"><a href="#Directives">Directives</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2796. </span><span class="spec-nt"><a href="#SelectionSet">SelectionSet</a></span></div>
  2797. </div>
  2798. <div class="spec-production oneof" id="OperationType"><span class="spec-nt"><a href="#OperationType">OperationType</a></span>
  2799. <table>
  2800. <tr>
  2801. <td class="spec-rhs"><span class="spec-t">query</span></td>
  2802. <td class="spec-rhs"><span class="spec-t">mutation</span></td>
  2803. </tr>
  2804. </table>
  2805. </div>
  2806. <div class="spec-production" id="VariableDefinitions"><span class="spec-nt"><a href="#VariableDefinitions">VariableDefinitions</a></span>
  2807. <div class="spec-rhs"><span class="spec-t">(</span><span class="spec-nt list"><a href="#VariableDefinition">VariableDefinition</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2808. </span><span class="spec-t">)</span></div>
  2809. </div>
  2810. <div class="spec-production" id="VariableDefinition"><span class="spec-nt"><a href="#VariableDefinition">VariableDefinition</a></span>
  2811. <div class="spec-rhs"><span class="spec-nt"><a href="#Variable">Variable</a></span><span class="spec-t">:</span><span class="spec-nt"><a href="#Type">Type</a></span><span class="spec-nt optional"><a href="#DefaultValue">DefaultValue</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2812. </span>
  2813. </div>
  2814. </div>
  2815. <div class="spec-production" id="Variable"><span class="spec-nt"><a href="#Variable">Variable</a></span>
  2816. <div class="spec-rhs"><span class="spec-t">$</span><span class="spec-nt"><a href="#Name">Name</a></span></div>
  2817. </div>
  2818. <div class="spec-production" id="DefaultValue"><span class="spec-nt"><a href="#DefaultValue">DefaultValue</a></span>
  2819. <div class="spec-rhs"><span class="spec-t">=</span><span class="spec-nt"><a href="#Value">Value</a><span class="spec-mods"><span class="spec-params"><span class="spec-param">Const</span></span>
  2820. </span>
  2821. </span>
  2822. </div>
  2823. </div>
  2824. <div class="spec-production" id="SelectionSet"><span class="spec-nt"><a href="#SelectionSet">SelectionSet</a></span>
  2825. <div class="spec-rhs"><span class="spec-t">{</span><span class="spec-nt list"><a href="#Selection">Selection</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2826. </span><span class="spec-t">}</span></div>
  2827. </div>
  2828. <div class="spec-production" id="Selection"><span class="spec-nt"><a href="#Selection">Selection</a></span>
  2829. <div class="spec-rhs"><span class="spec-nt"><a href="#Field">Field</a></span></div>
  2830. <div class="spec-rhs"><span class="spec-nt"><a href="#FragmentSpread">FragmentSpread</a></span></div>
  2831. <div class="spec-rhs"><span class="spec-nt"><a href="#InlineFragment">InlineFragment</a></span></div>
  2832. </div>
  2833. <div class="spec-production" id="Field"><span class="spec-nt"><a href="#Field">Field</a></span>
  2834. <div class="spec-rhs"><span class="spec-nt optional"><a href="#Alias">Alias</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2835. </span><span class="spec-nt"><a href="#Name">Name</a></span><span class="spec-nt optional"><a href="#Arguments">Arguments</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2836. </span><span class="spec-nt optional"><a href="#Directives">Directives</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2837. </span><span class="spec-nt optional"><a href="#SelectionSet">SelectionSet</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2838. </span>
  2839. </div>
  2840. </div>
  2841. <div class="spec-production" id="Alias"><span class="spec-nt"><a href="#Alias">Alias</a></span>
  2842. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span><span class="spec-t">:</span></div>
  2843. </div>
  2844. <div class="spec-production" id="Arguments"><span class="spec-nt"><a href="#Arguments">Arguments</a></span>
  2845. <div class="spec-rhs"><span class="spec-t">(</span><span class="spec-nt list"><a href="#Argument">Argument</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2846. </span><span class="spec-t">)</span></div>
  2847. </div>
  2848. <div class="spec-production" id="Argument"><span class="spec-nt"><a href="#Argument">Argument</a></span>
  2849. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span><span class="spec-t">:</span><span class="spec-nt"><a href="#Value">Value</a></span></div>
  2850. </div>
  2851. </section>
  2852. <section id="sec-Syntax.Fragments">
  2853. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Syntax.Fragments">8.3.3</a></span>Fragments</h4>
  2854. <p>Fragments allow for the reuse of common selections of fields, reducing duplicated text in the document. Inline fragments can be used directly inline a selection to apply a type condition when querying against an interface or union.</p>
  2855. <div class="spec-production"
  2856. id="FragmentSpread"><span class="spec-nt"><a href="#FragmentSpread">FragmentSpread</a></span>
  2857. <div class="spec-rhs"><span class="spec-t">...</span><span class="spec-nt"><a href="#FragmentName">FragmentName</a></span><span class="spec-nt optional"><a href="#Directives">Directives</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2858. </span>
  2859. </div>
  2860. </div>
  2861. <div class="spec-production" id="InlineFragment"><span class="spec-nt"><a href="#InlineFragment">InlineFragment</a></span>
  2862. <div class="spec-rhs"><span class="spec-t">...</span><span class="spec-t">on</span><span class="spec-nt"><a href="#TypeCondition">TypeCondition</a></span><span class="spec-nt optional"><a href="#Directives">Directives</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2863. </span><span class="spec-nt"><a href="#SelectionSet">SelectionSet</a></span></div>
  2864. </div>
  2865. <div class="spec-production" id="FragmentDefinition"><span class="spec-nt"><a href="#FragmentDefinition">FragmentDefinition</a></span>
  2866. <div class="spec-rhs"><span class="spec-t">fragment</span><span class="spec-nt"><a href="#FragmentName">FragmentName</a></span><span class="spec-t">on</span><span class="spec-nt"><a href="#TypeCondition">TypeCondition</a></span><span class="spec-nt optional"><a href="#Directives">Directives</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2867. </span><span class="spec-nt"><a href="#SelectionSet">SelectionSet</a></span></div>
  2868. </div>
  2869. <div class="spec-production" id="FragmentName"><span class="spec-nt"><a href="#FragmentName">FragmentName</a></span>
  2870. <div class="spec-rhs"><span class="spec-lookahead not"><span class="spec-t">on</span></span><span class="spec-nt"><a href="#Name">Name</a></span></div>
  2871. </div>
  2872. <div class="spec-production" id="TypeCondition"><span class="spec-nt"><a href="#TypeCondition">TypeCondition</a></span>
  2873. <div class="spec-rhs"><span class="spec-nt"><a href="#TypeName">TypeName</a></span></div>
  2874. </div>
  2875. </section>
  2876. <section id="sec-Values">
  2877. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Values">8.3.4</a></span>Values</h4>
  2878. <p>Fields may take values for arguments. A value may be any JSON&#8208;style value, a variable or an Enum value.</p>
  2879. <div class="spec-production" id="Value"><span class="spec-nt"><a href="#Value">Value</a><span class="spec-mods"><span class="spec-params"><span class="spec-param">Const</span></span>
  2880. </span>
  2881. </span>
  2882. <div class="spec-rhs"><span class="spec-condition not">Const</span><span class="spec-nt"><a href="#Variable">Variable</a></span></div>
  2883. <div class="spec-rhs"><span class="spec-nt"><a href="#IntValue">IntValue</a></span></div>
  2884. <div class="spec-rhs"><span class="spec-nt"><a href="#FloatValue">FloatValue</a></span></div>
  2885. <div class="spec-rhs"><span class="spec-nt"><a href="#StringValue">StringValue</a></span></div>
  2886. <div class="spec-rhs"><span class="spec-nt"><a href="#BooleanValue">BooleanValue</a></span></div>
  2887. <div class="spec-rhs"><span class="spec-nt"><a href="#EnumValue">EnumValue</a></span></div>
  2888. <div class="spec-rhs"><span class="spec-nt"><a href="#ArrayValue">ArrayValue</a><span class="spec-mods"><span class="spec-params"><span class="spec-param conditional">Const</span></span>
  2889. </span>
  2890. </span>
  2891. </div>
  2892. <div class="spec-rhs"><span class="spec-nt"><a href="#ObjectValue">ObjectValue</a><span class="spec-mods"><span class="spec-params"><span class="spec-param conditional">Const</span></span>
  2893. </span>
  2894. </span>
  2895. </div>
  2896. </div>
  2897. <div class="spec-production" id="BooleanValue"><span class="spec-nt"><a href="#BooleanValue">BooleanValue</a></span>
  2898. <div class="spec-rhs"><span class="spec-t">true</span></div>
  2899. <div class="spec-rhs"><span class="spec-t">false</span></div>
  2900. </div>
  2901. <div class="spec-production" id="EnumValue"><span class="spec-nt"><a href="#EnumValue">EnumValue</a></span>
  2902. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span></div>
  2903. </div>
  2904. <section id="sec-Array-Value">
  2905. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Array-Value">8.3.4.1</a></span>Array Value</h5>
  2906. <div class="spec-production" id="ArrayValue"><span class="spec-nt"><a href="#ArrayValue">ArrayValue</a><span class="spec-mods"><span class="spec-params"><span class="spec-param">Const</span></span>
  2907. </span>
  2908. </span>
  2909. <div class="spec-rhs"><span class="spec-t">[</span><span class="spec-t">]</span></div>
  2910. <div class="spec-rhs"><span class="spec-t">[</span><span class="spec-nt list"><a href="#Value">Value</a><span class="spec-mods"><span class="spec-params"><span class="spec-param conditional">Const</span></span><span class="spec-mod list">list</span></span>
  2911. </span><span class="spec-t">]</span></div>
  2912. </div>
  2913. <p><strong>Semantics</strong></p>
  2914. <div class="spec-semantic"><span class="spec-nt"><a href="#ArrayValue">ArrayValue</a></span>
  2915. <div class="spec-rhs"><span class="spec-t">[</span><span class="spec-t">]</span></div>
  2916. <ol>
  2917. <li>Return a new empty list value.</li>
  2918. </ol>
  2919. </div>
  2920. <div class="spec-semantic"><span class="spec-nt"><a href="#ArrayValue">ArrayValue</a></span>
  2921. <div class="spec-rhs"><span class="spec-t">[</span><span class="spec-nt list"><a href="#Value">Value</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2922. </span><span class="spec-t">]</span></div>
  2923. <ol>
  2924. <li>Let <var>inputList</var> be a new empty list value.</li>
  2925. <li>For each <span class="spec-nt list"><a href="#Value">Value</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2926. </span>
  2927. <ol>
  2928. <li>Let <var>value</var> be the result of evaluating <span class="spec-nt"><a href="#Value">Value</a></span>.</li>
  2929. <li>Append <var>value</var> to <var>inputList</var>.</li>
  2930. </ol>
  2931. </li>
  2932. <li>Return <var>inputList</var></li>
  2933. </ol>
  2934. </div>
  2935. </section>
  2936. <section id="sec-Object-Value">
  2937. <h5><span class="spec-secnum" title="link to this section"><a href="#sec-Object-Value">8.3.4.2</a></span>Object Value</h5>
  2938. <div class="spec-production" id="ObjectValue"><span class="spec-nt"><a href="#ObjectValue">ObjectValue</a><span class="spec-mods"><span class="spec-params"><span class="spec-param">Const</span></span>
  2939. </span>
  2940. </span>
  2941. <div class="spec-rhs"><span class="spec-t">{</span><span class="spec-t">}</span></div>
  2942. <div class="spec-rhs"><span class="spec-t">{</span><span class="spec-nt list"><a href="#ObjectField">ObjectField</a><span class="spec-mods"><span class="spec-params"><span class="spec-param conditional">Const</span></span><span class="spec-mod list">list</span></span>
  2943. </span><span class="spec-t">}</span></div>
  2944. </div>
  2945. <div class="spec-production" id="ObjectField"><span class="spec-nt"><a href="#ObjectField">ObjectField</a><span class="spec-mods"><span class="spec-params"><span class="spec-param">Const</span></span>
  2946. </span>
  2947. </span>
  2948. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span><span class="spec-t">:</span><span class="spec-nt"><a href="#Value">Value</a><span class="spec-mods"><span class="spec-params"><span class="spec-param conditional">Const</span></span>
  2949. </span>
  2950. </span>
  2951. </div>
  2952. </div>
  2953. <p><strong>Semantics</strong></p>
  2954. <div class="spec-semantic"><span class="spec-nt"><a href="#ObjectValue">ObjectValue</a></span>
  2955. <div class="spec-rhs"><span class="spec-t">{</span><span class="spec-t">}</span></div>
  2956. <ol>
  2957. <li>Return a new input object value with no fields.</li>
  2958. </ol>
  2959. </div>
  2960. <div class="spec-semantic"><span class="spec-nt"><a href="#ObjectValue">ObjectValue</a></span>
  2961. <div class="spec-rhs"><span class="spec-t">{</span><span class="spec-nt list"><a href="#ObjectField">ObjectField</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2962. </span><span class="spec-t">}</span></div>
  2963. <ol>
  2964. <li>Let <var>inputObject</var> be a new input object value with no fields.</li>
  2965. <li>For each <var>field</var> in <span class="spec-nt list"><a href="#ObjectField">ObjectField</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2966. </span>
  2967. <ol>
  2968. <li>Let <var>name</var> be <span class="spec-nt"><a href="#Name">Name</a></span> in <var>field</var>.</li>
  2969. <li>If <var>inputObject</var> contains a field named <var>name</var> throw Syntax Error.</li>
  2970. <li>Let <var>value</var> be the result of evaluating <span class="spec-nt"><a href="#Value">Value</a></span> in <var>field</var>.</li>
  2971. <li>Add a field to <var>inputObject</var> of name <var>name</var> containing value <var>value</var>.</li>
  2972. </ol>
  2973. </li>
  2974. <li>Return <var>inputObject</var></li>
  2975. </ol>
  2976. </div>
  2977. </section>
  2978. </section>
  2979. <section id="sec-Syntax.Directives">
  2980. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Syntax.Directives">8.3.5</a></span>Directives</h4>
  2981. <p>Directives provide a way to describe runtime execution and type validation behavior in a GraphQL document.</p>
  2982. <div class="spec-production" id="Directives"><span class="spec-nt"><a href="#Directives">Directives</a></span>
  2983. <div class="spec-rhs"><span class="spec-nt list"><a href="#Directive">Directive</a><span class="spec-mods"><span class="spec-mod list">list</span></span>
  2984. </span>
  2985. </div>
  2986. </div>
  2987. <div class="spec-production" id="Directive"><span class="spec-nt"><a href="#Directive">Directive</a></span>
  2988. <div class="spec-rhs"><span class="spec-t">@</span><span class="spec-nt"><a href="#Name">Name</a></span><span class="spec-nt optional"><a href="#Arguments">Arguments</a><span class="spec-mods"><span class="spec-mod optional">opt</span></span>
  2989. </span>
  2990. </div>
  2991. </div>
  2992. </section>
  2993. <section id="sec-Syntax.Types">
  2994. <h4><span class="spec-secnum" title="link to this section"><a href="#sec-Syntax.Types">8.3.6</a></span>Types</h4>
  2995. <p>GraphQL describes the schema of the data it provides using a type system. These types are referred to in the document when defining query variables.</p>
  2996. <div class="spec-production" id="Type"><span class="spec-nt"><a href="#Type">Type</a></span>
  2997. <div class="spec-rhs"><span class="spec-nt"><a href="#TypeName">TypeName</a></span></div>
  2998. <div class="spec-rhs"><span class="spec-nt"><a href="#ListType">ListType</a></span></div>
  2999. <div class="spec-rhs"><span class="spec-nt"><a href="#NonNullType">NonNullType</a></span></div>
  3000. </div>
  3001. <div class="spec-production" id="TypeName"><span class="spec-nt"><a href="#TypeName">TypeName</a></span>
  3002. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span></div>
  3003. </div>
  3004. <div class="spec-production" id="ListType"><span class="spec-nt"><a href="#ListType">ListType</a></span>
  3005. <div class="spec-rhs"><span class="spec-t">[</span><span class="spec-nt"><a href="#Type">Type</a></span><span class="spec-t">]</span></div>
  3006. </div>
  3007. <div class="spec-production" id="NonNullType"><span class="spec-nt"><a href="#NonNullType">NonNullType</a></span>
  3008. <div class="spec-rhs"><span class="spec-nt"><a href="#TypeName">TypeName</a></span><span class="spec-t">!</span></div>
  3009. <div class="spec-rhs"><span class="spec-nt"><a href="#ListType">ListType</a></span><span class="spec-t">!</span></div>
  3010. </div>
  3011. <p><strong>Semantics</strong></p>
  3012. <div class="spec-semantic"><span class="spec-nt"><a href="#Type">Type</a></span>
  3013. <div class="spec-rhs"><span class="spec-nt"><a href="#Name">Name</a></span></div>
  3014. <ol>
  3015. <li>Let <var>name</var> be the string value of <span class="spec-nt"><a href="#Name">Name</a></span></li>
  3016. <li>Let <var>type</var> be the type defined in the Schema named <var>name</var></li>
  3017. <li><var>type</var> must not be <span class="spec-keyword">null</span></li>
  3018. <li>Return <var>type</var></li>
  3019. </ol>
  3020. </div>
  3021. <div class="spec-semantic"><span class="spec-nt"><a href="#Type">Type</a></span>
  3022. <div class="spec-rhs"><span class="spec-t">[</span><span class="spec-nt"><a href="#Type">Type</a></span><span class="spec-t">]</span></div>
  3023. <ol>
  3024. <li>Let <var>itemType</var> be the result of evaluating <span class="spec-nt"><a href="#Type">Type</a></span></li>
  3025. <li>Let <var>type</var> be a List type where <var>itemType</var> is the contained type.</li>
  3026. <li>Return <var>type</var></li>
  3027. </ol>
  3028. </div>
  3029. <div class="spec-semantic"><span class="spec-nt"><a href="#Type">Type</a></span>
  3030. <div class="spec-rhs"><span class="spec-nt"><a href="#Type">Type</a></span><span class="spec-t">!</span></div>
  3031. <ol>
  3032. <li>Let <var>nullableType</var> be the result of evaluating <span class="spec-nt"><a href="#Type">Type</a></span></li>
  3033. <li>Let <var>type</var> be a Non&#8208;Null type where <var>nullableType</var> is the contained type.</li>
  3034. <li>Return <var>type</var> </li>
  3035. </ol>
  3036. </div>
  3037. </section>
  3038. </section>
  3039. </section>
  3040. <footer>Written in <a href="http://leebyron.com/spec-md/" target="_blank">Spec Markdown</a>.</footer>