Skip to content

Expose getOperationRootType(schema, operationNode) #1345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 1 addition & 41 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import promiseReduce from '../jsutils/promiseReduce';
import type { ObjMap } from '../jsutils/ObjMap';
import type { MaybePromise } from '../jsutils/MaybePromise';

import { getOperationRootType } from '../utilities/getOperationRootType';
import { typeFromAST } from '../utilities/typeFromAST';
import { Kind } from '../language/kinds';
import {
Expand Down Expand Up @@ -415,47 +416,6 @@ function executeOperation(
}
}

/**
* Extracts the root type of the operation from the schema.
*/
export function getOperationRootType(
schema: GraphQLSchema,
operation: OperationDefinitionNode,
): GraphQLObjectType {
switch (operation.operation) {
case 'query':
const queryType = schema.getQueryType();
if (!queryType) {
throw new GraphQLError(
'Schema does not define the required query root type.',
[operation],
);
}
return queryType;
case 'mutation':
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError('Schema is not configured for mutations.', [
operation,
]);
}
return mutationType;
case 'subscription':
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError('Schema is not configured for subscriptions.', [
operation,
]);
}
return subscriptionType;
default:
throw new GraphQLError(
'Can only execute queries, mutations and subscriptions.',
[operation],
);
}
}

/**
* Implements the "Evaluating selection sets" section of the spec
* for "write" mode.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ export {
introspectionQuery,
// 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.
Expand Down
2 changes: 1 addition & 1 deletion src/subscription/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
collectFields,
execute,
getFieldDef,
getOperationRootType,
resolveFieldValueOrError,
responsePathAsArray,
} from '../execution/execute';
Expand All @@ -29,6 +28,7 @@ import type { ObjMap } from '../jsutils/ObjMap';
import type { ExecutionResult } from '../execution/execute';
import type { DocumentNode } from '../language/ast';
import type { GraphQLFieldResolver } from '../type/definition';
import { getOperationRootType } from '../utilities/getOperationRootType';

/**
* Implements the "Subscribe" algorithm described in the GraphQL specification.
Expand Down
136 changes: 136 additions & 0 deletions src/utilities/__tests__/getOperationRootType-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { getOperationRootType } from '../getOperationRootType';
import { parse, GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../';

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 },
}),
});

describe('getOperationRootType', () => {
it('Gets a Query type for an unnamed OperationDefinitionNode', () => {
const testSchema = new GraphQLSchema({
query: queryType,
});
const doc = parse('{ field }');
expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal(
queryType,
);
});

it('Gets a Query type for an named OperationDefinitionNode', () => {
const testSchema = new GraphQLSchema({
query: queryType,
});

const doc = parse('query Q { field }');
expect(getOperationRootType(testSchema, doc.definitions[0])).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 operationTypes = doc.definitions[0].operationTypes;
expect(getOperationRootType(testSchema, operationTypes[0])).to.equal(
queryType,
);
expect(getOperationRootType(testSchema, operationTypes[1])).to.equal(
mutationType,
);
expect(getOperationRootType(testSchema, operationTypes[2])).to.equal(
subscriptionType,
);
});

it('Gets a Mutation type for an OperationDefinitionNode', () => {
const testSchema = new GraphQLSchema({
mutation: mutationType,
});

const doc = parse('mutation { field }');
expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal(
mutationType,
);
});

it('Gets a Subscription type for an OperationDefinitionNode', () => {
const testSchema = new GraphQLSchema({
subscription: subscriptionType,
});

const doc = parse('subscription { field }');
expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal(
subscriptionType,
);
});

it('Throws when query type not defined in schema', () => {
const testSchema = new GraphQLSchema({});

const doc = parse('query { field }');
expect(() => getOperationRootType(testSchema, doc.definitions[0])).to.throw(
'Schema does not define the required query root type.',
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rewrite it as:

expect(() => getOperationRootType(testSchema, doc.definitions[0]))
  .to.throw()
  .deep.equal({
    descripton: 'Schema does not define the required query root type.',
    locations: [/* */],
  });

That way your also testing that errors are binded to correct location.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good suggestion, though I'm having a hard time finding other examples of this being done. I'm not really familiar with Chai, so I'm OK not being the first person to use all its best features. Additionally, this feels closer to "testing the current API and internal structure for GraphQLError" instead of "testing getOperationRootType". Basically, if we make a change to one of GraphQLError's properties, I don't want the person doing that to have to figure out why this test is failing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjmahone You right I run 'grep' and there are no such checks in other places.
I still think it's important to test that errors are bind to correct location.
But you right such tests shouldn't be very dependent on GraphQLError 👍

});

it('Throws when mutation type not defined in schema', () => {
const testSchema = new GraphQLSchema({});

const doc = parse('mutation { field }');
expect(() => getOperationRootType(testSchema, doc.definitions[0])).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 }');
expect(() => getOperationRootType(testSchema, doc.definitions[0])).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 }');
doc.definitions[0].operation = 'non_existent_operation';
expect(() => getOperationRootType(testSchema, doc.definitions[0])).to.throw(
'Can only have query, mutation and subscription operations.',
);
});
});
57 changes: 57 additions & 0 deletions src/utilities/getOperationRootType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

import { GraphQLError } from '../error/GraphQLError';
import type {
OperationDefinitionNode,
OperationTypeDefinitionNode,
} from '../language/ast';
import { GraphQLSchema } from '../type/schema';
import type { GraphQLObjectType } from '../type/definition';

/**
* Extracts the root type of the operation from the schema.
*/
export function getOperationRootType(
schema: GraphQLSchema,
operation: OperationDefinitionNode | OperationTypeDefinitionNode,
): GraphQLObjectType {
switch (operation.operation) {
case 'query':
const queryType = schema.getQueryType();
if (!queryType) {
throw new GraphQLError(
'Schema does not define the required query root type.',
[operation],
);
}
return queryType;
case 'mutation':
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError('Schema is not configured for mutations.', [
operation,
]);
}
return mutationType;
case 'subscription':
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError('Schema is not configured for subscriptions.', [
operation,
]);
}
return subscriptionType;
default:
throw new GraphQLError(
'Can only have query, mutation and subscription operations.',
[operation],
);
}
}
3 changes: 3 additions & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ 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';

Expand Down