graphql / graphql-js

A reference implementation of GraphQL for JavaScript
http://graphql.org/graphql-js/
MIT License
20.06k stars 2.02k forks source link

Bug in buildASTSchema when mutations are passed #100

Closed nemanja-stanarevic closed 9 years ago

nemanja-stanarevic commented 9 years ago

All, great work all around on graphql reference implementation, it's incredibly helpful.

I have been using the schema DSL parser and buildASTSchema to materialize the schema and noticed a small bug in utilities/buildASTSchema. Specifically, when both queryTypeName and mutationTypeName are passed to the buildASTSchema, the schema is not constructed correctly - query is missing.

I traced the bug down to the lines 150-157 of utilities/buildASTSchema, where if mutationTypeName is passed, schema object is constructed with the queryTypeName string instead of queryType object:

  if (isNullish(mutationTypeName)) {
    schema = new GraphQLSchema({query: queryType});
  } else {
    schema = new GraphQLSchema({
      query: queryTypeName,
      mutation: produceTypeDef(astMap[mutationTypeName]),
    });
  }

This should fix it -

  if (isNullish(mutationTypeName)) {
    schema = new GraphQLSchema({query: queryType});
  } else {
    schema = new GraphQLSchema({
      query: queryType,
      mutation: produceTypeDef(astMap[mutationTypeName]),
    });
  }

Note that this slipped since buildASTSchema tests don't include a case when both mutation and query are passed. I am happy to fix this, add a test and submit a PR, if welcome.

dschafer commented 9 years ago

Thanks for the report and PR! Just pushed 0.2.4 which should contain a fix for this issue!