graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

Validations are unable to return proper error messages with graphql v16 #392

Open jeanlescure opened 2 years ago

jeanlescure commented 2 years ago

I have started a new project, when I added graphql it installed version ^16.0.1.

In my project I have a mongoose.Schema that validates an email string, the validation itself works, but when the mutation is given an invalid entry rather than returning the validation error message, I'm getting the following error:

{
  "errors": [
    {
      "message": "Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.",
      "locations": [
        {
          "line": 7,
          "column": 5
        }
      ],
      "path": [
        "userCreateOne",
        "error"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "message": "Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.",
          "stacktrace": [
            "GraphQLError: Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.",
            "    at ensureValidRuntimeType (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:816:11)",
            "    at completeAbstractValue (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:784:5)",
            "    at completeValue (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:612:12)",
            "    at executeField (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:477:19)",
            "    at executeFields (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:401:20)",
            "    at completeObjectValue (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:895:10)",
            "    at completeValue (./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:624:12)",
            "    at ./node_modules/.pnpm/graphql@16.0.1/node_modules/graphql/execution/execute.js:474:9",
            "    at processTicksAndRejections (internal/process/task_queues.js:95:5)",
            "    at execute (./node_modules/.pnpm/apollo-server-core@3.5.0_graphql@16.0.1/node_modules/apollo-server-core/src/requestPipeline.ts:485:14)"
          ]
        }
      }
    }
  ],
  "data": {
    "userCreateOne": {
      "recordId": null,
      "record": null,
      "error": null
    }
  }
}

If I downgrade graphql to ^15.5.0, then I get a proper validation error message:

{
  "data": {
    "userCreateOne": {
      "recordId": null,
      "record": null,
      "error": {
        "message": "User validation failed: email: Path `email` is invalid (example.com)."
      }
    }
  }
}

I am guessing that the composed resolver returns a GraphQLObjectType as the resolveType when an error occurs, and as the error points out:

Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.