当我尝试使用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 for mutation中的子类。
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},
})
);
}
}
任何人请给我建议如何解决它,以及如何处理突变。任何帮助非常感谢。
答案 0 :(得分:2)
中继突变必须具有clientMutationId
中接受的input
字段,并在生成的有效负载中按原样返回。 Relay使用它们来跟踪哪个变异服务器有响应。除了Relay,突变必须接受一个参数 - input
。它可以是InputObject类型,但必须像这样形成。以下是Relay GraphQL Mutation inteface 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,
}),
}
})
})
})
答案 1 :(得分:1)
只需使用graphql-relay
包
mutationWithClientMutationId
它将提供与Relay兼容的突变