maticzav / graphql-middleware

Split up your GraphQL resolvers in middleware functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Path and locations info improvement when type error returned

ph55 opened this issue · comments

Using this library for permission management.
But there is some misleading info in path when validation of type failed.

Example.

Schema:

type Query {
  user(id: Int!): User
}

type User {
  id: Int!
  email: String!
  username: String!
}

Apply middleware on User type:

schema = applyMiddleware(schema, {
  User: () => new ForbiddenError(`Access denied`)
})

then following query (that return User type)

{
  user(id: 1) {
    username
    email
  }
}

throwing error:

{
  "errors": [
    {
      "message": "Access denied",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "user",
        "username"
      ],
      "extensions": {
        "code": "FORBIDDEN"
      }
    }
  ],
  "data": {
    "user": null
  }
}

Look at locations and path - it includes first queried attribute of User (username). Which is misleading.
Ideally it should show information regarding user query only:

"locations": [
  {
    "line": 2,
    "column": 3
  }
],
"path": [
  "user"
]

Basically following middlewares currently return same path and location in error when they shouldn't:

[
  {
    User: () => new ForbiddenError(`Access denied`)
  },
  {
     User: {
       username: () => new ForbiddenError(`Access denied`)
     }
   }
]
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.