我正在尝试使用jwt令牌向我的应用添加身份验证。我已经使用FIELDS_CHANGE类型创建了一个LoginMutation,但看起来relay并没有将它与现有模型挂钩。 我已经列出了我的中继突变和模式变异。
我的LoginMutation:
import Relay from 'react-relay';
export default class LoginMutation extends Relay.Mutation {
static fragments = {
viewer: () => Relay.QL`
fragment on User {
id
}
`
};
getMutation() {
return Relay.QL`mutation{loginUser}`;
}
getFatQuery() {
return Relay.QL`
fragment on LoginPayload {
viewer {
userId
email
password
jwt_token
}
}
`;
}
getConfigs() {
return [{
type: 'FIELDS_CHANGE',
fieldIDs: {
viewer: this.props.viewer.id
}
}];
}
getVariables() {
// inputs to the mutation
return {
email: this.props.credentials.email,
password: this.props.credentials.password
};
}
getOptimisticResponse() {
return {
viewer: {
email: this.props.credentials.email
}
};
}
}
const loginMutation = mutationWithClientMutationId({
name: 'Login',
inputFields: {
email: {
type: new GraphQLNonNull(GraphQLString)
},
password: {
type: new GraphQLNonNull(GraphQLString)
}
},
outputFields: {
viewer: {
type: userType,
resolve: (user) => user
}
},
mutateAndGetPayload: ({ email, password }, request) => {
return getUserByCredentials(email, password)
.then((user) => {
if (!user) {
return newUser;
}
user.jwt_token = jwt.sign({
id: user.id,
name: user.name,
email: user.email
}, JWT_SECRET);
return user;
})
.catch((error) => { throw error; });
}
});