使用继电器现代graphql的AddMutation [英] AddMutation using relay modern graphql
问题描述
我正在尝试使用relay添加用户,以下是我的架构文件
schema.graphql
I'm trying to add a user using relay , below is my schema file
schema.graphql
createUser(input: CreateUserInput!): UserPayload
input CreateUserInput {
clientMutationId: String
data: CreateUserData!
}
type CreateUserData {
firstName: String!
lastName: String!
password: String!
email: String
}
type UserPayload {
clientMutationId: String
node: User
}
type User {
id: ID!
firstName: String!
lastName: String!
email: String
password: String
}
下面是我的突变文件,
AddUserMutation.js
below is my mutation file ,
AddUserMutation.js
const mutation = graphql`
mutation AddUserMutation($input: CreateUserInput!) {
createUser(input: $input) {
data {
firstName
lastName
email
password
}
}
}
`;
function commit(environment, node) {
var firstName = node.fName;
var lastName = node.lName;
var email = node.signupEmail;
var password = node.pwd;
return commitMutation(environment, {
mutation,
variables: {
input: {
firstName,
lastName,
email,
password
},
},onCompleted: (response, errors) => {
console.log('Response received from server.',response)
},
onError: err => console.error(err),
},
);
}
export default { commit };
但是它抛出了这个错误
GraphQLParser:类型为
UserPayload
的未知字段data
.来源:文档AddUserMutation
文件:js/mutations/AddUserMutation.js
.
GraphQLParser: Unknown field
data
on typeUserPayload
. Source: documentAddUserMutation
file:js/mutations/AddUserMutation.js
.
所以我在这里做错了什么,我搜索了许多仍然能够找到解决方案的站点.有人可以帮忙/澄清一下吗.
so what i did wrong here , i searched many sites still able to find a solution for this . can someone help/clarify me on this .
推荐答案
根据您的架构, createUser
突变解析为 UserPayload
类型的对象.该类型在您的模式中只有两个字段:
According to your schema, the createUser
mutation resolves to an object of the UserPayload
type. That type has only two fields in your schema:
type UserPayload {
clientMutationId: String
node: User
}
但是,您的查询实际上是在请求 data
字段,对于 UserPayload
类型而言,该字段不存在.您需要修改查询以请求正确的字段,例如:
However, your query is actually requesting a data
field, which doesn't exist for the UserPayload
type. You'll need to modify your query to request the correct fields, for example:
const mutation = graphql`
mutation AddUserMutation($input: CreateUserInput!) {
createUser(input: $input) {
node: {
firstName
lastName
email
password
}
}
}
`;
您可能还需要更改架构,因为 CreateUserData
应该是 input
而不是 type
.
You may also need to change your schema, since CreateUserData
should be an input
not a type
.
这篇关于使用继电器现代graphql的AddMutation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!