Skip to content

Commit d8da198

Browse files
committed
Expose getOperationType(operation) on GraphQLSchema
We're finding we frequently do switch statements on an OperationDefinition's operation to pull the right type out of the schema. This hardens that approach: if for some reason we added a new operation kind, you could just update this function, rather than all the callsites that match on the operation type.
1 parent 2eccaad commit d8da198

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/type/schema.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
GraphQLObjectType,
2323
} from './definition';
2424
import type {
25+
OperationTypeNode,
2526
SchemaDefinitionNode,
2627
SchemaExtensionNode,
2728
} from '../language/ast';
@@ -172,6 +173,17 @@ export class GraphQLSchema {
172173
});
173174
}
174175

176+
getOperationType(operation: OperationTypeNode): ?GraphQLObjectType {
177+
switch (operation) {
178+
case 'query':
179+
return this.getQueryType();
180+
case 'mutation':
181+
return this.getMutationType();
182+
case 'subscription':
183+
return this.getSubscriptionType();
184+
}
185+
}
186+
175187
getQueryType(): ?GraphQLObjectType {
176188
return this._queryType;
177189
}

0 commit comments

Comments
 (0)