0

当我尝试使用 Relay Mutations 更新 Relay 中的记录时,我收到这样的错误。

没有字段“clientMutationId”

但是我没有在我的 GraphQL 模式中声明任何 clientMutationId。这是我在 Relay 中的代码

function updateZipdata1(){
  console.log("update zip");
   return new Promise((resolve, reject) => {
    var Connection = require('tedious').Connection;
    var Request = require('tedious').Request;
    var config = {
        userName: 'xxx',
        password: 'xxxx',
        server: 'xxxx', 
        options: {
            database: 'xxxx'

        }
     }
    var results = [];
    var connection = new Connection(config);
    connection.on('connect', (err)=> {
         if (err) {
            console.log(err)
            reject(err);
        }
        var ZipCode="123456";
        var RecNo="789456";

        var sql = "update dbo.xrxZip set ZipCode='"+ZipCode+"' where RecNo='"+RecNo+"'";
        var request = new Request(sql,
            (err, rowCount)=> {
                if (err) {
                    console.log('SQLError');
                    connection.close();
                    reject(err);
                }
                else {
                  console.log("sql:"+rowCount)
                    connection.close();
                    resolve(results);   
              }
            });
            request.on('row', (columns)=> {
            var row = {};
            columns.forEach((column)=> {
                if (column.isNull) {
                    row[column.metadata.colName] = null;
                } else {
                    row[column.metadata.colName] = column.value;
                }
            });
            console.log("hai:"+results)
            results.push(row);

        });
          connection.execSql(request);
      })
   })
};  

我的 GraphQL 架构:

var zipType = new GraphQLObjectType({
  name: 'Zipcode',
  fields: () => ({

    ZipCode: {type: GraphQLString},
    City: {type: GraphQLString},
    ShortCode:{type: GraphQLString},
    State:{type: GraphQLString},
    Phone:{type: GraphQLString},
    TaxCode:{type: GraphQLString},
    Tax:{type: GraphQLString},
    RecNo:{type: GraphQLString}

  }),
});

var zipCodeType = new GraphQLObjectType({
  name: 'Zip',
  fields: () => ({
    zipcodes: {type: new GraphQLList(zipType)},
  }),
});

export var Schema = new GraphQLSchema({
  mutation: new GraphQLObjectType({
    name: "Mutation",
     fields: () => ({
      UpdateZipcode:{
        type: zipCodeType,
        args: {
          ZipCode: {type: GraphQLString}
        },
        resolve: (root,{ZipCode}) =>updateZipdata,

      }
    })
  })
})
 const updateZipdata = {
 zipcodes: updateZipdata1(), // Calling the function
};

当我在终端中为硬编码的 ZipCode 和 RecNo 运行它时,它可以正常工作,没有任何错误。但是当我尝试在 Relay 中执行时,我收到了如上所述的错误。

听到是我的中继代码:

这是 Relay 中用于突变的子类。

export default class updateZipMutation extends Relay.Mutation {
  getMutation() {
    alert("getMutation");
    return Relay.QL`mutation{UpdateZipcode}` ;
  }
  getVariables() {
    alert("getVariables");
    return { RecNo: this.props.data.RecNo,ZipCode:this.props.data.ZipCode,City:this.props.data.City,Phone:this.props.data.Phone,ShortCode:this.props.data.ShortCode,Tax:this.props.data.Tax,TaxCode:this.props.data.TaxCode,State:this.props.data.State};
  }
  getFatQuery() {
    alert("getFatQuery")
    return Relay.QL`
      fragment on Zipcode {
          RecNo,
          ZipCode,
          City,
          Phone,
          ShortCode,
          Tax,
          TaxCode,
          State
      }
    `;
  }
  getConfigs() {
    alert("getConfigs");
    return [{
      type: 'zipCodeType',
      fieldIDs:{
        RecNo: this.props.data.RecNo
      },
    }];
  }
}

这是我在 Relay 中调用突变子类的主类:

export default class zips extends React.Component {
 Relay.Store.update(
      new updateZipMutation({        //I am getting the data from form
        data: { RecNo, ZipCode,City,Phone,ShortCode,Tax,TaxCode,State},
      })
    );
  }
}

任何人请给我建议如何解决它,以及如何处理突变。非常感谢任何帮助。

4

2 回答 2

3

中继突变必须具有clientMutationId在其中被接受的字段,input并在结果有效负载中按原样返回。Relay 使用它们来跟踪哪个突变服务器有响应。此外,对于 Relay,突变必须接受一个参数 - input。它可以是 InputObject 类型,但必须这样形成。这是 Relay GraphQL Mutation 接口的详细指南https://facebook.github.io/relay/docs/graphql-mutations.html#content

在您的情况下,您可以执行以下操作:

export var Schema = new GraphQLSchema({
  mutation: new GraphQLObjectType({
    name: "Mutation",
     fields: () => ({
      UpdateZipcode:{
        type: zipCodeType,
        args: {
          input: {type: new GraphQLInputObjectType({
            name: "UpdateZipCodeInput",
            fields: {
              clientMutationId: { type: String },
              ZipCode: { type: String },
          })}
        },
        resolve: (root,{input: { clientMutationId, ZipCode}) => ({
          ...updateZipCode(ZipCode),
          clientMutationId,
        }),
      }
    })
  })
})
于 2016-04-14T09:44:12.357 回答
1

只需使用graphql-relay包中的中继辅助功能

mutationWithClientMutationId

它将提供一个中继兼容的突变

https://github.com/graphql/graphql-relay-js#mutations

于 2016-05-28T14:27:44.147 回答