Suppose this is the schema:
type Actor {
actorId: ID!
name: String
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
}
type Movie {
movieId: ID!
title: String
description: String
year: Int
actors(limit: Int = 10): [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
}
Now, let's suppose I want top movies with most actors. This is a solution with map projection.
type Query {
getMoviesWithMostActors(limit: Int = 5): [Movie]
(
statement: """
MATCH (movie:Movie)
MATCH (movie) <-[act:ACTED_IN]- (:Actor)
WITH movie, count(act) AS actorCount
ORDER BY actorCount DESCENDING
LIMIT $limit
RETURN movie {.*, numActors: actorCount}
"""
)
}
The query itself runs fine in Neo4j Browser, but GraphQL can't use it.
Suppose I use the following in GraphQL playground:
query {
this_works: getMoviesWithMostActorsbase(limit: 2) {
movieId
}
this_does_not_work: getMoviesWithMostActorsbase(limit: 2) {
movieId
numActors
}
}
It fails with GRAPHQL_VALIDATION_FAILED.
"GraphQLError: Cannot query field \"numActors\" on type \"Movie\"."
I can extend the Movie type with a new property, but I will not need it anywhere else. Also, I have a lot of queries, and all with their specific extra properties. Also, in my actual database, there will be queries on other type of nodes too.
So, I want to know if there is a way to temporarily add a property for GraphQL to process. Or, is there a better way handle this situation in a general way (and not just this dummy example)?
I am also open to suggestions with returning a map, if it somehow avoids creation of new types in every single cases, similar to this:
type Query {
getMoviesWithMostActors(limit: Int = 5): [Movie]
(
statement: """
MATCH (movie:Movie)
MATCH (movie) <-[act:ACTED_IN]- (:Actor)
WITH movie, count(act) AS actorCount
ORDER BY actorCount DESCENDING
LIMIT $limit
RETURN {movieData: movie, movieScore: actorCount}
"""
)
}