简体   繁体   中英

Create an entry to a REST API with post

I'm struggling a bit within a small project for fetching and creating (via POST) an entry, where I have on one side:

  • A GraphQL server (apollo)
  • A react app, using useQuery hook
  • A rest API, where the resolvers of the Apollo project is fetching data with async JS functions

I have the following obstacles: I'm not able to post an entry for the rest API via GraphQl query or Mutation. I have success in this post request:

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

The success response of this is:

{entry_id: 2}

In my graphQL schema:

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

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

and in the resolvers:

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

my ChannelEntriesSerives looks like:

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)

When I try to execute the mutation on the GraphQl studio:

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

I've got an error:

{
  "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
  }
}

What I'm doing wrong?

I found the reason for the error, and this is due to my rusty Axios basics. The config of an Axios request should have "data" property, so changing it to

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}`
    }

returns the correct response. The other issue is just a correct mapping of the response with graphql schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM