(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[239],{7256:function(s,e,a){(window.__NEXT_P=window.__NEXT_P||[]).push(["/docs/guide/inputs",function(){return a(90)}])},90:function(s,e,a){"use strict";a.r(e),a.d(e,{__N_SSG:function(){return t},frontmatter:function(){return c}});var n=a(5250),l=a(7160),r=a(3210),t=!0;let c={title:"Defining Input Objects in GraphQL Pothos",name:"Input Objects",menu:"Guide",description:"Guide for defining Input Object types in Pothos"},i=r.k;function h(s){let e=Object.assign({h1:"h1",h2:"h2",p:"p",code:"code",pre:"pre",span:"span"},(0,l.ah)(),s.components);return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(e.h1,{id:"input-objects",children:"Input Objects"}),"\n",(0,n.jsx)(e.h2,{id:"creating-input-objects",children:"Creating Input objects"}),"\n",(0,n.jsxs)(e.p,{children:["Input objects can be created using ",(0,n.jsx)(e.code,{children:"builder.inputType"}),"."]}),"\n",(0,n.jsx)(e.pre,{children:(0,n.jsxs)(e.code,{className:"hljs language-typescript",children:[(0,n.jsx)(e.span,{className:"hljs-keyword",children:"const"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"GiraffeInput"})," = builder.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"inputType"}),"(",(0,n.jsx)(e.span,{className:"hljs-string",children:"'GiraffeInput'"}),", {\n  ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"name"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"string"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n    ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"birthdate"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"string"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n    ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"height"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"float"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n  }),\n});\n\nbuilder.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"mutationType"}),"({\n  ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"createGiraffe"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"field"}),"({\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"type"}),": ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Giraffe"}),",\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"args"}),": {\n        ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"input"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"arg"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"type"}),": ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"GiraffeInput"}),", ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n      },\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"resolve"}),": ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"root, args"}),") =>"]}),"\n        ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Giraffe"}),"(args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"name"}),", ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Date"}),"(args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"birthdate"}),"), args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"height"}),"),\n    }),\n  }),\n});\n"]})}),"\n",(0,n.jsx)(e.h2,{id:"recursive-inputs",children:"Recursive inputs"}),"\n",(0,n.jsx)(e.p,{children:"Types for recursive inputs get slightly more complicated to implement because their types can't\neasily be inferred. Referencing other input types works without any additional logic, as long as\nthere is no circular reference to the original type."}),"\n",(0,n.jsxs)(e.p,{children:["To build input types with recursive references you can use ",(0,n.jsx)(e.code,{children:"builder.inputRef"})," along with a type or\ninterface that describes the fields of your input object. The builder will still ensure all the\ntypes are correct, but needs type definitions to help infer the correct values."]}),"\n",(0,n.jsx)(e.pre,{children:(0,n.jsxs)(e.code,{className:"hljs language-typescript",children:[(0,n.jsx)(e.span,{className:"hljs-keyword",children:"interface"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"RecursiveGiraffeInputShape"})," {\n  ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"name"}),": ",(0,n.jsx)(e.span,{className:"hljs-built_in",children:"string"}),";\n  ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"birthdate"}),": ",(0,n.jsx)(e.span,{className:"hljs-built_in",children:"string"}),";\n  ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"height"}),": ",(0,n.jsx)(e.span,{className:"hljs-built_in",children:"number"}),";\n  friends?: ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"RecursiveGiraffeInputShape"}),"[];\n}\n\n",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"const"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"RecursiveGiraffeInput"})," = builder\n  .",(0,n.jsx)(e.span,{className:"hljs-property",children:"inputRef"}),"<",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"RecursiveGiraffeInputShape"}),">(",(0,n.jsx)(e.span,{className:"hljs-string",children:"'RecursiveGiraffeInput'"}),")\n  .",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"implement"}),"({\n    ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"name"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"string"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"birthdate"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"string"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"height"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"float"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"friends"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"field"}),"({\n        ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"type"}),": [",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"RecursiveGiraffeInput"}),"],\n      }),\n    }),\n  });\n\nbuilder.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"mutationType"}),"({\n  ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"createGiraffeWithFriends"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"field"}),"({\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"type"}),": [",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Giraffe"}),"],\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"args"}),": {\n        ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"input"}),": t.",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"arg"}),"({ ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"type"}),": ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"RecursiveGiraffeInput"}),", ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(e.span,{className:"hljs-literal",children:"true"})," }),\n      },\n      ",(0,n.jsx)(e.span,{className:"hljs-attr",children:"resolve"}),": ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"root, args"}),") =>"]})," {\n        ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"const"})," friends = (args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"friends"})," || []).",(0,n.jsx)(e.span,{className:"hljs-title function_",children:"map"}),"(\n          ",(0,n.jsxs)(e.span,{className:"hljs-function",children:["(",(0,n.jsx)(e.span,{className:"hljs-params",children:"friend"}),") =>"]}),"\n            ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Giraffe"}),"(args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"name"}),", ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Date"}),"(args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"birthdate"}),"), args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"height"}),"),\n        );\n\n        ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"return"})," [\n          ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Giraffe"}),"(args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"name"}),", ",(0,n.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(e.span,{className:"hljs-title class_",children:"Date"}),"(args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"birthdate"}),"), args.",(0,n.jsx)(e.span,{className:"hljs-property",children:"input"}),".",(0,n.jsx)(e.span,{className:"hljs-property",children:"height"}),"),\n          ...friends,\n        ];\n      },\n    }),\n  }),\n});\n"]})})]})}e.default=function(){let s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return(0,n.jsx)(i,Object.assign({},s,{children:(0,n.jsx)(h,s)}))}}},function(s){s.O(0,[8430,3210,9774,2888,179],function(){return s(s.s=7256)}),_N_E=s.O()}]);