0

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.

1 Answers1

1

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.

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64
  • I will try the marking the fields and arguments as options. But similar to the java implementation, do we have something like adding an exclamation mark to the field type (eg. column: String!) in the .graphqls file only when we want to mark a field as non-nullable. Also shouldn't all the fields be nullable by default like in java and other implementations of GraphQl. Is there an alternative approach to defining schema other than the schema specification object ? Use of a .graphqls file schema is way easier. Thanks ! – Shaggy1755 May 12 '20 at 11:02
  • In Scala nobody ever assumes that field is nullable. `null`s are basically only used when interacting with Java, so nobody builds tooling with "nullable by default" in mind. – Mateusz Kubuszok May 12 '20 at 11:42
  • You could generate schema from code (https://github.com/muuki88/sbt-graphql) but I am not aware of doing it the other way round. – Mateusz Kubuszok May 12 '20 at 11:43
  • Thanks @Mateusz!!! Marking the fields as options did work. I do get the required nulls in the response now. But how does the graphiql console schema still show those fields as non-nullable and still send nulls in the response. – Shaggy1755 May 12 '20 at 12:48