简体   繁体   English

我无法解决的Graphql架构设计问题

[英]Graphql schema design problem that I can't solve

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.用户 A 喜欢用户 B 用户 B 匹配用户 B 现在用户 A 和用户 B 可以开始互相聊天了。

I also keep track of who visited each profile User A VISITED BY User B User A Visited BY User C我还跟踪谁访问了每个个人资料 用户 A 被用户 B 访问 用户 A 被用户 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 查询:

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 查询列出我附近的用户,看起来像这样:

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.我的问题由于用户(LIKED_BY,MATCHED_WITH)之间存在关系,我在我的架构中在哪里使用这种关系以便它可以兑现。 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.请记住,客户端的关系可以从 NO_RELATIONSHIP 更改为 LIKED_BY 再到 MATCHED_WITH,因此如果关系在多个地方重复,这将是一个问题。

I would really appreciate any help as I am out of ideas.我真的很感激任何帮助,因为我没有想法。

Thanks in advance.提前致谢。

In the GraphQL schema you'd generally make links like this explicit references to the other object.在 GraphQL 模式中,您通常会像这样显式引用另一个对象的链接。

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在您的顶级Query类型中,您可以返回当前用户

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.您的应用程序的解析器代码需要填写这些对象引用。

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; relationToMe字段可以有任意数量(包括零)的关系类型; you can define those as a GraphQL enum您可以将它们定义为 GraphQL 枚举

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": "jsmith@example.com",
        "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.如果您有类似多对多 SQL 连接表的内容,您可以查询它以填充此字段(如果需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM