UNPKG

@redocly/graphql-docs

Version:

Redocly GraphQL docs

2 lines (1 loc) 2.4 kB
import{getNamedType as a,GraphQLScalarType as p,isInterfaceType as h,isUnionType as u,Kind as y}from"graphql";import{getTypeField as n,getTypeGroup as g,isExcludedType as c,isFieldsType as f,TypeGroup as o}from"../schema/index.js";class l{schema;referenceMap=new Map;constructor(e){this.schema=e,this.createReferenceMap()}getTypes(e,t){return this.filterByIncluded(this.getSchemaTypes(e),t)}getQueryTypes(e){return this.filterByIncluded(this.schema.getQueryType()?.getFields(),e)}getMutationTypes(e){return this.filterByIncluded(this.schema.getMutationType()?.getFields(),e)}getSubscriptionTypes(e){return this.filterByIncluded(this.schema.getSubscriptionType()?.getFields(),e)}getDirectiveTypes(e){return this.filterByIncluded(this.schema.getDirectives(),e)}getQueryType(){return this.schema.getQueryType()}getQueryField(e){const t=this.getQueryType();return t&&n(t,e)}getMutationType(){return this.schema.getMutationType()}getMutationField(e){const t=this.getMutationType();return t&&n(t,e)}getSubscriptionType(){return this.schema.getSubscriptionType()}getSubscriptionField(e){const t=this.getSubscriptionType();return t&&n(t,e)}getDirective(e){return this.schema.getDirective(e)||void 0}getType(e){return this.schema.getType(e)??void 0}getTypeReferences(e){return this.referenceMap.get(e)}getImplementingTypes(e){const t=this.schema.getType(e);if(!h(t))return;const{objects:s,interfaces:r}=this.schema.getImplementations(t),i=[...s,...r];return i.length>0?i:void 0}filterByIncluded(e,t){const s=Array.isArray(e)?[...e]:Object.values(e??{});return t?s.filter(r=>t.has(r.name)):s}getSchemaTypes(e){return e===y.SCALAR_TYPE_DEFINITION?Object.values(this.schema.getTypeMap()).filter(t=>t instanceof p&&!c(t,this.schema)):Object.values(this.schema.getTypeMap()).filter(t=>t.astNode?.kind===e&&!c(t,this.schema))}createReferenceMap(){const e=this.schema.getTypeMap();Object.values(e).forEach(t=>{!c(t,this.schema)&&f(t)?this.setFieldsTypeReferences(t):u(t)&&this.setUnionTypeReferences(t)})}setFieldsTypeReferences(e){const t=e.getFields();Object.values(t).forEach(s=>{const r=a(s.type),i={name:e.name,field:s.name,group:g(e)};this.setReference(r.name,i)})}setUnionTypeReferences(e){e.getTypes().forEach(t=>{const s=a(t),r={name:e.name,group:o.unions};this.setReference(s.name,r)})}setReference(e,t){const s=this.referenceMap.get(e);s?.length?s.push(t):this.referenceMap.set(e,[t])}}export{l as AppStore};