Possible Alternative for countData type queries
The method to have separate queries of the type countData has the following drawbacks:
- The query has only one field in their return type which is count of UIDs matching filter. It cannot have any other fields of the
Datatype. - For most purposes, a
countDataquery will have to be accompanied byqueryDataquery. AscountDataquery does not return anything more than count of UIDs. It may not prove that useful as it is intended to be.
To solve this problem of countData type query, the alternative is to make all queryData type queries return an extra field count . This field will contain the count of UIDs satisfying the given filter condition.
This approach has the following advantages:
- Any other fields of Data type could also be queried along with this
countvariable. Making it easier to get all needed data using a single queryData query rather than using 2 queries,countDataandqueryData. - This way of having a
countvariable inqueryDatais more consistent with the waycountis handled in DQL. There is no separatecountDataquery in DQL. Rather, acount(UID)variable is present which could be used to get a count of matching UIDs. This will make rewriting to DQL easier.
This approach has the following disadvantage:
- This will be a breaking change as the return type of queries of the form,
queryDatawill change from[Data]to a structure containingint(to store count) andData. This will have to be implemented and tested with greater care to avoid any problems with current structure.
Sample Example:
type Data {
id: ID!
name: String!
intList: [Int]
stringList: [String]
metaData: [Data]
}
The following queryData query will be generated in output schema from the above input schema.
query {
queryData(filter: DataFilter): (Int, [Data])
}
Example GraphQL count query:
query {
queryData(filter: \*Some Filter*\) {
count
name
stringList
}
}
The above GraphQL query would be rewritten to the following DQL query.
query {
queryData(func: type(Data)) @filter(/* rewritten filter condtition */)) {
count: count(uid)
name
stringList
}
}
@amaster507, I will like to hear your comments about this and whether this solves some of the concerns mentioned by you.