私はUser
オブジェクトの更新トリガする必要がありRelay.Mutation
作成:実行、リレーはへuser
を通過すると中継変異で未解決の小道具を扱うにはどうすればいいですか?
// I initially passed this.props.user to the mutation
this.props.relay.commitUpdate(new UserMutation({ user: this.state.user })
:私はこのように私のコンポーネントUserDetails
にこの変異を使用してい
class UserMutation extends Relay.Mutation {
public getMutation() {
return Relay.QL`mutation {saveUser}`;
}
public getVariables() {
return {
id: this.props.user.id,
loginName: this.props.user.loginName,
firstName: this.props.user.firstName,
lastName: this.props.user.lastName,
mail: this.props.user.mail
}
}
public getFatQuery() {
return Relay.QL`
fragment on UserPayload {
user {
id,
loginName,
firstName,
lastName,
mail
}
}
`;
}
public getConfigs() {
return [{
type: "FIELDS_CHANGE",
fieldIDs: {
user: this.props.user.id
}
}];
}
static fragments = {
user:() => Relay.QL`
fragment on User {
id,
// I initially had only id here
loginName,
firstName,
lastName,
mail
}
`
}
}
をバックエンドでid
のみが設定され、他のプロパティはありません。入力変数に他のフィールドがないため、突然変異は実行されません。
突然変異をデバッグした後、this.props.user
がid以外のすべてのフィールドにundefined
を設定していることがわかりました。ただし、this._unresolvedProps.user
はuser
で、すべてのフィールドが正しく設定されています。
this.props
をthis._unresolvedProps
に置き換えると、必要なデータがすべてバックエンドに送信され、エラーは発生せずに突然変異が実行されます。フロントエンドキャッシュも正しく更新されているようです(他のコンポーネントではfirstName
などのフィールドが更新されます)。しかし、私はこれが正しい道であるとは思っていません。
私は何が間違っていますか? loginName
等
UPDATE
UserDetails
成分負荷のユーザーデータと、これらのプロパティを変更するテキストボックスを提供します。対応するリレーコンテナは、次のようになります
export default Relay.createContainer(UserDetails, {
fragments: {
user:() => Relay.QL`
fragment on User {
id,
loginName,
firstName,
lastName,
mail,
roles {
id,
name
},
${UserMutation.getFragment("user")}
}
`
}
});
...
public handleTextInput(fieldName: string, event: any) {
let user = this.state.user;
switch (fieldName) {
case "loginName": {
user.loginName = event.target.value;
break;
}
case "firstName": {
user.firstName = event.target.value;
break;
}
case "lastName": {
user.lastName = event.target.value;
break;
}
case "mail": {
user.mail = event.target.value;
break;
}
}
this.setState({ user: user });
}
...と私は今、合格提出するハンドラに提出形成変異
this.state.user
:
public handleSubmit(e: any) {
e.preventDefault();
this.props.relay.commitUpdate(new UserMutation({ user: this.state.user }), {
onSuccess: (response: any) => {
this.setState({ user: response.saveUser.user });
}
});
}
私はC#のバックエンドを使用します。graphql-dotnet。
public class ApplicationSchema : Schema
{
public ApplicationSchema()
{
this.Query = new ApplicationQuery();
this.Mutation = new ApplicationMutation();
}
}
public class ApplicationMutation : ObjectGraphType
{
public ApplicationMutation()
{
this.Name = "Mutation";
// save a user
this.Field<UserPayloadType>(
"saveUser",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<UserInputType>>
{
Name = "input",
Description = "the user that should be saved"
}),
resolve: context =>
{
var userInput = context.Argument<UserInput>("input");
var clientMutationId = userInput.ClientMutationId;
var user = MemoryRepository.UpdateUser(new User()
{
Id = userInput.Id,
LoginName = userInput.LoginName,
FirstName = userInput.FirstName,
LastName = userInput.LastName,
Mail = userInput.Mail
});
return new UserPayload()
{
ClientMutationId = clientMutationId,
User = user
};
});
}
}
public class UserInputType : InputObjectGraphType
{
public UserInputType()
{
this.Name = "UserInput";
this.Field<NonNullGraphType<StringGraphType>>("id", "The id of the user.");
this.Field<NonNullGraphType<StringGraphType>>("loginName", "The login name of the user.");
this.Field<NonNullGraphType<StringGraphType>>("firstName", "The first name of the user.");
this.Field<NonNullGraphType<StringGraphType>>("lastName", "The last name of the user.");
this.Field<NonNullGraphType<StringGraphType>>("mail", "The mail adress of the user.");
this.Field<NonNullGraphType<StringGraphType>>("clientMutationId", "react-relay property.");
}
}
public class UserPayloadType : ObjectGraphType
{
public UserPayloadType()
{
this.Name = "UserPayload";
this.Field<NonNullGraphType<UserType>>("user", "The user.");
this.Field<NonNullGraphType<StringGraphType>>("clientMutationId", "react-relay property.");
}
}
public class UserType : ObjectGraphType
{
public UserType()
{
this.Name = "User";
this.Field<NonNullGraphType<StringGraphType>>("id", "The id of the user.");
this.Field<NonNullGraphType<StringGraphType>>("loginName", "The login name of the user.");
this.Field<NonNullGraphType<StringGraphType>>("firstName", "The first name of the user.");
this.Field<NonNullGraphType<StringGraphType>>("lastName", "The last name of the user.");
this.Field<NonNullGraphType<StringGraphType>>("mail", "The mail adress of the user.");
Field<ListGraphType<RoleType>>("roles", resolve: context => MemoryRepository.GetRolesOfUser(context.Source as DomainModel.Models.User));
}
}
サーバー側のGraphQL変異「saveUser」を表示できますか?問題は 'saveUser'である可能性があります。 –
また、Relayコンテナの 'fragments'フィールドを追加してください。 –
必要なコードフラグメントを追加しました。ありがとう。 –