AddMutation 使用中继现代 graphql

AddMutation using relay modern graphql

我正在尝试使用中继添加用户,下面是我的架构文件
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

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: Unknown field data on type UserPayload. Source: document AddUserMutation file: js/mutations/AddUserMutation.js.

所以我在这里做错了什么,我搜索了很多网站仍然能够找到解决方案。有人可以 help/clarify 我在这个 .

根据您的架构,createUser 突变解析为 UserPayload 类型的对象。该类型在您的架构中只有两个字段:

type UserPayload {
  clientMutationId: String
  node: User
}

但是,您的查询实际上是在请求 data 字段,UserPayload 类型不存在该字段。您需要修改查询以请求正确的字段,例如:

const mutation = graphql`
  mutation AddUserMutation($input:  CreateUserInput!) {
  createUser(input: $input) {
      node: {
        firstName
        lastName
        email
        password
      }
    }
  }
`;

您可能还需要更改架构,因为 CreateUserData 应该是 input 而不是 type