【问题标题】:React useMutation doesn't pass the correct parameters inputReact useMutation 没有传递正确的参数输入
【发布时间】:2021-09-03 23:56:50
【问题描述】:

我正在尝试在一个项目中执行 Mutation,我将 Appollo GraphQL 作为中间件,将 reactJS 作为前端。

我有以下架构(想法是发送联系表格):

input entryIntput {
        url_title: String!
        title: String!
        channel_id: Int!
        entry_date: String!
        name: String!,
        motivation: String!
        mail: String!
        job_position: String
    }
...
type Mutation {
        createEntry(input: entryIntput): postEntrySuccess
    }

解析器:

  Mutation: {
    createEntry: (_, { input }) => channelEntriesService.postEntry(input)
  }

ChannelEntriesService:

    url_title: 'Application Form Entry-',
    title: 'Application Form Entry #',
    entry_date: Date.now(),
    channel_id: 3
  }) {
    const auth = await authenticate.auth()
    const nextEntry = await this.getNextEntry();

    const patch = {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      url: `${this._options.url}/create_channel_entry`,
      data: `channel_id=${entry.channel_id}&url_title=${entry.url_title}${nextEntry}&title=${entry.title} ${nextEntry}&name=${entry.name}&motivation=${entry.motivation}&job_position=${entry.job_position}&mail=${entry.mail}&entry_date=${entry.entry_date}&session_id=${auth.session_id}`
    }

    const response = await axios(patch);

    return response.data;
  }

我通过useMutation 执行队列并声明如下:

    const APPLY = gql`
      mutation PostEntry($input: entryIntput) {
        createEntry(input: $input) {
          entry_id
        }
      }
    `;
....
  const [createEntry, { error: formError, loading: formLoading }] = useMutation(APPLY);

在前端,我正在使用 React-Hook-Form:

  const onSubmit = (applicantData) =>
    createEntry({
      variables: {
        url_title: applicantData?.name?.trim(),
        entry_date: Date.now(),
        title: applicantData?.name,
        ...applicantData,
      },
    });

当我执行提交时,我的 channelEntries 中的 entry 参数有 undefined .postEntry(),

While I execute it through a mutation from the apollo studio like: 
mutation applicationEntryForm($entryInput: entryIntput) {
  createEntry(input: $entryInput) {
    entry_id
  }
}

query_variables: 
{
  "entryInput": {
    "channel_id": 3,
    "url_title": "my First Entry input",
    "entry_date": "1623829177995",
    "title": "Voila intruder!",
    "name": "Vlad",
    "job_position": "cleaner",
    "motivation": "My motivation notes",
    "mail": "vlado@vlado1.com"
  }
}

它具有正确的参数并且成功创建了一个条目。 我在前端“useMutation”和需要传递变量的提交函数中做错了什么?

我认为你提前阅读了这篇文章并给了我一些提示,因为我真的很累:(

【问题讨论】:

  • 从 ... createEntry({ variables: { entryInput <<<<< ... 开始,稍后在开发工具/网络中检查请求正文
  • 将变量下的属性名称更改为 entryInput 会为传递的参数提供相同的未定义值。关于检查标头请求的好建议:由于某种原因没有参数:operationName: "postEntry" query: "mutation postEntry($createEntryInput: entryIntput) { createEntry(input: $createEntryInput) { entry_id __typename }}" variables: {}
  • 下一个变量重命名?类型 [和输入类型] 名称通常以大写字符 (PascalCase) 开头 - 更容易识别 props/vars 与类型...在传递给突变调用程序 (createEntry) 之前准备并记录整个 variables ... 表单数据处理问题?显示所有表单[和数据/状态]相关的组件代码

标签: reactjs graphql react-hooks react-apollo apollo-server


【解决方案1】:

在将一些变量重命名为@xadm 后,我发现了以下内容:

我用的时候有区别:

  const [createEntry, { error: formError, loading: formLoading }] = useMutation(APPLY);

  const [createEntry, { error: formError, loading: formLoading }] = useMutation(APPLY, {});

例如为 options 参数传递一个空对象实际上会使事情发生。

【讨论】:

  • 它对网络请求正文(浏览器开发工具)真的有影响吗?显示它
  • 看来是这样,不带options参数我再试一次。
猜你喜欢
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多