(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[949],{7549:function(s,l,e){(window.__NEXT_P=window.__NEXT_P||[]).push(["/docs/plugins/simple-objects",function(){return e(7887)}])},7887:function(s,l,e){"use strict";e.r(l),e.d(l,{__N_SSG:function(){return i},frontmatter:function(){return t}});var n=e(5250),a=e(7160),c=e(3210),i=!0;let t={name:"Simple objects plugin",title:"Simple objects plugin for Pothos GraphQL",menu:"Plugins",description:"Simple objects plugin docs for Pothos"},r=c.k;function h(s){let l=Object.assign({h1:"h1",p:"p",h2:"h2",h3:"h3",pre:"pre",code:"code",span:"span"},(0,a.ah)(),s.components);return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(l.h1,{id:"simple-objects-plugin",children:"Simple Objects Plugin"}),"\n",(0,n.jsx)(l.p,{children:"The Simple Objects Plugin provides a way to define objects and interfaces without defining type\ndefinitions for those objects, while still getting full type safety."}),"\n",(0,n.jsx)(l.h2,{id:"usage",children:"Usage"}),"\n",(0,n.jsx)(l.h3,{id:"install",children:"Install"}),"\n",(0,n.jsx)(l.pre,{children:(0,n.jsx)(l.code,{className:"hljs language-bash",children:"yarn add @pothos/plugin-simple-objects\n"})}),"\n",(0,n.jsx)(l.h3,{id:"setup",children:"Setup"}),"\n",(0,n.jsx)(l.pre,{children:(0,n.jsxs)(l.code,{className:"hljs language-typescript",children:[(0,n.jsx)(l.span,{className:"hljs-keyword",children:"import"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SimpleObjectsPlugin"})," ",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"from"})," ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'@pothos/plugin-simple-objects'"}),";\n",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"const"})," builder = ",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SchemaBuilder"}),"({\n  ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"plugins"}),": [",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SimpleObjectsPlugin"}),"],\n});\n"]})}),"\n",(0,n.jsx)(l.h3,{id:"example",children:"Example"}),"\n",(0,n.jsx)(l.pre,{children:(0,n.jsxs)(l.code,{className:"hljs language-typescript",children:[(0,n.jsx)(l.span,{className:"hljs-keyword",children:"import"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SchemaBuilder"})," ",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"from"})," ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'@pothos/core'"}),";\n",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"import"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SimpleObjectsPlugin"})," ",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"from"})," ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'@pothos/plugin-simple-objects'"}),";\n\n",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"const"})," builder = ",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"new"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SchemaBuilder"}),"({\n  ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"plugins"}),": [",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"SimpleObjectsPlugin"}),"],\n});\n\n",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"const"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"ContactInfo"})," = builder.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"simpleObject"}),"(",(0,n.jsx)(l.span,{className:"hljs-string",children:"'ContactInfo'"}),", {\n  ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"email"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"string"}),"({\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"nullable"}),": ",(0,n.jsx)(l.span,{className:"hljs-literal",children:"false"}),",\n    }),\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"phoneNumber"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"string"}),"({\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"nullable"}),": ",(0,n.jsx)(l.span,{className:"hljs-literal",children:"true"}),",\n    }),\n  }),\n});\n\n",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"const"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"Node"})," = builder.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"simpleInterface"}),"(",(0,n.jsx)(l.span,{className:"hljs-string",children:"'Node'"}),", {\n  ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"id"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"id"}),"({\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"nullable"}),": ",(0,n.jsx)(l.span,{className:"hljs-literal",children:"false"}),",\n    }),\n  }),\n});\n\n",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"const"})," ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"UserType"})," = builder.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"simpleObject"}),"(\n  ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'User'"}),",\n  {\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"interfaces"}),": [",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"Node"}),"],\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"firstName"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"string"}),"(),\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"lastName"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"string"}),"(),\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"contactInfo"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"field"}),"({\n        ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"type"}),": ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"ContactInfo"}),",\n        ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"nullable"}),": ",(0,n.jsx)(l.span,{className:"hljs-literal",children:"false"}),",\n      }),\n    }),\n  },\n  ",(0,n.jsx)(l.span,{className:"hljs-comment",children:"// You can add additional fields with resolvers with a third fields argument"}),"\n  ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"fullName"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"string"}),"({\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"resolve"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"user"}),") =>"]})," ",(0,n.jsxs)(l.span,{className:"hljs-string",children:["`",(0,n.jsx)(l.span,{className:"hljs-subst",children:"${user.firstName}"})," ",(0,n.jsx)(l.span,{className:"hljs-subst",children:"${user.lastName}"}),"`"]}),",\n    }),\n  }),\n);\n\nbuilder.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"queryType"}),"({\n  ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"fields"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"user"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"field"}),"({\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"type"}),": ",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"UserType"}),",\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"args"}),": {\n        ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"id"}),": t.",(0,n.jsx)(l.span,{className:"hljs-property",children:"arg"}),".",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"id"}),"({ ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"required"}),": ",(0,n.jsx)(l.span,{className:"hljs-literal",children:"true"})," }),\n      },\n      ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"resolve"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"parent, args, { User }"}),") =>"]})," {\n        ",(0,n.jsx)(l.span,{className:"hljs-keyword",children:"return"})," {\n          ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"id"}),": ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'1003'"}),",\n          ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"firstName"}),": ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'Leia'"}),",\n          ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"lastName"}),": ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'Organa'"}),",\n          ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"contactInfo"}),": {\n            ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"email"}),": ",(0,n.jsx)(l.span,{className:"hljs-string",children:"'leia@example.com'"}),",\n            ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"phoneNumber"}),": ",(0,n.jsx)(l.span,{className:"hljs-literal",children:"null"}),",\n          },\n        };\n      },\n    }),\n  }),\n});\n"]})}),"\n",(0,n.jsx)(l.h2,{id:"extending-simple-objects",children:"Extending simple objects"}),"\n",(0,n.jsx)(l.p,{children:"In some cases, you may want to add more complex fields with resolvers or args where the value isn't\njust passed down from the parent."}),"\n",(0,n.jsxs)(l.p,{children:["In these cases, you can either add the field in the 3rd arg (fields) as shown above, or you can add\nadditional fields to the type using methods like ",(0,n.jsx)(l.code,{children:"builder.objectType"}),":"]}),"\n",(0,n.jsx)(l.pre,{children:(0,n.jsxs)(l.code,{className:"hljs language-typescript",children:["builder.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"objectType"}),"(",(0,n.jsx)(l.span,{className:"hljs-title class_",children:"UserType"}),", ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n  ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"fullName"}),": t.",(0,n.jsx)(l.span,{className:"hljs-title function_",children:"string"}),"({\n    ",(0,n.jsx)(l.span,{className:"hljs-attr",children:"resolve"}),": ",(0,n.jsxs)(l.span,{className:"hljs-function",children:["(",(0,n.jsx)(l.span,{className:"hljs-params",children:"user"}),") =>"]})," ",(0,n.jsxs)(l.span,{className:"hljs-string",children:["`",(0,n.jsx)(l.span,{className:"hljs-subst",children:"${user.firstName}"})," ",(0,n.jsx)(l.span,{className:"hljs-subst",children:"${user.lastName}"}),"`"]}),",\n  }),\n}));\n"]})}),"\n",(0,n.jsx)(l.h2,{id:"limitations",children:"Limitations"}),"\n",(0,n.jsxs)(l.p,{children:["When using simpleObjects in combination with other plugins like authorization, those plugins may use\n",(0,n.jsx)(l.code,{children:"unknown"})," as the parent type in some custom fields (eg. ",(0,n.jsx)(l.code,{children:"parent"})," of a permission check function on\na field)."]})]})}l.default=function(){let s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return(0,n.jsx)(r,Object.assign({},s,{children:(0,n.jsx)(h,s)}))}}},function(s){s.O(0,[8430,3210,9774,2888,179],function(){return s(s.s=7549)}),_N_E=s.O()}]);