使用 post 创建 REST API 的条目

问题描述

我在一个小项目中为获取和创建(通过 POST)一个条目而苦苦挣扎,我在一边:

  • GraphQL 服务器 (apollo)
  • 一个使用 useQuery 钩子的 React 应用
  • rest API,Apollo 项目的解析器在其中使用异步 JS 函数获取数据

我有以下障碍: 我无法通过 GraphQl 查询或 Mutation 发布其余 API 的条目。 我在此帖子请求中取得了成功:

POST https://technologytalents.io/space-cats/index.PHP/openapi/create_channel_entry
Accept: application/json
Content-Type: application/x-www-form-urlencoded
User-Agent: axios/0.21.1

channel_id=1&url_title=Blas&entry_date=12345678&title=Dooom&session_id=b288ea559b20c584a3a793685ceb20c240c26569

这个成功的响应是:

{entry_id: 2}

在我的 graphQL 架构中:

input entryIntput {
        url_title: String
        title: String
        channel_id: Int
        entry_date: Int
    }
type postEntrySuccess {
        entry_id: Int
    }

type Mutation {
        createEntry(input: entryIntput): postEntrySuccess
    }

在解析器中:

Mutation: {
    createEntry: async (_,entry) => await channelEntriesService.postEntry(entry)
  }

我的 ChannelEntriesSerives 看起来像:

const axios = require('axios')
const authenticate = require('./authenticate')

class ChannelEntries {

  constructor(options) {
    this._options = options
  }

  async getEntries() {
    const auth = await authenticate.auth()

    const patch = {
      ...options,url: `${options.url}/get_channel_entries?channel_id=1&where[status]=open&session_id=${auth.session_id}`
    }

    const response = await axios(patch)

    return response.data
  }

  async postEntry(entry = { url_title: 'Blas',title: 'Dooom',entry_date: Date.Now(),channel_id: 1 }) {
    const auth = await authenticate.auth()

    const patch = {
      method: 'POST',headers: {
        'Accept': 'application/json','Content-Type': 'application/x-www-form-urlencoded'
      },url: `${this._options.url}/create_channel_entry?channel_id=${entry.channel_id}&url_title=${entry.url_title}&title=${entry.title}&entry_date=${entry.entry_date}_id=${auth.session_id}`
    }
    const response = await axios.request(patch)

    return response.data
  }
}

const options = {
  method: 'GET',url: 'https://technologytalents.io/space-cats/index.PHP/openapi',headers: {
    'Content-Type': 'application/json','Accept': 'application/json'
  }
}

module.exports = instance = new ChannelEntries(options)

当我尝试在 GraphQl 工作室上执行突变时:

mutation CreateEntry($createEntryInput: entryIntput) {
  createEntry(input: $createEntryInput) {
    entry_id
  }
}

我有一个错误

{
  "errors": [
    {
      "message": "Request Failed with status code 400","locations": [
        {
          "line": 2,"column": 3
        }
      ],"path": [
        "createEntry"
      ],"extensions": {
        "code": "INTERNAL_SERVER_ERROR","exception": {
          "config": {
            "url": "https://technologytalents.io/space-cats/index.PHP/openapi/create_channel_entry?channel_id=undefined&url_title=undefined&title=undefined&entry_date=undefined_id=b3c77d7c74b0cc10de61c90f8e1a34b30e454f7a","method": "post","headers": {
              "Accept": "application/json","Content-Type": "application/x-www-form-urlencoded","User-Agent": "axios/0.21.1"
            },"transformRequest": [
              null
            ],"transformResponse": [
              null
            ],"timeout": 0,"xsrfCookieName": "XSRF-TOKEN","xsrfheaderName": "X-XSRF-TOKEN","maxContentLength": -1,"maxBodyLength": -1
          }
        }
      }
    }
  ],"data": {
    "createEntry": null
  }
}

我做错了什么?

解决方法

我找到了错误的原因,这是由于我生锈的 Axios 基础。 Axios 请求的配置应该具有“数据”属性,因此将其更改为

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}&title=${entry.title}&entry_date=${entry.entry_date}&session_id=${auth.session_id}`
    }

返回正确的响应。 另一个问题只是响应与 graphql 模式的正确映射。