javascript - Urql错误: cannot read property of variable name undefined,并且我正确定义了它

标签 javascript react-redux graphql react-hooks urql

这是我的问题:

const GraphComponent = () => {
  const { GraphQuery } = useContext(GlobalContext);
  const query = `
  query($input: MeasurementQuery ){
      getMeasurements(input: $input){
      metric,
      value,
      unit,
      at
      }
  }
  `;
  const [{ fetching, data, error }] = useQuery({
    query,
    variables: {
      GraphQuery,
    },
  });
  useEffect(() => {
    const fun = () => {
      if (fetching) return <p>Loading...</p>;
      if (error) console.log(error.message);
      if (!data) return;
      console.log(data.getMeasurements);
    };
    fun();
  }, [fetching, error, data]);

  return <div></div>;
};

export default GraphComponent;

“GraphQuery”变量是一个如下所示的对象

obj = {
      metricName: deets,
      after: Date.now() - 36000000,
      before: Date.now(),
    };

Playground 上接受变量的 graphql 查询如下所示

{
  getMeasurements(input:{metricName:"tubingPressure", after:1587371198855,before:1587370098855}){
    metric,
    value,
    unit,
    at
  }
}

但是我不断收到的错误是

[GraphQL] Cannot read property 'metricName' of undefined

最佳答案

useQuery内尝试将代码更改为:

variables: {
      input: GraphQuery,
    },
``

关于javascript - Urql错误: cannot read property of variable name undefined,并且我正确定义了它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61318263/

相关文章:

javascript - 重新加载 Flexslider

javascript,promises,如何在 then 范围内访问变量 this

javascript - 始终滚动 div 元素而不是页面本身

javascript - 防止 react 路由器检测位置变化

javascript - 尝试将 React 连接到 redux

android - 在带有 Kotlin 和流程的 Android 上使用带有 Apollo 的 GraphQL 订阅时处理网络错误

graphql - 使用 nexus-prisma graphql 处理文件上传

javascript - 从回调函数内部继续 forEach?

unit-testing - 使用 jest 测试 react-native - redux app

javascript - 如何解析 GraphQL 中的嵌套类型?