Azure / Azure/data-api-builder

GraphQL relationships can cause a stack overflow exception

Open
#746 0 comments 0 reactions 0 assignees View on GitHub
bug cosmos engine graphql
Dominant language
C#
Stars
1.5k
Forks
370
Avg merge
3d 22h
Merged PRs (30d)
9

Description

Due to the type walking within our schema builder, it's possible to create a GraphQL schema that will result in a stack overflow exception.

Here's an example of a schema that causes the problem:

```graphql
interface Node {
id: ID!
}

enum GameState {
WaitingForPlayers
Started
Completed
}

type Question implements Node @model {
id: ID!
question: String!
correctAnswer: String!
answers: [String!]!
}

type Player implements Node @model {
id: ID!
name: String!
game(gameId: ID!): Game!
games: [Game!]!
}

type Game implements Node @model {
id: ID!
state: GameState
players: [Player!]!
questions: [Question!]!
}
```

The head of the stack is here:

```
at Azure.DataApiBuilder.Service.GraphQLBuilder.GraphQLUtils.IsBuiltInType(HotChocolate.Language.ITypeNode)
at Azure.DataApiBuilder.Service.GraphQLBuilder.Mutations.CreateMutationBuilder.FieldAllowedOnCreateInput(HotChocolate.Language.FieldDefinitionNode, Azure.DataApiBuilder.Config.DatabaseType, System.Collections.Generic.IEnumerable`1)
```

But the full exception message is over 10k lines long.

I'm not sure what the state of relationships are in Cosmos, but this is realistic GraphQL schema (and the one I use for the demos I generally do).

My guess is that it doesn't like that `Player` references `Game` which references `Player` which references `Game` which...

Contributor guide

Open the contributing guide

Research direction

Reproduce the stack overflow with the GraphQL schema in the issue, then inspect GraphQLUtils.IsBuiltInType and Mutations.FieldAllowedOnCreateInput, identified in the stack trace. Trace how the Player–Game relationship is walked repeatedly and establish a bounded traversal; done means this schema builds without a stack overflow and existing mutation behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, graphql
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.