Graphiti icon indicating copy to clipboard operation
Graphiti copied to clipboard

Many-to-many relationships in Graphiti Schema

Open RoProducts opened this issue 1 year ago • 2 comments

Hi,

I'm having issues creating a GraphQL schema with Graphiti containing a many-to-many relationship.

The setup is basically this :

let schema = try! Schema<Resolver, Request> {

  Scalar(UUID.self)

    Type(User.self) {
        Field("id", at: \.id)
        [ ... ]
        // Users' groups (many-to-many relationship)
        Field("groups", at: User.getGroups)
    }

    Type(Group.self){
    
        Field("id", at: \.id)
        [ ... ]
        Field("users", at: Group.getUsers)
   }     

   [ ... ]
 
}

I'm getting the error

Fatal error: 'try!' expression unexpectedly raised an error: Cannot use type "Array<Group>" for field "groups". Type does not map to a GraphQL type.

The problem seems that User uses groups before Group is declared. Do you have a sample or some docs I didn't see how to pre-declare types in this scenario ?

Thank you

RoProducts avatar Sep 09 '24 06:09 RoProducts