我目前正在使用DefaultNetworkLayer中的自定义标头集处理中继之外的身份验证。它是首选的方式吗?有没有在接力赛中这样做的方法?当我尝试在中继中实现注册功能时,我被卡住了:在中继中,有以下配置: FIELDS_CHANGE,NODE_DELETE,RANGE_ADD,RANGE_DELETE。所以RANGE_ADD在这里是唯一适用的,但是我需要一个父级和连接,这对于新创建的用户是没有的……
发布于 2016-01-24 18:23:20
我在我的项目中也遇到了这个问题,下面是我如何处理它的。我使用这些字段创建了一个用户模式
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',
}
})下面是我的根模式中的用户字段
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函数,如下所示,这是主要的技巧!
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客户端
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服务器端
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;
})
});就是这样!为了回答你的问题,我使用了FIELD_CHANGE变异,并共享相同的id,无论哪个用户登录,用于获得正确用户的真实id实际上是userID。它在我的项目中工作得很好。你可以在这里看一下Relay-Graphql-repo
PS:你必须把它添加到你的networkLayer上才能接受cookies
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('/graphql', {
credentials: 'same-origin'
})
);发布于 2015-09-13 06:10:09
中继有意地与身份验证机制无关,因此通过网络层使用自定义标头是合适的。
至于如何处理登录后的响应,最好是在此时触发整个页面刷新或重定向,因为Relay目前没有重置所有内部状态的方法(尽管是it is on the list of desirable items)。鉴于查看者身份可能会极大地影响项目可见性,为了保持正确/健壮的行为,完全重置是非常必要的。
发布于 2015-09-21 10:05:42
存在一个未记录的配置类型,它看起来非常适合该用例:https://github.com/facebook/relay/issues/237
https://stackoverflow.com/questions/32535141
复制相似问题