Graphql schema design problem that I can't solve

233 views Asked by At

I am trying to design a schema for an application and I have a problem that I can't solve. Here is a detail of the application

User A LIKES User B User B MATCHS User B Now User A and User B can start chatting with each others.

I also keep track of who visited each profile User A VISITED BY User B User A Visited BY User C

In the application, I have Me type with details of the user running the app. I have a me query that looks like this:

me {
  id
  name
  email
  ...
  ...
  likes {  ## users who liked me
    nextToken
    edges {
      node { ## user
        id
        name
        ...
      }
    }
  }
  matchs { ## users who matched with me
    nextToken
    edges {
      node { ## user
        id
        name
        ...
        ...
      }
    }
  } 
  Vists { ## users who visited me
    nextToken
    edges {
      node { ## 
        id
        name
        ...
        ...
      }
    }
  }
}

In addition to that, I have listUsers query that list users nearby to Me and looks something like this:

listUsers {
  nextToken
  total
  edges {
    distance
    node {  ## user
      id
      name
      ...
      ...
    }
  }
}

MY QUESTION Since there is a relationship between users (LIKED_BY, MATCHED_WITH) where do I use this relationship in my schema such that it is cashable. Keep in mind the relationship can change at the client from NO_RELATIONSHIP to LIKED_BY to MATCHED_WITH so if the relationship is duplicated in multiple places, this will be a problem.

I would really appreciate any help as I am out of ideas.

Thanks in advance.

2

There are 2 answers

1
David Maze On

You clarified in a comment:

While listing users in the search query, I need to be able to know the relationship between Me and the users

That is, you're trying to ask about some user

type User {
  relationToMe: [RelationshipType!]
}

The relationToMe field can have any number (including zero) of relationship types; you can define those as a GraphQL enum

enum RelationshipType {
  LIKES, LIKED_BY,
  VISITED, VISITED_BY,
  MATCHES               # bidirectional
}

Then in your query you can ask

query FindSomeUsers($input: SearchUsersInput!) {
  searchUsers(input: $input) {
    users {
      id, name, email
      relationToMe
    }
  }
}

and get back a response like

{
  "data": {
    "searchUsers": {
      "users": [
        "id": "12345",
        "name": "John Smith",
        "email": "[email protected]",
        "relationToMe": ["VISITED", "MATCHES"]
      ]
  }
}

You'd need to implement a custom resolver in your implementation to populate this field. If you had something like a many-to-many SQL join table, you could query that to fill in this field, if requested.

5
David Maze On

In the GraphQL schema you'd generally make links like this explicit references to the other object.

type User {
  id: ID!
  ...
  matches: [User!]  # actual users, not their IDs
  likes: [User!]
  visitedBy: [User!]
}

In your top-level Query type you can return the current user

type Query {
  me: User
}

Now if I want the names and email addresses of people who have visited me, I can find out

query WhoVisitedMe {
  me {
    visitedBy { name, email }
  }
}

You can traverse this graph, for example, to get recommendations: for people who visit you, who do they like?

query WhoDoVisitorsLike {
  me {
    visitedBy {
      likes {
        id
        name
      }
    }
  }
}

Your application's resolver code would need to fill in these object references.