Skip to content

Using Option in fields fails in the None case #967

@anderslm

Description

@anderslm

If a field has an Option as it's value, eg:

  val FieldWithOption = ObjectType[Unit, Option[String]](
    "Option", 
    fields[Unit, Option[String]](Field("hello", StringType, resolve = _.value.getOrElse("Hello, world!"))))

  val QueryTypeThatFails = ObjectType("Query", fields[Unit, Unit](
    Field("hello", FieldWithOption, resolve = _ => None)
  ))

When this field is queried in the Some case it works, but in the None case it fails at runtime, when queried, with:

{
  "data" : null,
  "errors" : [
    {
      "message" : "Cannot return null for non-nullable type (line 1, column 3):\n{ hello { hello } }\n  ^",
      "path" : [
        "hello"
      ],
      "locations" : [
        {
          "line" : 1,
          "column" : 3
        }
      ]
    }
  ]
}

The complete example can be found here https://github.com/anderslm/sangria-option/

I'm guessing this is why it happens:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions