alm*_*man 13 javascript relayjs
我目前正在使用DefaultNetworkLayer中设置的自定义标头处理中继之外的身份验证.这是首选的方式吗?有没有办法在接力中做到这一点?我试图在继电器中实现注册功能时遇到困难:在中继中有以下配置:FIELDS_CHANGE,NODE_DELETE,RANGE_ADD,RANGE_DELETE.所以RANGE_ADD是唯一可以应用的,但是我需要一个父和连接,我没有为新创建的用户....
Dud*_*che 16
我的项目也遇到了这个问题,这就是我处理它的方式.我用那些字段做了一个用户模式
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',
}
})
Run Code Online (Sandbox Code Playgroud)
那么这就是我的Root模式上的用户字段的样子
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;
})
}
Run Code Online (Sandbox Code Playgroud)
在你的Root模式中,你要求我实现的getUser函数如下,这是主要的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;
})
}
Run Code Online (Sandbox Code Playgroud)
这是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,
}
}
`;
}
Run Code Online (Sandbox Code Playgroud)
那么这里是loginMutation服务器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;
})
});
Run Code Online (Sandbox Code Playgroud)
Etvoilà!为了回答你的问题,我使用了FIELD_CHANGE变异,并且共享相同的id无论登录哪个用户,用于获取正确用户的真实ID实际上是userID.它在我的项目中工作正常.您可以在这里查看Relay-Graphql-repo
PS:你必须在你的networkLayer上添加它才能接受cookie
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('/graphql', {
credentials: 'same-origin'
})
);
Run Code Online (Sandbox Code Playgroud)
中继故意不了解身份验证机制,因此通过网络层使用自定义标头是合适的。
至于如何处理登录后响应,最好在此时触发全页刷新或重定向,因为 Relay 目前没有办法重置所有内部状态(尽管它在理想的列表中)项目)。鉴于查看者身份可以极大地影响项目可见性,因此几乎需要完全重置才能保持正确/稳健的行为。
| 归档时间: |
|
| 查看次数: |
3162 次 |
| 最近记录: |