UNPKG

@redocly/graphql-docs

Version:

Redocly GraphQL docs

2 lines (1 loc) 2.21 kB
import{getNamedType as c,GraphQLScalarType as p,isUnionType as h,Kind as u}from"graphql";import{getTypeField as i,getTypeGroup as y,isExcludedType as n,isFieldsType as g,TypeGroup as T}from"../schema/index.js";class d{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&&i(t,e)}getMutationType(){return this.schema.getMutationType()}getMutationField(e){const t=this.getMutationType();return t&&i(t,e)}getSubscriptionType(){return this.schema.getSubscriptionType()}getSubscriptionField(e){const t=this.getSubscriptionType();return t&&i(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)}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===u.SCALAR_TYPE_DEFINITION?Object.values(this.schema.getTypeMap()).filter(t=>t instanceof p&&!n(t,this.schema)):Object.values(this.schema.getTypeMap()).filter(t=>t.astNode?.kind===e&&!n(t,this.schema))}createReferenceMap(){const e=this.schema.getTypeMap();Object.values(e).forEach(t=>{!n(t,this.schema)&&g(t)?this.setFieldsTypeReferences(t):h(t)&&this.setUnionTypeReferences(t)})}setFieldsTypeReferences(e){const t=e.getFields();Object.values(t).forEach(s=>{const r=c(s.type),a={name:e.name,field:s.name,group:y(e)};this.setReference(r.name,a)})}setUnionTypeReferences(e){e.getTypes().forEach(t=>{const s=c(t),r={name:e.name,group:T.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{d as AppStore};