UNPKG

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