You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apollo GraphQL中同时使用@include指令与字段参数

解决方案

你可以直接将$after和$first参数挂载到reviews字段上,字段参数和@include指令可以共存,只需要将参数放在字段名后、指令之前的位置即可,修改后的代码如下:

export const GET_AUTHORIZED_USER = gql`
    query GetAuthorizedUser(
        $includeReviews: Boolean!
        $after: String
        $first: Int
    ) {
        authorizedUser {
            id
            username
            reviews(after: $after, first: $first) @include(if: $includeReviews) {
                pageInfo {
                    hasPreviousPage
                    hasNextPage
                    startCursor
                    endCursor
                }
                edges {
                    cursor
                    node {
                        id
                        repositoryId
                        rating
                        text
                        createdAt
                        user {
                            username
                        }
                    }
                }
            }
        }
    }
`;

即使$includeReviews值为false,你传入的分页参数也不会产生多余的查询开销,不需要单独拆分拉取reviews的查询语句,完全满足你的需求。

内容的提问来源于stack exchange,提问作者Didzis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 05:24:03