中继突变中没有字段“clientMutationId”
Posted
技术标签:
【中文标题】中继突变中没有字段“clientMutationId”【英文标题】:doesn't have a field "clientMutationId" in Relay mutations 【发布时间】:2016-05-13 16:54:45 【问题描述】:当我尝试使用 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`mutationUpdateZipcode` ;
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,
)
);
任何人请给我建议如何解决它,以及如何处理突变。非常感谢任何帮助。
【问题讨论】:
【参考方案1】:只需使用 graphql-relay
包中的 Relay 辅助函数
mutationWithClientMutationId
它将提供一个与 Relay 兼容的突变
https://github.com/graphql/graphql-relay-js#mutations
【讨论】:
【参考方案2】:中继突变必须具有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,
),
)
)
)
【讨论】:
天啊!这是一个非常模糊的错误消息背后的“隐藏”错误!非常感谢!以上是关于中继突变中没有字段“clientMutationId”的主要内容,如果未能解决你的问题,请参考以下文章
中继突变。脂肪查询。询问 REQUIRED_CHILDREN 中的所有字段