无法存储GraphQL查询的结果-始终未定义

时间:2019-01-10 18:36:53

标签: javascript node.js reactjs typescript graphql

我正在查询,并且已经记录了结果。一切正常,但是当我要继续处理结果时,它始终是“未定义的”(请参阅​​第二次日志记录)。 我敢肯定,我在这里缺少明显的东西,但真的很想得到您的帮助:)

$product = Product::where('slug', $slug)->firstOrFail();
$brand = $product->brands()->first(); //get the first brand
$brands = $product->brands; //get all brands for the product
$my_other_property = $product->properties()->where('identifier', 'my_other_property')->first(); //get first prop called 'my_other_property'

1 个答案:

答案 0 :(得分:0)

如果children给您正确的结果,您是否不仅可以将变量saved分配给this.props.apolloClient函数,并在subscribe调用中返回子代?

类似的东西:

 render() {
  const saved = this.props.apolloClient
  .watchQuery<Query>({
    query: gql`
      query($id: ID!) {
        element(id: $id) {
          children {
            ... on TextNote {
              name
              description
              type
            }
          }
        }
      }
    `,
    variables: { id: 0 }
  })
  .subscribe(({ data: { element: { children } } }) => {
    console.log(children); // this is the wanted result
    return children; // <-- return the value here
  });

console.log("save");
console.log(saved);   // <-- should have children assigned to saved