简体   繁体   English

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

[英]Can't make correct GraphQL query to Yelp

Maybe this thing is obvious but I'm new to Apollo. 也许这件事很明显,但我是Apollo的新手。 I'm trying to send a simple graphQL query to yelp server 我正在尝试向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'),
);

tried: query1: 尝试过:query1:

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

error: Uncaught (in promise) Error: Network error: Unexpected token < in JSON at position 0 错误:未捕获(承诺)错误:网络错误:意外令牌<在JSON中的位置0

query2: 查询2:

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

error: "Syntax Error: Expected $, found Name "id"" 错误:“语法错误:应为$,找到名称为“ id””

query3: 查询3:

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

error: Uncaught (in promise) Error: Network error: Unexpected token < in JSON at position 0 错误:未捕获(承诺)错误:网络错误:意外令牌<在JSON中的位置0

I'm getting data with postman https://imgur.com/hoC7s5e 我正在通过邮递员https://imgur.com/hoC7s5e获取数据

import ApolloClient from 'apollo-boost'; 从'apollo-boost'导入ApolloClient;

I was trying to apply configuration for apollo-client client to apollo-boost client 我正在尝试将配置用于apollo-client客户端到apollo-boost客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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