GraphQL Tour: Interfaces and Unions
Clay Allsopp
6627

Why not introduce conditional query? I tired of lack features of GraphQL. I write an alternative myself; I called it NextQL. NextQL implement conditional queries; I think it more powerful than GraphQL interfaces. The idea that have resolvers which return a type if conditionals meet.

{ 
    // conditional type resolver for person  
"?person": (source, params) => {
if (source instanceof Person) {
return "person";
}
    },
"?drone": (source, params) => {
if (source instanceof Drone) {
return "drone";
}
}
}

And client could call the resolver to perform conditional query. If type of object is person, query “fullName”; if drone query “idName” field.

{
"?drone": {
idName: 1
},
   "?person": {
fullName: 1
}
}

Conditional resolver more powerful because you could call them with params — so you could have a generic conditional like “?model” which resolve to any type.

"?model": (source, params) => {
    if (source && (source.constructor.name == params.name)) {
return source.constructor.name;
}
}
Like what you read? Give Giap Nguyen a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.