diff --git a/src/index.ts b/src/index.ts index 8ecd76d34a..5bbf0da3a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -402,8 +402,6 @@ export { getIntrospectionQuery, // Gets the target Operation from a Document. getOperationAST, - // Gets the Type for the target Operation AST. - getOperationRootType, // Convert a GraphQLSchema to an IntrospectionQuery. introspectionFromSchema, // Build a GraphQLSchema from an introspection result. diff --git a/src/utilities/__tests__/getOperationRootType-test.ts b/src/utilities/__tests__/getOperationRootType-test.ts deleted file mode 100644 index ce683a5a12..0000000000 --- a/src/utilities/__tests__/getOperationRootType-test.ts +++ /dev/null @@ -1,159 +0,0 @@ -import { expect } from 'chai'; -import { describe, it } from 'mocha'; - -import { invariant } from '../../jsutils/invariant'; - -import type { DocumentNode, OperationDefinitionNode } from '../../language/ast'; -import { Kind } from '../../language/kinds'; -import { parse } from '../../language/parser'; - -import { GraphQLObjectType } from '../../type/definition'; -import { GraphQLString } from '../../type/scalars'; -import { GraphQLSchema } from '../../type/schema'; - -import { getOperationRootType } from '../getOperationRootType'; - -const queryType = new GraphQLObjectType({ - name: 'FooQuery', - fields: () => ({ - field: { type: GraphQLString }, - }), -}); - -const mutationType = new GraphQLObjectType({ - name: 'FooMutation', - fields: () => ({ - field: { type: GraphQLString }, - }), -}); - -const subscriptionType = new GraphQLObjectType({ - name: 'FooSubscription', - fields: () => ({ - field: { type: GraphQLString }, - }), -}); - -function getOperationNode(doc: DocumentNode): OperationDefinitionNode { - const operationNode = doc.definitions[0]; - invariant(operationNode.kind === Kind.OPERATION_DEFINITION); - return operationNode; -} - -describe('Deprecated - getOperationRootType', () => { - it('Gets a Query type for an unnamed OperationDefinitionNode', () => { - const testSchema = new GraphQLSchema({ - query: queryType, - }); - const doc = parse('{ field }'); - const operationNode = getOperationNode(doc); - expect(getOperationRootType(testSchema, operationNode)).to.equal(queryType); - }); - - it('Gets a Query type for an named OperationDefinitionNode', () => { - const testSchema = new GraphQLSchema({ - query: queryType, - }); - - const doc = parse('query Q { field }'); - const operationNode = getOperationNode(doc); - expect(getOperationRootType(testSchema, operationNode)).to.equal(queryType); - }); - - it('Gets a type for OperationTypeDefinitionNodes', () => { - const testSchema = new GraphQLSchema({ - query: queryType, - mutation: mutationType, - subscription: subscriptionType, - }); - - const doc = parse(` - schema { - query: FooQuery - mutation: FooMutation - subscription: FooSubscription - } - `); - - const schemaNode = doc.definitions[0]; - invariant(schemaNode.kind === Kind.SCHEMA_DEFINITION); - const [queryNode, mutationNode, subscriptionNode] = - schemaNode.operationTypes; - - expect(getOperationRootType(testSchema, queryNode)).to.equal(queryType); - expect(getOperationRootType(testSchema, mutationNode)).to.equal( - mutationType, - ); - expect(getOperationRootType(testSchema, subscriptionNode)).to.equal( - subscriptionType, - ); - }); - - it('Gets a Mutation type for an OperationDefinitionNode', () => { - const testSchema = new GraphQLSchema({ - mutation: mutationType, - }); - - const doc = parse('mutation { field }'); - const operationNode = getOperationNode(doc); - expect(getOperationRootType(testSchema, operationNode)).to.equal( - mutationType, - ); - }); - - it('Gets a Subscription type for an OperationDefinitionNode', () => { - const testSchema = new GraphQLSchema({ - subscription: subscriptionType, - }); - - const doc = parse('subscription { field }'); - const operationNode = getOperationNode(doc); - expect(getOperationRootType(testSchema, operationNode)).to.equal( - subscriptionType, - ); - }); - - it('Throws when query type not defined in schema', () => { - const testSchema = new GraphQLSchema({}); - - const doc = parse('query { field }'); - const operationNode = getOperationNode(doc); - expect(() => getOperationRootType(testSchema, operationNode)).to.throw( - 'Schema does not define the required query root type.', - ); - }); - - it('Throws when mutation type not defined in schema', () => { - const testSchema = new GraphQLSchema({}); - - const doc = parse('mutation { field }'); - const operationNode = getOperationNode(doc); - expect(() => getOperationRootType(testSchema, operationNode)).to.throw( - 'Schema is not configured for mutations.', - ); - }); - - it('Throws when subscription type not defined in schema', () => { - const testSchema = new GraphQLSchema({}); - - const doc = parse('subscription { field }'); - const operationNode = getOperationNode(doc); - expect(() => getOperationRootType(testSchema, operationNode)).to.throw( - 'Schema is not configured for subscriptions.', - ); - }); - - it('Throws when operation not a valid operation kind', () => { - const testSchema = new GraphQLSchema({}); - const doc = parse('{ field }'); - const operationNode: OperationDefinitionNode = { - ...getOperationNode(doc), - // @ts-expect-error - operation: 'non_existent_operation', - }; - - expect(() => getOperationRootType(testSchema, operationNode)).to.throw( - 'Can only have query, mutation and subscription operations.', - ); - }); -}); diff --git a/src/utilities/getOperationRootType.ts b/src/utilities/getOperationRootType.ts deleted file mode 100644 index db20a793a8..0000000000 --- a/src/utilities/getOperationRootType.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { GraphQLError } from '../error/GraphQLError'; - -import type { - OperationDefinitionNode, - OperationTypeDefinitionNode, -} from '../language/ast'; - -import type { GraphQLObjectType } from '../type/definition'; -import type { GraphQLSchema } from '../type/schema'; - -/** - * Extracts the root type of the operation from the schema. - * - * @deprecated Please use `GraphQLSchema.getRootType` instead. Will be removed in v17 - */ -export function getOperationRootType( - schema: GraphQLSchema, - operation: OperationDefinitionNode | OperationTypeDefinitionNode, -): GraphQLObjectType { - if (operation.operation === 'query') { - const queryType = schema.getQueryType(); - if (!queryType) { - throw new GraphQLError( - 'Schema does not define the required query root type.', - { nodes: operation }, - ); - } - return queryType; - } - - if (operation.operation === 'mutation') { - const mutationType = schema.getMutationType(); - if (!mutationType) { - throw new GraphQLError('Schema is not configured for mutations.', { - nodes: operation, - }); - } - return mutationType; - } - - if (operation.operation === 'subscription') { - const subscriptionType = schema.getSubscriptionType(); - if (!subscriptionType) { - throw new GraphQLError('Schema is not configured for subscriptions.', { - nodes: operation, - }); - } - return subscriptionType; - } - - throw new GraphQLError( - 'Can only have query, mutation and subscription operations.', - { nodes: operation }, - ); -} diff --git a/src/utilities/index.ts b/src/utilities/index.ts index 452b975233..277bc2d715 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -29,9 +29,6 @@ export type { // Gets the target Operation from a Document. export { getOperationAST } from './getOperationAST'; -// Gets the Type for the target Operation AST. -export { getOperationRootType } from './getOperationRootType'; - // Convert a GraphQLSchema to an IntrospectionQuery. export { introspectionFromSchema } from './introspectionFromSchema';