Ответ 1
У меня тоже была проблема с моим проектом, вот как я справился с этим. Я сделал схему пользователя с этими полями
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\ 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 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;
})
});
Et voilà! Чтобы ответить на ваш вопрос, я использовал мутацию FIELD_CHANGE и поделился одним и тем же идентификатором, не имеет значения, какой пользователь зарегистрирован, реальный идентификатор, используемый для получения нужного пользователя, на самом деле является userID. Он отлично работает в моем проекте. Вы можете посмотреть здесь Relay-Graphql-repo
PS: вам нужно добавить его на свой сетевой слой, чтобы принимать файлы cookie
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('/graphql', {
credentials: 'same-origin'
})
);