graphql - 使用中继现代 graphql 添加突变

标签 graphql relay relaymodern react-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: Unknown field data on type UserPayload. Source: document AddUserMutation file: js/mutations/AddUserMutation.js.

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

最佳答案

根据您的架构,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 应该是输入而不是类型

关于graphql - 使用中继现代 graphql 添加突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49280814/

相关文章:

java - 使用 Relay Modern 和 GraphQL 上传文件

Django Graphite 烯过滤器国外模型

graphql where 子句 Angular Apollo Client

graphql - 对于 AWS 放大中的架构,graphql 中的数组和映射是否有标量类型?

django - 如何为 django-graphql-auth 使用自定义用户模型

java - 用于 Java-GraphQL 服务器的 Java 中基于中继的分页

javascript - 如何将变量数据传递给createFragmentContainer

javascript - 使用 Relay 和 Graphql 在根 Node 上分页

ios - (550 5.1.1 Relay not allowed APPLE)我无法从 gmail 地址向私有(private)苹果地址发送电子邮件

reactjs - 找到路由器(用于中继现代) protected 身份验证路由