GraphQLSwift / Graphiti

The Swift GraphQL Schema framework for macOS and Linux
MIT License
531 stars 67 forks source link

Many-to-many relationships in Graphiti Schema #141

Open RoProducts opened 1 month ago

RoProducts commented 1 month ago

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

cshadek commented 1 month ago

Hi @RoProducts, what is the function signature for User.getGroups()?

RoProducts commented 1 month ago

The function signature is

   func getGroups(request: Request,arguments: NoArguments) throws -> EventLoopFuture<[Group]> {
        [ ... ]
    }