I had a problem trying to extend the API to include the GraphQL endpoint. The application I'm working on is a kind of forum with Messages. A message may contain type comments Message. If the post is a comment, it has a parent type Message. Simplified, the scheme looks like this:
type Message {
id: String
content: String
comments: [Message]
parent: Message
}
type RootQuery {
message(id: String): Message
messages: [Message]
}
The problem with this scheme is that it allows you to query such requests:
{
messages {
comments {
parent {
comments {
parent {
comments {
parent {
id
content
}
}
}
}
}
}
}
}
Keep in mind that I can allow arbitrarily deep nesting of comments. In this case, the following request should be allowed:
{
messages {
comments {
comments {
comments {
id
content
}
}
}
}
}
So my question is this: should I introduce a new type - comment - an API that does not know its parent? Or are there other ways to limit this kind of unwanted behavior?
, Comment fragment messageFields on Message ? , ?
, ( ):
interface Message {
id: String
content: String
comments: [Message]
}
type DefaultMessage : Message {
id: String
content: String
comments: [Comment]
parent: Message
}
type Comment : Message {
id: String
content: String
comments: [Message]
}
type RootQuery {
message(id: String): Message
messages: [Message]
}