Custom Query not working with Float and Integer types

I am using dgraph version 20.11.02

I have a simple rest API which returns the following json

{"id":"0x1a","class":"Time","label":"Label","value":10,"standardValue":1.1,"text":"Test Single Reason"}

In my schema, I have the following:

type ReasonTest @remote {
    id : ID!
    class : String!
    label : String!
    text : String!
    value : Int
    standardValue : Float
}

type Query{
    getTest: ReasonTest @custom(http:{
        url:  "http://host.docker.internal:10000/getTest",
        method: GET
    })
}

when I query getTest, I get the string types OK, but float and integer types are null

{
“data”: {
“getTest”: {
“id”: “0x1a”,
“label”: “Label”,
“text”: “Test Single Reason”,
“class”: “Time”,
“value”: null,
“standardValue”: null
}
},
“extensions”: {
“tracing”: {
“version”: 1,
“startTime”: “2021-02-28T01:56:18.5597489Z”,
“endTime”: “2021-02-28T01:56:18.5803396Z”,
“duration”: 20618800
}
}
}

1 Like

Hi @Geoff.Nunan, Welcome to Dgraph community!!
Ideally it shouldn’t happen. Can you please provide some more details like how you are constructing and returning response from rest API.
I am able to get even the Int and float in response with our implementation of rest API.

Hi @JatinDevDG ,

I have implemented a very simple service in Golang that populates the following response variable

	var response []struct{
		Id graphql.ID `json:"id"`
		Class graphql.String `json:"class"`
		Label graphql.String `json:"label"`
		Text graphql.String `json:"text"`
		StandardValue graphql.Float `json:"standardValue"`
		EquipmentClass struct {
			Id graphql.ID `json:"id"`
			Name graphql.String `json:"name"`
		} `json:"equipmentClass"`
			EquipmentOverrides [] struct {
			Id graphql.ID `json:"id"`
			StandardValue graphql.Float `json:"standardValue"`
			Ignore graphql.Boolean `json:"ignore"`
		}`json:"equipmentOverrides"`
	}

Which then gets returned in the rest handler method using

json.NewEncoder(w).Encode(response)

I have tried to build a very simple schema and service, and the simple one works fine.

As my schema gets more complex, some fields return null.

Interestingly, when this occurs, if I rename the field in the schema and in the service it starts working again. I am literally just renaming the field that is returning null with no other changes.

@JatinDevDG I have isolated the bug.

It seems that having multiple types implementing an interface breaks @custom queries.

Steps to reproduce:
1- Run the dgraph standalone docker container.
2 - publich the following schema into it

type Item implements Base{
    name:String
    value:Float
}
type Order implements Base{
    name:String
    value:Float
}
interface Base {
    id:ID!
}
type Query{
    getCustomTest:[Item] @custom(http:{
        url:  "http://host.docker.internal:8080/getTest",
        method: GET
    })
}

3- Write a simple golang service at the /getTest endpoint like this.

func getTest(w http.ResponseWriter, r *http.Request) {

	response := []domain.Item {{
		Id:"0x1",
		Name:"01",
		Value: 1,
	},{

		Id:"0x2",
		Name:"02",
		Value: 2,
	}}
	log.Print(response)
	json.NewEncoder(w).Encode(response)
}

4 - using GraphQL playground, query

query{
  getCustomTest{
    id
    name
    value
  }
}

I am getting the following response

{
  "data": {
    "getCustomTest": [
      {
        "id": "0x1",
        "name": null,
        "value": null
      },
      {
        "id": "0x2",
        "name": null,
        "value": null
      }
    ]
  },
  "extensions": {}
}

5 - Edit the schema to remove the interface like this:

type Item {
    id:ID!
    name:String
    value:Float
}
type Order {
    id:ID!
    name:String
    value:Float
}
type Query{
    getCustomTest:[Item] @custom(http:{
        url:  "http://host.docker.internal:8080/getTest",
        method: GET
    })
}

Run the query again and you get the proper response

{
  "data": {
    "getCustomTest": [
      {
        "id": "0x1",
        "name": "01",
        "value": 1
      },
      {
        "id": "0x2",
        "name": "02",
        "value": 2
      }
    ]
  },
  "extensions": {}
}
3 Likes

Hi @Geoff.Nunan , Thanks for detailed description of the bug. This bug is already fixed but not available in 20.11.02. We had similar kind of bug previously also Custom field resolvers are not always called

We made many changes related to JSON encoding in master branch which fix such type of bugs.
These changes will be part of 21.03 release and currently available only in master branch only.
PR:perf(GraphQL): Generate GraphQL query response by optimized JSON encoding (GraphQL-730) by abhimanyusinghgaur · Pull Request #7371 · dgraph-io/dgraph · GitHub

1 Like