UNPKG

1.24 kBJavaScriptView Raw
1import { memoize1 } from './memoize.js';
2export function getDefinedRootType(schema, operation) {
3 const rootTypeMap = getRootTypeMap(schema);
4 const rootType = rootTypeMap.get(operation);
5 if (rootType == null) {
6 throw new Error(`Root type for operation "${operation}" not defined by the given schema.`);
7 }
8 return rootType;
9}
10export const getRootTypeNames = memoize1(function getRootTypeNames(schema) {
11 const rootTypes = getRootTypes(schema);
12 return new Set([...rootTypes].map(type => type.name));
13});
14export const getRootTypes = memoize1(function getRootTypes(schema) {
15 const rootTypeMap = getRootTypeMap(schema);
16 return new Set(rootTypeMap.values());
17});
18export const getRootTypeMap = memoize1(function getRootTypeMap(schema) {
19 const rootTypeMap = new Map();
20 const queryType = schema.getQueryType();
21 if (queryType) {
22 rootTypeMap.set('query', queryType);
23 }
24 const mutationType = schema.getMutationType();
25 if (mutationType) {
26 rootTypeMap.set('mutation', mutationType);
27 }
28 const subscriptionType = schema.getSubscriptionType();
29 if (subscriptionType) {
30 rootTypeMap.set('subscription', subscriptionType);
31 }
32 return rootTypeMap;
33});