如何从一个突变中获得一个新对象的ID ?

时间:2021-02-09 16:49:04

I have a createObject mutation that returns the ID of the new object.

我有一个createObject突变,它返回新对象的ID。

After it returns I want to redirect to a detail page about the new object.

返回后,我想重定向到一个关于新对象的详细页面。

How can I get response fields from a mutation in the containing component using react/relay?

如何使用react/relay从包含组件的突变中获得响应字段?

E.g. my createObject page contains the mutation with code like:

例如,我的createObject页面包含如下代码的突变:

var onFailure = (transaction) => {

};

var onSuccess = () => {
  redirectTo('/thing/${newthing.id}');   // how can I get this ID?
};

// To perform a mutation, pass an instance of one to `Relay.Store.update`
Relay.Store.update(new AddThingMutation({
  userId: this.props.userId,
  title: this.refs.title.value,
}), { onFailure, onSuccess });
}

newthing should be the object created by the mutation, but how can I get hold of it?

newthing应该是由突变产生的对象,但是我如何才能得到它呢?

1 个解决方案

#1


19  

Normally we would configure the client-side of the mutation with RANGE_ADD and return a new thingEdge from the server side of the mutation, but here you don't have a range on the client to add the new node to. To tell Relay to fetch an arbitrary field, use the REQUIRED_CHILDREN config.

通常情况下,我们会用RANGE_ADD来配置突变的客户端,并从该突变的服务器端返回一个新的东西,但是在这里,您没有在客户端上添加新节点的范围。要告诉Relay获取一个任意字段,请使用REQUIRED_CHILDREN配置。

Server side mutation

var AddThingMutation = mutationWithClientMutationId({
  /* ... */
  outputFields: {
    newThingId: {
      type: GraphQLID,
      // First argument: post-mutation 'payload'
      resolve: ({thing}) => thing.id,
    },
  },
  mutateAndGetPayload: ({userId, title}) => {
    var thing = createThing(userId, title);
    // Return the 'payload' here
    return {thing};
  },
  /* ... */
});

Client side mutation

class AddThingMutation extends Relay.Mutation {
  /* ... */
  getConfigs() {
    return [{
      type: 'REQUIRED_CHILDREN',
      // Forces these fragments to be included in the query
      children: [Relay.QL`
        fragment on AddThingPayload {
          newThingId
        }
      `],
    }];
  }
  /* ... */
}

Example usage

var onFailure = (transaction) => {
  // ...
};

var onSuccess = (response) => {
  var {newThingId} = response.addThing;
  redirectTo(`/thing/${newThingId}`);
};

Relay.Store.update(
  new AddThingMutation({
    title: this.refs.title.value,
    userId: this.props.userId,
  }), 
  {onSuccess, onFailure}
);

Note that any fields you query for by using this technique will be made available to the onSuccess callback, but will not be added to the client side store.

注意,使用此技术查询的任何字段都将对onSuccess回调可用,但不会添加到客户端存储中。

#1


19  

Normally we would configure the client-side of the mutation with RANGE_ADD and return a new thingEdge from the server side of the mutation, but here you don't have a range on the client to add the new node to. To tell Relay to fetch an arbitrary field, use the REQUIRED_CHILDREN config.

通常情况下,我们会用RANGE_ADD来配置突变的客户端,并从该突变的服务器端返回一个新的东西,但是在这里,您没有在客户端上添加新节点的范围。要告诉Relay获取一个任意字段,请使用REQUIRED_CHILDREN配置。

Server side mutation

var AddThingMutation = mutationWithClientMutationId({
  /* ... */
  outputFields: {
    newThingId: {
      type: GraphQLID,
      // First argument: post-mutation 'payload'
      resolve: ({thing}) => thing.id,
    },
  },
  mutateAndGetPayload: ({userId, title}) => {
    var thing = createThing(userId, title);
    // Return the 'payload' here
    return {thing};
  },
  /* ... */
});

Client side mutation

class AddThingMutation extends Relay.Mutation {
  /* ... */
  getConfigs() {
    return [{
      type: 'REQUIRED_CHILDREN',
      // Forces these fragments to be included in the query
      children: [Relay.QL`
        fragment on AddThingPayload {
          newThingId
        }
      `],
    }];
  }
  /* ... */
}

Example usage

var onFailure = (transaction) => {
  // ...
};

var onSuccess = (response) => {
  var {newThingId} = response.addThing;
  redirectTo(`/thing/${newThingId}`);
};

Relay.Store.update(
  new AddThingMutation({
    title: this.refs.title.value,
    userId: this.props.userId,
  }), 
  {onSuccess, onFailure}
);

Note that any fields you query for by using this technique will be made available to the onSuccess callback, but will not be added to the client side store.

注意,使用此技术查询的任何字段都将对onSuccess回调可用,但不会添加到客户端存储中。