在 gql 中传递变量时出现 400 Bad request(Apollo 客户端)

400 Bad request when passing in variables in gql (Apollo client)

我试图将一些变量传递到我的 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}

您需要将变量转换为字符串!因为在查询中你期望变量类型为 string