【问题标题】:Error with calling graphQl mutation from react app从反应应用程序调用graphQl突变时出错
【发布时间】:2020-10-07 10:58:17
【问题描述】:

我尝试从 react 应用调用简单的 graphQl 突变。是变异描述:

createOrganization(
organization: OrganizationInput!
): Organization

type OrganizationInput {
name: String!
type: String
billingAddress: AddressInput
shippingAddress: AddressInput
}

我尝试称它为:

const CREATE_ORGANIZATION = gql`
  mutation createOrganization($organization: OrganizationInput!) {
    _id
  }
`;
const [mutation] = useMutation(CREATE_ORGANIZATION);
...
mutation({
          variables: { name: '' },
        });

不幸的是,graphQl 没有提供任何可读的日志,我有:

Unhandled Rejection (Error): Network error: Response not successful: Received status code 400
new ApolloError
src/errors/ApolloError.ts:46
  43 | // Constructs an instance of ApolloError given a GraphQLError
  44 | // or a network error. Note that one of these has to be a valid
  45 | // value or the constructed error will be meaningless.
> 46 | constructor({
     | ^  47 |   graphQLErrors,
  48 |   networkError,
  49 |   errorMessage, 

在网络中,我看到调用了突变,但我不知道当您使用 graphQl 时出现什么问题以及如何处理问题最重要。

附言它正在起作用 - 从 playgraund 调用它不是问题。

【问题讨论】:

  • 让它在操场上工作使用“查询变量”(只有一个“组织”参数)-graphql.org/learn/queries/#variables ...然后在代码中重新创建
  • 更改为mutation({ variables: { organization: { name: '' } }, });它仍然不起作用。在操场上没问题
  • 语法仍然不好
  • 我不明白...我看到很多样本,而且到处都是相同的简单方法。
  • 只是硬编码误导......遵循这个文档和这种传递变量的方式

标签: reactjs graphql apollo-client


【解决方案1】:

我发现了我的错误。它在gql中。正确的版本是:

const CREATE_ORGANIZATION = gql`
  mutation($organization: OrganizationInput!) {
    createOrganization(organization: $organization) {
      _id
    }
  }
`;

【讨论】:

    猜你喜欢
    • 2019-03-18
    • 2019-07-20
    • 2020-12-12
    • 1970-01-01
    • 2019-01-24
    • 2019-01-09
    • 2018-10-03
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多