使用GraphQl片段用于整个返回值

发布于 2025-02-05 21:32:40 字数 1821 浏览 0 评论 0原文

我在查询中重复使用片段很难。

依赖项:

  • @apollo/client:3.5.6
  • GraphQl:15.8.0
  • React:17.0.2

我是从 @apollo/client导入的GQL。

这是我的片段:

export const totalRecipeReturnValFragment = gql`
  fragment totalRecipeReturnValFragment on RecipeOutput {
    _id
    instructions
    sections {
      name
      ingredients {
        name
        amount
        unit
        isRangedAmount
        topRange
        bottomRange
      }
    }
    name
    duration
    userId
    preparations {
      _id
      stringifiedTime
      occasion
      rating
      comment
    }
  }
`;

当我仅在突变中使用“变量”时,它可以按预期工作:

export const updateRecipe = gql`
  mutation updateRecipe($updatedRecipe: RecipeUpdateInput!) {
    updateRecipe(updatedRecipe: $updatedRecipe) {
      success
      errorMessage
      recipeAfterUpdate {
        ...totalRecipeReturnValFragment
      }
    }
  }
  ${totalRecipeReturnValFragment}
`;

但是,我有一些疑问,我想只使用片段,而没有“可变” 。

这是原始查询之一:

export default gql`
  query getUserRecipes($userId: String!) {
    getUserRecipes(userId: $userId) {
      _id
      instructions
      sections {
        name
        ingredients {
          name
          amount
          unit
          isRangedAmount
          topRange
          bottomRange
        }
      }
      name
      duration
      userId
      preparations {
        _id
        stringifiedTime
        comment
        occasion
        rating
      }
    }
  }
`;

这是我一直在尝试使用的内容:

export default gql`
  query getUserRecipes($userId: String!) {
    getUserRecipes(userId: $userId) {
      ...totalRecipeReturnValFragment
    }
  }
  ${totalRecipeReturnValFragment}
`;

以上查询编译,但是代码中的数据不包含任何字段。

有什么建议吗?谢谢!

I'm having trouble reusing fragments inside queries.

Dependencies:

  • @apollo/client: 3.5.6
  • graphql: 15.8.0
  • react: 17.0.2

I'm importing gql from @apollo/client.

This is my fragment:

export const totalRecipeReturnValFragment = gql`
  fragment totalRecipeReturnValFragment on RecipeOutput {
    _id
    instructions
    sections {
      name
      ingredients {
        name
        amount
        unit
        isRangedAmount
        topRange
        bottomRange
      }
    }
    name
    duration
    userId
    preparations {
      _id
      stringifiedTime
      occasion
      rating
      comment
    }
  }
`;

When I'm using just in a mutation, with a "variable", it works as expected:

export const updateRecipe = gql`
  mutation updateRecipe($updatedRecipe: RecipeUpdateInput!) {
    updateRecipe(updatedRecipe: $updatedRecipe) {
      success
      errorMessage
      recipeAfterUpdate {
        ...totalRecipeReturnValFragment
      }
    }
  }
  ${totalRecipeReturnValFragment}
`;

But, I have a few queries in which I'd like to use just the fragment, without a "variable".

This is one of the original queries:

export default gql`
  query getUserRecipes($userId: String!) {
    getUserRecipes(userId: $userId) {
      _id
      instructions
      sections {
        name
        ingredients {
          name
          amount
          unit
          isRangedAmount
          topRange
          bottomRange
        }
      }
      name
      duration
      userId
      preparations {
        _id
        stringifiedTime
        comment
        occasion
        rating
      }
    }
  }
`;

This is what I've been trying to use:

export default gql`
  query getUserRecipes($userId: String!) {
    getUserRecipes(userId: $userId) {
      ...totalRecipeReturnValFragment
    }
  }
  ${totalRecipeReturnValFragment}
`;

The above query compiles, but the data in the code doesn't contain any field.

Any suggestions? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2025-02-12 21:32:40

问题是我在我的ApolloClient中使用了缓存:New InMemoryCache({addTypename:addtyPename:false})。一旦我删除了{addTypename:false},它就像魅力一样。

The issue was that I used cache: new InMemoryCache({ addTypename: false }) in my ApolloClient. Once I removed the { addTypename: false }, it worked like a charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文