【问题标题】:400 Bad request when passing in variables in gql (Apollo client)在gql(Apollo客户端)中传入变量时出现400错误请求
【发布时间】:2021-04-30 10:42:42
【问题描述】:

我正在尝试将一些变量传递到我的 gql 字符串中,但似乎无法让它工作。手动传递数据时,查询有效。

  const UPDATE_WEATHER = gql`
  {
    weatherByLocation(
      latitude: "51.8917473"
      longitude: "-2.0877334999999997"
    ) {
      currently {
        summary
      }
    }
  }
`;

const WeatherInfo = ({ lat, long }) => {
  const { loading, error, data } = useQuery(UPDATE_WEATHER, {
    variables: { lat, long },
  });

  if (loading) return null;
  if (error) return `Error! ${error}`;

  return <h1>{data.weatherByLocation[0].currently.summary}</h1>;
};

我尝试按照指南here 进行操作,但没有成功。这是我用变量更新的 gql

const UPDATE_WEATHER = gql`
  query weatherByLocation($lat: String!, $long: String!) {
    weatherByLocation(latitude: $lat, longitude: $long) {
      currently {
        summary
      }
    }
  }
`;

但是,这会导致 400 错误请求。这是请求负载

operationName: "weatherByLocation"
query: "query weatherByLocation($lat: String!, $long: String!) {↵  weatherByLocation(latitude: $lat, longitude: $long) {↵    currently {↵      summary↵      __typename↵    }↵    __typename↵  }↵}↵"
variables: {lat: 51.891751299999996, long: -2.0877779}

【问题讨论】:

  • striiiiiiiiiiings

标签: reactjs graphql apollo-client


【解决方案1】:

您需要将变量转换为字符串!因为在查询中,您希望变量具有字符串类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    相关资源
    最近更新 更多