Dgraph returning empty string instead of null for time

To facilitate better answering of questions, if you have a question, please fill in the following info. Otherwise, please delete the template.

What I want to do

When querying dgraph, through GraphQL playground, I see the following:

Query:
{“operationName”:null,“variables”:{“input”:[{“name”:“brian”}]},“query”:“mutation ($input: [AddPersonInput!]!) {\n addPerson(input: $input) {\n person {\n name\n birthName\n birthDate\n deathDate\n }\n }\n}\n”}

Response:
{“data”:{“addPerson”:{“person”:[{“name”:“brian”,“birthName”:null,“birthDate”:null,“deathDate”:null}]}}}

However when querying the same request through Go, with the same query I get the following response:

{“data”:{“addPerson”:{“person”:[{“name”:“brian”,“birthName”:"",“birthDate”:"",“deathDate”:""}]}}}

This causes issues as when unmarshalling the json into a struct, it produces an error when parsing time:

parsing time “”"" as "“2006-01-02T15:04:05Z07:00"”: cannot parse

GraphQL schema:

type Person {
name: String
birthName: String
birthDate: DateTime
deathDate: DateTime
}

I want Dgraph to respond with null instead of an empty string

What I did

I have tried mimicking the network request from Graphql playground with identical headers, however cannot seem to get Dgraph to respond with null instead of “”.

Dgraph metadata

dgraph version

Dgraph version v20.03.0 running in a docker container

To avoid this you can use the graphql default value see this link GraphQL. In the case of the date, you should use a value there instead of null or empty.

I don’t think GraphQL mutation default input variables is what is being looked for. Maybe a better understanding of the go client being used might help (not a golang dev myself). I think what is being sought is how to get nil values from the response in golang from the GraphQL endpoint of Dgraph.

There should be some experienced golang developers here maybe @matthewmcneely might know??

It looks like Dgraph is returning null values as expected. The problem most likely is in the JSON decoding of null to the non-pointer values into your Golang struct. I’m guessing your Golang struct has time.Time members for those dates.

One way that I “get around this”, is to use pointers (*time.Time) for the time struct members so that when Golang decodes the JSON returned from Dgraph, it correctly assigns nil to those values.

2 Likes

Thanks for all your suggestions.

Have tried using *time.Time but appeared to have the same issue

Have looked into this further and the following appears

  • When setting date time through GQL/DQL as “0001-01-01T00:00:00Z” (Golang JSON zero value)
  • Dgraph returns "" instead of 0001-01-01T00:00:00Z or null

Is this the intended behaviour?

Looks like dgraph-io/dgraph#3169 added the behaviour to return an empty string for 0 time

and dgraph-io/dgraph#6067 fixed it

however it does not appear to be fixed for me

Seems as those from 6067 that it should be returning golang “empty” time.

I’d recommend that you create a small test of this in a repo or gist to hand over to the Dgraph team. For golang tests, there’s a dgraph docker helper that makes it easy to write a complete E2E test in one file.