-1

I am trying to pass an input object to a GraphQL object that looks like this:

input User {
    id: ID
    posts: [Post]
}
input Post {
    id: ID
    user: User
}

When pass an input object of type User which has a circular dependency with Post, I get a circular dependency error in the JSON.stringify call made during the fetch:

Converting circular structure to JSON
        --> starting at object with constructor 'User'
        |     property 'post' -> object with constructor 'Array'
        |     index 0 -> object with constructor 'Post'
        --- property 'user' closes the circle"

This issue is discussed here:

GraphQL circular dependency

The answer talks about changing the schema to use extend. However extend won’t work with input types. Is there another solution?

jmc42
  • 359
  • 5
  • 22
  • Please show your input type and mutation definition. I'm not sure how you could get this error unless your input type is `user > posts > user` – Michel Floyd Aug 31 '23 at 21:21
  • the input types from the schema are listed above and yes user > posts > user. – jmc42 Sep 01 '23 at 09:20

1 Answers1

0

Your input types are over-specified. Input types do not need to match up with object types 1:1. In other words, you can have:

type User {
    id: ID
    name: String
    posts: [Post]
}
type: Post {
    id: ID
    body: String
    user: User
}

with input types:

input User {
    id: ID
    name: String
    posts: [Post]
}
input Post {
    id: ID
    body: String
}

There's no point in Post also specifying User data since you're starting from the User.

Although TBH you typically want to generate IDs on the server instead of passing them through a mutation.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39