2

我正在尝试使用 relay 添加用户,下面是我的架构文件
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:data类型上的未知字段UserPayload。来源:文档AddUserMutation文件:js/mutations/AddUserMutation.js.

所以我在这里做错了什么,我搜索了许多网站仍然能够找到解决方案。有人可以帮助/澄清我吗?

4

1 回答 1

3

根据您的架构,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.

于 2018-03-14T15:09:27.920 回答