Hi,
I am trying to define my graphql schema/resolvers using Reason and I have a problem related to dependency cycle
that I don’t know how to solve.
ninja: error: dependency cycle: src/User.cmj -> src/Project.cmj -> src/User.cmj
Obviously I have a cyclic dependency graph. I have checked some OCaml resources and found some ways that could solve this problem (recursive modules, functors, :sweat_smile) but they seem over complicated and not recommended!
My GraphQL schema is really simple so I am not sure If the only option to solve this problem is to use recursive modules or not.
There is a User with a reference to its projects and a Project which also has a reference to the user that created it, i.e. users have projects and each project is assigned to a user.
GraphQL schema (I put it together here for simplicity but it is defined as a multiline string on the reason modules )
type User {
id: Int!
name: String!
projects: [Project!]
}
type Project {
id: Int!
name: String!
user: User!
}
Here are my modules:
User.re
type t = {
.
"id": int,
"name": string,
};
let typeDef = {|
type User {
id: Int!
name: String
projects: [Project!]
}
|};
/* Here is where the problem lies, I need a reference to the Project module types and methods! */
type resolvers = {
.
"projects": t => array(Project.t),
};
let resolvers = {
"projects": (user: t) =>
Js.Array.filter(
project => project##userId === user##id,
Project.projects,
),
};
/* mock some users */
let users = [|{"id": 1, "name": "Alistair"}, {"id": 2, "name": "Ariel"}|];
Project.re
type t = {
.
"id": int,
"name": string,
"userId": int,
};
let typeDef = {|
type Project {
id: Int!
name: String!
user: User!
}
|};
type resolvers = {
.
"user": t => Js.Nullable.t(User.t),
};
let resolvers: resolvers = {
"user": project =>
switch (Js.Array.find(user => user##id === project##userId, User.users)) {
| None => Js.Nullable.undefined
| Some(p) => Js.Nullable.return(p)
},
};
/* mock some projects */
let projects = [|
{"id": 1, "userId": 1},
{"id": 2, "userId": 1},
{"id": 3, "userId": 2},
|];
I appreciate any kind of help in advance!
Thank you!
Ariel.