Search code examples
scalagraphqlgraphiqlsangria

All fields and parameters marked as non-null in Graphiql : Sangria, scala


I am not able to use postman to query a graphql server written in scala using its sangria library. I get a request malformed error in postman if I use the content-type as "application-json" and if I use plain text I get content type not supported. The postman version I am using is Version 6.2.4. The code is pretty straightforward in which I read data from hbase. I use the sangria macro function deriveObjectTypeUnit, myCaseClass to define an object. I am however able to use the graphiql console by concatenating a graphiql.html file from the resources to the route created. The issue with the graphiql console is that it marks all the arguments and fields as non-nullable which it should not as by default the fields are all nullable in graphql. I checked this from the documentation tab of the graphiql console where I can see all my fields and the parameters are marked as non-nullable(Suffixed with an exclamation mark !). Sample query is as follows:

{
  hBaseTable(date: "2019-11-21", key: "10100003071234") {
    RowKey
    DateOfInt
  }
}

My question is how do we set the default nullability in sangria and how does graphql java supports the postman version 6 but not scala implementation.


Solution

  • Field might by nullable by default in GraphQL schemas but in Scala nulls are almost always programming errors (and surely always if you pass or return them as parameters) - you want nullable, you explicitly model them with Option. If you want to stick to GraphQL convention just write all fields in your case classes as Optional.