无法对Yelp进行正确的GraphQL查询

时间:2018-07-24 20:12:58

标签: reactjs api graphql apollo yelp

也许这件事很明显,但是我是Apollo的新手。 我正在尝试向yelp服务器发送一个简单的graphQL查询

import React from 'react';
import { render } from 'react-dom';
import ApolloClient from 'apollo-boost';
import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import gql from 'graphql-tag';
import { ApolloProvider } from 'react-apollo';
import { yelpCredentials } from '../../../keys/yelp/config';
import 'cross-fetch/polyfill';
import App from './components/App';

const httpLink = createHttpLink({
  uri: 'https://api.yelp.com/v3/graphql',
});
const authLink = setContext((_, { headers }) => {
  const token = yelpCredentials.API_Key;
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : '',
      'Content-Type': 'application/graphql',
    },
  };
});
const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
});

client.query(
 ***[QUERY_CONTENT]***
 ,
}).then(console.log);

render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById('app'),
);

尝试: 查询1:

client.query({
  query: gql`
  query business($id: String!) {
      business(id: $id) {
        name
        id
        alias
        rating
        url
      }
    }
  `,
  variables: { id: 'garaje-san-francisco' },
}).then(console.log);

错误:未捕获(承诺)错误:网络错误:意外令牌

query2:

client.query({
  query: gql`
      query business(id: "garaje-san-francisco") {
        name
        id
        alias
        rating
        url
      }
    }
  `,
}).then(console.log);

错误:“语法错误:应为$,找到名称为“ id””

query3:

 client.query({
  query: gql`
    query business($id: String!) {
      name
      id
      alias
      rating
      url
    }
  `,
  variables: {
    id: 'garaje-san-francisco',
  },
}).then(console.log);

错误:未捕获(承诺)错误:网络错误:意外令牌

我正在通过邮递员https://imgur.com/hoC7s5e来获取数据

1 个答案:

答案 0 :(得分:0)

从“ apollo-boost”导入ApolloClient;

我试图将apollo-client客户端的配置应用到apollo-boost客户端