(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[2075],{4125:function(s,e,n){(window.__NEXT_P=window.__NEXT_P||[]).push(["/docs/plugins/sub-graph",function(){return n(5371)}])},5371:function(s,e,n){"use strict";n.r(e),n.d(e,{__N_SSG:function(){return r},frontmatter:function(){return t}});var l=n(5250),a=n(7160),i=n(3210),r=!0;let t={name:"SubGraph plugin",title:"SubGraph plugin for Pothos GraphQL",menu:"Plugins",description:"SubGraph plugin docs for Pothos"},h=i.k;function c(s){let e=Object.assign({h1:"h1",p:"p",h2:"h2",h3:"h3",pre:"pre",code:"code",span:"span",ul:"ul",li:"li"},(0,a.ah)(),s.components);return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(e.h1,{id:"subgraph-plugin",children:"SubGraph Plugin"}),"\n",(0,l.jsx)(e.p,{children:"A plugin for creating sub-selections of your graph. This allows you to use the same code/types for\nmultiple variants of your API."}),"\n",(0,l.jsx)(e.p,{children:"One common use case for this is to share implementations between your public and internal APIs, by\nonly exposing a subset of your graph publicly."}),"\n",(0,l.jsx)(e.h2,{id:"usage",children:"Usage"}),"\n",(0,l.jsx)(e.h3,{id:"install",children:"Install"}),"\n",(0,l.jsx)(e.pre,{children:(0,l.jsx)(e.code,{className:"hljs language-bash",children:"yarn add @pothos/plugin-sub-graph\n"})}),"\n",(0,l.jsx)(e.h3,{id:"setup",children:"Setup"}),"\n",(0,l.jsx)(e.pre,{children:(0,l.jsxs)(e.code,{className:"hljs language-typescript",children:[(0,l.jsx)(e.span,{className:"hljs-keyword",children:"import"})," ",(0,l.jsx)(e.span,{className:"hljs-title class_",children:"SubGraphPlugin"})," ",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"from"})," ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'@pothos/plugin-sub-graph'"}),";\n",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"const"})," builder = ",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,l.jsx)(e.span,{className:"hljs-title class_",children:"SchemaBuilder"}),"<{\n  ",(0,l.jsx)(e.span,{className:"hljs-title class_",children:"SubGraphs"}),": ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Public'"})," | ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Internal'"}),";\n}>({\n  ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"plugins"}),": [",(0,l.jsx)(e.span,{className:"hljs-title class_",children:"SubGraphPlugin"}),"],\n  ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"subGraphs"}),": {\n    ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"defaultForTypes"}),": [],\n    ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"fieldsInheritFromTypes"}),": ",(0,l.jsx)(e.span,{className:"hljs-literal",children:"true"}),",\n  },\n});\n\n",(0,l.jsx)(e.span,{className:"hljs-comment",children:"//in another file:"}),"\n\n",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"const"})," schema = builder.",(0,l.jsx)(e.span,{className:"hljs-title function_",children:"toSchema"}),"();\n",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"const"})," publicSchema = builder.",(0,l.jsx)(e.span,{className:"hljs-title function_",children:"toSchema"}),"({ ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"subGraph"}),": ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Public'"})," });\n",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"const"})," internalSchema = builder.",(0,l.jsx)(e.span,{className:"hljs-title function_",children:"toSchema"}),"({ ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"subGraph"}),": ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Internal'"})," });\n\n",(0,l.jsx)(e.span,{className:"hljs-comment",children:"// You can also build a graph using multiple subgraphs:"}),"\n",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"const"})," combinedSchema = builder.",(0,l.jsx)(e.span,{className:"hljs-title function_",children:"toSchema"}),"({ ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"subGraph"}),": [",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Internal'"}),", ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Public'"}),"] });\n"]})}),"\n",(0,l.jsx)(e.h3,{id:"options-on-types",children:"Options on Types"}),"\n",(0,l.jsxs)(e.ul,{children:["\n",(0,l.jsxs)(e.li,{children:[(0,l.jsx)(e.code,{children:"subGraphs"}),": An optional array of sub-graph the type should be included in."]}),"\n"]}),"\n",(0,l.jsx)(e.h3,{id:"object-and-interface-types",children:"Object and Interface types:"}),"\n",(0,l.jsxs)(e.ul,{children:["\n",(0,l.jsxs)(e.li,{children:[(0,l.jsx)(e.code,{children:"defaultSubGraphsForFields"}),": Default sub-graph for fields of the type to be included in."]}),"\n"]}),"\n",(0,l.jsx)(e.h2,{id:"options-on-fields",children:"Options on Fields"}),"\n",(0,l.jsxs)(e.ul,{children:["\n",(0,l.jsxs)(e.li,{children:["\n",(0,l.jsxs)(e.p,{children:[(0,l.jsx)(e.code,{children:"subGraphs"}),": An optional array of sub-graph the field to be included in. If not provided, will"]}),"\n",(0,l.jsx)(e.p,{children:"fallback to:"}),"\n",(0,l.jsxs)(e.ul,{children:["\n",(0,l.jsxs)(e.li,{children:[(0,l.jsx)(e.code,{children:"defaultForFields"})," if set on type"]}),"\n",(0,l.jsxs)(e.li,{children:[(0,l.jsx)(e.code,{children:"subGraphs"})," of the type if ",(0,l.jsx)(e.code,{children:"subGraphs.fieldsInheritFromTypes"})," was set in the builder"]}),"\n",(0,l.jsx)(e.li,{children:"an empty array"}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,l.jsx)(e.h3,{id:"options-on-builder",children:"Options on Builder"}),"\n",(0,l.jsxs)(e.ul,{children:["\n",(0,l.jsxs)(e.li,{children:[(0,l.jsx)(e.code,{children:"subGraphs.defaultForTypes"}),": Specifies what sub-graph a type is part of by default."]}),"\n",(0,l.jsxs)(e.li,{children:[(0,l.jsx)(e.code,{children:"subGraphs.fieldsInheritFromTypes"}),": defaults to ",(0,l.jsx)(e.code,{children:"false"}),". When true, fields on a type will default\nto being part of the same sub-graph as their parent type. Only applies when type does not have\n",(0,l.jsx)(e.code,{children:"defaultForFields"})," set."]}),"\n"]}),"\n",(0,l.jsx)(e.h3,{id:"usage-1",children:"Usage"}),"\n",(0,l.jsx)(e.pre,{children:(0,l.jsxs)(e.code,{className:"hljs language-typescript",children:["builder.",(0,l.jsx)(e.span,{className:"hljs-title function_",children:"queryType"}),"({\n  ",(0,l.jsx)(e.span,{className:"hljs-comment",children:"// Query type will be available in default, Public, and Internal schemas"}),"\n  ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"subGraphs"}),": [",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Public'"}),", ",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Internal'"}),"],\n  ",(0,l.jsx)(e.span,{className:"hljs-comment",children:"// Fields on the Query object will now default to not being a part of any subgraph"}),"\n  ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"defaultForFields"}),": [];\n  ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"fields"}),": ",(0,l.jsxs)(e.span,{className:"hljs-function",children:["(",(0,l.jsx)(e.span,{className:"hljs-params",children:"t"}),") =>"]})," ({\n    ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"someField"}),": t.",(0,l.jsx)(e.span,{className:"hljs-title function_",children:"string"}),"({\n      ",(0,l.jsx)(e.span,{className:"hljs-comment",children:'// someField will be in the default schema and "Internal" sub graph, but'}),"\n      ",(0,l.jsx)(e.span,{className:"hljs-comment",children:"// not present in the Public sub graph"}),"\n      ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"subGraphs"}),": [",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Internal'"}),"]\n      ",(0,l.jsx)(e.span,{className:"hljs-attr",children:"resolve"}),": ",(0,l.jsx)(e.span,{className:"hljs-function",children:"() =>"})," {\n        ",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"throw"})," ",(0,l.jsx)(e.span,{className:"hljs-keyword",children:"new"})," ",(0,l.jsx)(e.span,{className:"hljs-title class_",children:"Error"}),"(",(0,l.jsx)(e.span,{className:"hljs-string",children:"'Not implemented'"}),");\n      },\n    }),\n  }),\n});\n"]})}),"\n",(0,l.jsx)(e.h3,{id:"missing-types",children:"Missing types"}),"\n",(0,l.jsx)(e.p,{children:"When creating a sub-graph, the plugin will only copy in types that are included in the sub-graph,\neither by explicitly setting it on the type, or because the sub-graph is included in the default\nlist. Like types, output fields that are not included in a sub-graph will also be omitted. Arguments\nand fields on Input types can not be removed because that would break assumptions about argument\ntypes in resolvers."}),"\n",(0,l.jsx)(e.p,{children:"If a type that is not included in the sub-graph is referenced by another part of the graph that is\nincluded in the graph, a runtime error will be thrown when the sub graph is constructed. This can\nhappen in a number of cases including cases where a removed type is used in the interfaces of an\nobject, a member of a union, or the type of a field argument."})]})}e.default=function(){let s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return(0,l.jsx)(h,Object.assign({},s,{children:(0,l.jsx)(c,s)}))}}},function(s){s.O(0,[8430,3210,9774,2888,179],function(){return s(s.s=4125)}),_N_E=s.O()}]);