Skip to content

Simplify operationTypes validation #999

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 3 commits into from
Dec 1, 2017

Conversation

IvanGoncharov
Copy link
Member

Simplify code and more importantly unify some of the exceptions.
@wincent Can you please review it?

@wincent
Copy link
Contributor

wincent commented Aug 22, 2017

Thanks @IvanGoncharov. Will take a look.

@@ -131,6 +131,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
throw new Error('Must provide a document ast.');
}


Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove whitespace

Copy link
Member Author

Choose a reason for hiding this comment

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

@asiandrummer Done 👍

Copy link
Contributor

@asiandrummer asiandrummer left a comment

Choose a reason for hiding this comment

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

Some questions inlined. I agree with the purpose of this PR, but I'm scared this would break some things potentially. Maybe I'm wrong

let queryTypeName;
let mutationTypeName;
let subscriptionTypeName;
let operationTypes = {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Should be const

Copy link
Member Author

Choose a reason for hiding this comment

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

@asiandrummer It's intentional since on line 174:

operationTypes = {};

This is because schema definition is optional but if it present you should use it instead on relaying on type names.

Copy link
Contributor

Choose a reason for hiding this comment

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

const operationTypes  = schemaDef ? { ... } : {};

Above seems better to me, but it's more of a personal pref to me - again it's a nit

Copy link
Member Author

Choose a reason for hiding this comment

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

@asiandrummer Great idea 👍
I refactor out getOperationTypes function so it become:

const operationTypes  = schemaDef ? getOperationTypes(schemaDef) : {
   ... 
};

}

if (!queryTypeName) {
if (!operationTypes['query']) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Query?
Also, we should probably create constants to avoid this type of typos.

@@ -280,8 +255,8 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
});
}

function getObjectType(typeNode: TypeDefinitionNode): GraphQLObjectType {
const type = typeDefNamed(typeNode.name.value);
function getObjectType(name: string): GraphQLObjectType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, this is an API change. Have we done a sweep to see if we're breaking anything? is getObjectType only used in this file?

Copy link
Member Author

Choose a reason for hiding this comment

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

@asiandrummer Yes, since it doesn't have export

subscription: subscriptionTypeName ?
getObjectType(nodeMap[subscriptionTypeName]) :
subscription: operationTypes['subscription'] ?
getObjectType(operationTypes['subscription']) :
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above. Am I misunderstanding something? It looks like we aren't being consistent with the operation names (query vs. Query and etc)

Copy link
Member Author

@IvanGoncharov IvanGoncharov Aug 29, 2017

Choose a reason for hiding this comment

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

@asiandrummer Fully remove strings now it: operationTypes.subscription.

consistent with the operation names (query vs. Query and etc)

It's how spec working, inside schema definition it's query operation but if schema definition is absent then you should look for Query type. So naming is following the specification.

@IvanGoncharov
Copy link
Member Author

I agree with the purpose of this PR, but I'm scared this would break some things potentially. Maybe I'm wrong

@asiandrummer I try to break my changes into small PRs that can be fully reviewed.

Right now I'm working on improving validation of SDL/IDL but before submitting my changes I want to simplify this function. Since if keep adding complexity to graphql-js without simplifying it code will quickly become unreadable.

@asiandrummer
Copy link
Contributor

asiandrummer commented Aug 29, 2017

@IvanGoncharov oh sorry, I meant to approve this PR a while ago. The changes look harmless - just wanted to double check that there are tests covering this part of the code, right?

Thanks for the improvements/contribution as always :D

@IvanGoncharov
Copy link
Member Author

The changes look harmless - just wanted to double check that there are tests covering this part of the code, right?

@asiandrummer Yes, it's fully covered. Here is negative cases, positive cases are spread through the entire file so I can provide a single link for them.

@leebyron
Copy link
Contributor

leebyron commented Dec 1, 2017

Nice work, @IvanGoncharov

@leebyron leebyron merged commit 9da6e06 into graphql:master Dec 1, 2017
@IvanGoncharov IvanGoncharov deleted the addNodesToErrors branch December 14, 2017 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants