使用GraphQl片段用于整个返回值
我在查询中重复使用片段很难。
依赖项:
- @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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我在我的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.