UNPKG

1.26 kBJavaScriptView Raw
1import { GraphQLError } from '../error/GraphQLError.mjs';
2
3/**
4 * Extracts the root type of the operation from the schema.
5 *
6 * @deprecated Please use `GraphQLSchema.getRootType` instead. Will be removed in v17
7 */
8export function getOperationRootType(schema, operation) {
9 if (operation.operation === 'query') {
10 const queryType = schema.getQueryType();
11
12 if (!queryType) {
13 throw new GraphQLError(
14 'Schema does not define the required query root type.',
15 {
16 nodes: operation,
17 },
18 );
19 }
20
21 return queryType;
22 }
23
24 if (operation.operation === 'mutation') {
25 const mutationType = schema.getMutationType();
26
27 if (!mutationType) {
28 throw new GraphQLError('Schema is not configured for mutations.', {
29 nodes: operation,
30 });
31 }
32
33 return mutationType;
34 }
35
36 if (operation.operation === 'subscription') {
37 const subscriptionType = schema.getSubscriptionType();
38
39 if (!subscriptionType) {
40 throw new GraphQLError('Schema is not configured for subscriptions.', {
41 nodes: operation,
42 });
43 }
44
45 return subscriptionType;
46 }
47
48 throw new GraphQLError(
49 'Can only have query, mutation and subscription operations.',
50 {
51 nodes: operation,
52 },
53 );
54}