【问题标题】:How to use arguments with include directive in Apollo GraphQL?如何在 Apollo GraphQL 中使用带有 include 指令的参数?
【发布时间】:2021-11-01 01:31:20
【问题描述】:

所以对于初学者来说,这是我的代码:

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

reviews 接受 $after$first 参数,但我的问题是我不知道如何添加它们。如果我删除 @includes 指令并添加参数,它工作正常。就我而言,我希望同时拥有指令和参数。

我怎样才能做到这一点?我知道我可以单独查询来获取评论,但我想使用指令。

【问题讨论】:

    标签: graphql apollo


    【解决方案1】:

    因此,显然在@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
                            }
                        }
                    }
                }
            }
        }
    `;
    

    【讨论】:

      猜你喜欢
      • 2019-07-31
      • 2021-06-18
      • 2021-04-03
      • 2021-01-10
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 2021-08-31
      相关资源
      最近更新 更多