如何解决使用Apollo提取XML restapi返回意外令牌
我开始构建ApolloClient。看起来像这样
import { ApolloClient,InMemoryCache,HttpLink } from "@apollo/client"
//import { RestLink } from "apollo-link-rest"
import fetch from "isomorphic-fetch"
const xml2js = require("xml2js")
const parseXmlResponsetoJson = xml => {
// The function is not being fired
const { parseString } = xml2js
let jsonFeed = null
parseString(xml,function (err,result) {
jsonFeed = result
})
return jsonFeed
}
const restLink = new HttpLink({
uri:
"https://cors-anywhere.herokuapp.com/https://www.w3schools.com/xml/note.xml",responseTransformer: async response =>
response.text().then(xml => parseXmlResponsetoJson(xml)),fetch,})
export const client = new ApolloClient({
link: restLink,cache: new InMemoryCache(),})
提供商看起来像这样
import React from "react"
import { client } from "../apollo"
import { ApolloProvider as Provider } from "@apollo/client"
function ApolloProvider({ children }) {
return <Provider client={client}>{children}</Provider>
}
export default ApolloProvider
我尝试使用的查询如下所示
import React from "react"
import gql from "graphql-tag"
import { useQuery } from "@apollo/client"
const APOLLO_QUERY = gql`
{
note {
to
from
heading
body
}
}
`
const Component = () => {
const { loading,error,data } = useQuery(APOLLO_QUERY)
console.log("Apollo data",loading,data)
The error will return with Unexpected token < in JSON at position 2
// ...
我一直为此苦苦挣扎。我究竟做错了什么?我尝试使其非常简单地与w3schools一起使用xml示例https://www.w3schools.com/xml/note.xml
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。