-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
); | ||
}); | ||
|
||
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.', | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
That way your also testing that errors are binded to correct location.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
👍