A scalar type was returned, but GraphQL was expecting an object

I want to query using custom DQL in GraphQL and return a scalar type:

schema

type URL {
  url: String!
}

type Query {
 co(tp: String!): Int @custom(dql: """
	query q($tp: string) {
		co(func: type($tp)) {
			count(uid)
		}
	}
	""")
}

query:

query MyQuery {
  co(tp: "URL")
}

response:

{
  "errors": [
    {
      "message": "A scalar type was returned, but GraphQL was expecting an object. This indicates an internal error - probably a mismatch between the GraphQL and Dgraph/remote schemas. The value was resolved as null (which may trigger GraphQL error propagation) and as much other data as possible returned.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "co"
      ]
    }
  ],
  "data": {
    "co": null
  },
...
...

Why is GraphQL expecting an object? Does GraphQL support returning scalar types from custom DQL? If so, how?

Thx

Currently, scalar as return types are not supported for custom queries/mutations but we plan to support it soon. There is already a request that we have for this and we’ll pick this up next month.

1 Like

Hey @vinniefg, Since DQL returns the result as a JSON object, We won’t be able to support returning scalar types. If you see the response of this DQL query:-

query{
		co(func: type(URL)) {
			count(uid)
		}
	}

It will be something like this:-

{
  "data": {
    "co": [
      {
        "count": 10
      }
    ]
  }
    ...
    ...
}

Instead, you can define the return type in your schema which should be the return type of your query:-

type URL {
  url: String!
}

type CoResult {
	count: Int
}

type Query {
 co(tp: String!): [CoResult] @custom(dql: """
	query q($tp: string) {
		co(func: type($tp)) {
			count(uid)
		}
	}
	""")
}

and your new query should be:-

query {
  co(tp: "URL"){
    count
  }
}