Relayjs Graphql用户身份验证

Sen*_*eca 9 graphql relayjs

是否可以通过graphql服务器结合中继和反应来验证具有不同角色的用户?

我环顾四周,找不到有关此主题的更多信息.

在我目前的设置中,具有不同角色的登录功能仍然通过传统的REST API ...(使用json web令牌'安全').

Dud*_*che 6

我在我的一个应用程序中做到了,基本上你只需要一个用户界面,如果没有人登录,这个在第一个根查询上返回null,然后你可以用传递凭证的登录变异更新它.主要问题是在发布中继请求中获取cookie或会话,因为它不处理请求中的cookie字段.

这是我的客户端变异:

 export default class LoginMutation extends Relay.Mutation {
  static fragments = {
    user: () => Relay.QL`
      fragment on User {
        id,
        mail
      }
    `,
  };
  getMutation() {
    return Relay.QL`mutation{Login}`;
  }

  getVariables() {
    return {
      mail: this.props.credentials.pseudo,
      password: this.props.credentials.password,
    };
  }
  getConfigs() {
    return [{
      type: 'FIELDS_CHANGE',
      fieldIDs: {
        user: this.props.user.id,
      }
    }];
  }
  getOptimisticResponse() {
    return {
      mail: this.props.credentials.pseudo,
    };
  }
  getFatQuery() {
    return Relay.QL`
    fragment on LoginPayload {
      user {
        userID,
        mail
      }
    }
    `;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的架构方面的突变

var LoginMutation = mutationWithClientMutationId({
  name: 'Login',
  inputFields: {
    mail: {
      type: new GraphQLNonNull(GraphQLString)
    },
    password: {
      type: new GraphQLNonNull(GraphQLString)
    }
  },
  outputFields: {
    user: {
      type: GraphQLUser,
      resolve: (newUser) => newUser
    }
  },
  mutateAndGetPayload: (credentials, {
    rootValue
  }) => co(function*() {
    var newUser = yield getUserByCredentials(credentials, rootValue);
    console.log('schema:loginmutation');
    delete newUser.id;
    return newUser;
  })
});
Run Code Online (Sandbox Code Playgroud)

让我的用户通过页面刷新进行记录我发送自己的请求并用cookie字段填充...这是目前使其工作的唯一方法...

  • 在没有复位功能(其中[继电器目前没有(https://github.com/facebook/relay/issues/233)),通过继电器本身做这可能不是一个好主意.由于Relay在内部缓存GraphQL数据,并且该数据可能是特定于用户的,因此您需要一种方法在logout _or_ login上使该缓存无效.目前,我们建议人们在其中任何一个事件发生时进行整页刷新. (2认同)