简体   繁体   English

中继/路由器登录变异?

[英]Relay/router login mutation?

I'm trying to implement a mutation for login. 我正在尝试实现登录变异。 The mutation validates the provided id_token and logs the user in via sessions. 该变异验证提供的id_token并通过会话记录用户。 The mutation itself works (verified with GraphiQL), but I'm having issues integrating it with Relay. 突变本身有效(用GraphiQL验证),但我遇到了将它与Relay集成的问题。

When a user logs in, the entire Relay store is possibly altered since "viewer" is the root query. 当用户登录时,整个中继存储可能会被更改,因为“viewer”是根查询。 But I don't want to list out my entire query tree in the fat query. 但我不想在胖查询中列出我的整个查询树。 It would be nice to be able to clear the entire store somehow, but I don't see a way of doing that with react-router-relay . 能够以某种方式清除整个商店会很高兴,但我没有看到使用react-router-relay做到这一点的方法。

Relay mutation: 中继突变:

export default class LoginMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL`mutation {login}`;
  }

  getVariables() {
    return {
      id_token: this.props.id_token
    };
  }

  getFatQuery() {
    // TODO: list everything?
    return Relay.QL`
      fragment on LoginPayload {
        viewer
      }
    `;
  }

  getConfigs() {
    return []; // TODO: not sure what to return...
  }
}

Usage: 用法:

Relay.Store.commitUpdate(new LoginMutation({id_token}), {
        onSuccess: (resp) => {
          history.push('/');
        }
      });

GraphQL schema: GraphQL架构:

input LoginInput {
  id_token: String!
  clientMutationId: String!
}

type LoginPayload {
  viewer: Viewer
  clientMutationId: String!
}

type Mutation {
  login(input: LoginInput!): LoginPayload
}

interface Node {
  id: ID!
}

type Query {
  viewer: Viewer
  node(id: ID!): Node
}

type Viewer implements Node {
  id: ID!
  user: User
  ships: [Ship]
  ship(id: ID!): Ship
}

schema {
  query: Query
  mutation: Mutation
}

In the fatQuery , when you list a non-scalar field, Relay will assume that any connected field may have updated. fatQuery ,当您列出non-scalar字段时,Relay将假定任何连接的字段可能已更新。 In certain fields that require arguments (eg, (first: n) ), you can use @relay(pattern: true) to bypass this requirement. 在某些需要参数的字段中(例如, (first: n) ),您可以使用@relay(pattern: true)来绕过此要求。 This SO question discusses the feature. 这个SO问题讨论了这个特征。

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

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