【问题标题】:AddMutation using relay modern graphqlAddMutation 使用中继现代 graphql
【发布时间】:2018-08-23 03:55:59
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

    标签: graphql relay relaymodern react-relay


    【解决方案1】:

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

    【讨论】:

    • 完美。 .. :)
    猜你喜欢
    • 2017-11-01
    • 2017-06-14
    • 2019-10-12
    • 2018-02-14
    • 2017-05-13
    • 2017-05-04
    • 2019-09-19
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多