relayjs:使用relay进行身份验证,使用哪个变种? [英] relayjs: authentication using relay, which mutation to use?

查看:103
本文介绍了relayjs:使用relay进行身份验证,使用哪个变种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用DefaultNetworkLayer中设置的自定义标头处理中继之外的身份验证。
这是首选的方式吗?有没有办法在接力中做到这一点?
我试图在继电器中实现注册功能时遇到困难:在中继中有以下配置:FIELDS_CHANGE,NODE_DELETE,RANGE_ADD,RANGE_DELETE。所以RANGE_ADD是唯一适用于此的地方,但我需要一个父和连接,我没有为新创建的用户....

I am currently handling authentication outside of relay with custom header set in DefaultNetworkLayer. Is it preferred way and Is there a way to do it in relay? I was stuck when trying to implement sign up functionality in relay: in relay there are following configs: FIELDS_CHANGE, NODE_DELETE, RANGE_ADD, RANGE_DELETE. So RANGE_ADD is the only one applicable here, however I need a parent and connection for that, which I do not have for a newly created user....

推荐答案

我的项目也有这个问题,这就是我处理它的方式。
我用那些字段做了一个用户模式

I had this issue on my project as well, here is how I handled it. I did a User Schema with that fields

export var GraphQLUser = new GraphQLObjectType({
  name: 'User',
  fields: {
  id: globalIdField('User'), //this id is an immutable string that never change.
  userID: {
    type: GraphQLString,
    description: 'the database user\'s id',
  },
  name: {
    type: GraphQLString,
    description: 'the name of the user',
  },
  mail: {
    type: GraphQLString,
    description: 'the mail of the user',
  }
})

然后这是我的Root模式上的用户字段的样子

then here is how my user field on my Root schema looks like

var GraphQLRoot = new GraphQLObjectType({
  user: {
  type: new GraphQLNonNull(GraphQLUser),
  description: 'the user',
  resolve: (root, {id}, {rootValue}) => co(function*() {
    var user = yield getUser(rootValue);
    return user;
  })
}

你想要我实现的getUser函数你的Root模式,这里是主要的hack!

on your Root schema you ask for the getUser function that I implemented as follows, here is the main hack!

const logID = 'qldfjbe2434RZRFeerg'; // random logID that will  remain the same forever for any user logged in, this is the id I use for my FIELD_CHANGE mutation client side

export function getUser(rootValue) {
  //IF there is no userID cookie field, no-one is logged in
  if (!rootValue.cookies.get('userID')) return {
    name: '',
    mail: '',
    id: logID
  };
  return co(function*() {
    var user =  yield getDatabaseUserByID(rootValue.cookies.get('userID'));
    user.userID = user.id;
    user.id = logID // change the id field with default immutable logID to handle FIELD_CHANGE mutation
    return user;
  })
}

这里是loginMutation客户端支持

here is the loginMutation client Side

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,
      id: this.props.user.id
    };
  }
  getConfigs() {
    return [{
      type: 'FIELDS_CHANGE',
      fieldIDs: {
        user: this.props.user.id,
      }
    }];
  }
  getOptimisticResponse() {
    return {
      mail: this.props.credentials.pseudo,
      id: this.props.user.id
    };
  }
  getFatQuery() {
    return Relay.QL`
    fragment on LoginPayload {
      user {
        userID,
        mail,
        name,
      }
    }
    `;
  }

然后这里是loginMutation服务器端

then here is the loginMutation server Side

export var LoginMutation = mutationWithClientMutationId({
  name: 'Login',
  inputFields: {
    mail: {
      type: new GraphQLNonNull(GraphQLString)
    },
    password: {
      type: new GraphQLNonNull(GraphQLString)
     },
     id: {
       type: new GraphQLNonNull(GraphQLString)
    }
  },
  outputFields: {
    user: {
      type: GraphQLUser,
      resolve: (newUser) => newUser
    }
  },
  mutateAndGetPayload: (credentials, {rootValue}) => co(function*() {
    var newUser = yield getUserByCredentials(credentials, rootValue);
    //don't forget to fill your cookie with the new userID (database id)
    rootValue.cookies.set('userID', user.userID);
    return newUser;
  })
});

Etvoilà!为了回答你的问题,我使用了FIELD_CHANGE变异,并且共享相同的id无论登录哪个用户,用于获取正确用户的真实ID实际上是userID。它在我的项目中工作正常。您可以在这里查看 Relay-Graphql-repo

Et voilà! To answer your question I used the FIELD_CHANGE mutation and shared the same id doesnt matter which user is logged in, the real id used to get the right user is actually userID. It works fine on my project. You can have a look on it here Relay-Graphql-repo

PS:你必须在你的networkLayer上添加它才能接受cookie

PS: You'll have to add it on your networkLayer to accept cookies

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('/graphql', {
  credentials: 'same-origin'
  })
);

这篇关于relayjs:使用relay进行身份验证,使用哪个变种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆