无论如何,在Typescript

时间:2019-09-19 00:03:59

标签: typescript graphql

因此,我正在尝试确保客户端GraphQL查询的安全(因此,如果有更好的方法,请告诉我)。

但是我一直在做的就是这样定义我的查询。

export const tenantManagePageQuery = async (tenantId: string) =>
    graphQLClient.request<{
        tenants: TenantManagePageQueryTenant[];
    }>(
        /* GraphQL */ `
            query tenants($tenantId: String!) {
                tenants(tenantIds: [$tenantId]) {
                    id
                    description
                    name
                    approvedUsers {
                        id
                        alias
                    }
                    pendingUsers {
                        id
                        alias
                    }
                }
            }
        `,
        { tenantId },
    );

为了定义TenantManagePageQueryTenant类型,我要做这样的事情

interface TenantManagePageQueryTenant
    extends Pick<Tenant, 'id' | 'description' | 'name'> {}

基本租户模型是我的GQL模型类型。

无论如何,都可以执行这种Pick语句,但也可以选择嵌套的属性。

类似

interface TenantManagePageQueryTenant
    extends Pick<Tenant, 'id' | 'description' | 'name' | Pick<approvedUser| 'id' | 'alias'> {}

2 个答案:

答案 0 :(得分:2)

@Avius发布的代码在正确的轨道上,但是扩展交叉点类型的接口会产生错误。我相信您需要使用以下类型:

import requests


response = requests.post('https://gql.tokopedia.com/', data={"operationName":"SearchProductQuery","variables":{"params":"&ob=23&identifier=dapur_aksesoris-dapur_alat-pemotong-serbaguna&sc=3470&user_id=110487876&rows=60&start=1&source=directory&device=desktop&page=1&related=true&st=product&safe_search=false","adParams":"&page=1&dep_id=3470&ob=23&ep=product&item=15&src=directory&device=desktop&user_id=110487876&minimum_item=15&start=1&no_autofill_range=5-14"},"query":"query SearchProductQuery($params: String, $adParams: String) {\n  CategoryProducts: searchProduct(params: $params) {\n    count\n    data: products {\n      id\n      url\n      imageUrl: image_url\n      imageUrlLarge: image_url_700\n      catId: category_id\n      gaKey: ga_key\n      countReview: count_review\n      discountPercentage: discount_percentage\n      preorder: is_preorder\n      name\n      price\n      original_price\n      rating\n      wishlist\n      labels {\n        title\n        color\n        __typename\n      }\n      badges {\n        imageUrl: image_url\n        show\n        __typename\n      }\n      shop {\n        id\n        url\n        name\n        goldmerchant: is_power_badge\n        official: is_official\n        reputation\n        clover\n        location\n        __typename\n      }\n      labelGroups: label_groups {\n        position\n        title\n        type\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n  displayAdsV3(displayParams: $adParams) {\n    data {\n      id\n      ad_ref_key\n      redirect\n      sticker_id\n      sticker_image\n      productWishListUrl: product_wishlist_url\n      clickTrackUrl: product_click_url\n      shop_click_url\n      product {\n        id\n        name\n        wishlist\n        image {\n          imageUrl: s_ecs\n          trackerImageUrl: s_url\n          __typename\n        }\n        url: uri\n        relative_uri\n        price: price_format\n        campaign {\n          original_price\n          discountPercentage: discount_percentage\n          __typename\n        }\n        wholeSalePrice: wholesale_price {\n          quantityMin: quantity_min_format\n          quantityMax: quantity_max_format\n          price: price_format\n          __typename\n        }\n        count_talk_format\n        countReview: count_review_format\n        category {\n          id\n          __typename\n        }\n        preorder: product_preorder\n        product_wholesale\n        free_return\n        isNewProduct: product_new_label\n        cashback: product_cashback_rate\n        rating: product_rating\n        top_label\n        bottomLabel: bottom_label\n        __typename\n      }\n      shop {\n        image_product {\n          image_url\n          __typename\n        }\n        id\n        name\n        domain\n        location\n        city\n        tagline\n        goldmerchant: gold_shop\n        gold_shop_badge\n        official: shop_is_official\n        lucky_shop\n        uri\n        owner_id\n        is_owner\n        badges {\n          title\n          image_url\n          show\n          __typename\n        }\n        __typename\n      }\n      applinks\n      __typename\n    }\n    template {\n      isAd: is_ad\n      __typename\n    }\n    __typename\n  }\n}\n"})
print(response)

Playground Link

答案 1 :(得分:0)

不确定我是否正确理解了这个问题,但是交叉点类型会有所帮助吗?

假设approvedUsers实际上是一个数组,则类型可能看起来像这样:

interface TenantManagePageQueryTenant extends
    Pick<Tenant, 'id' | 'description' | 'name'>
    & { approvedUsers: Pick<ApprovedUser | 'id' | 'alias'>[] }
{}

这将是TenantManagePageQueryTenant类型的有效对象:

{
  id: "123",
  description: "456",
  name: "789",
  approvedUsers: [{
    id: "aaa",
    alias: "bbb"
  }]
}

而这些不会:

// missing alias in approvedUsers[0]

{
  id: "123",
  description: "456",
  name: "789",
  approvedUsers: [{
    id: "aaa"
  }]
}
// unknown field extra in approvedUsers[0]
{
  id: "123",
  description: "456",
  name: "789",
  approvedUsers: [{
    id: "aaa",
    alias: "bbb",
    extra: 345678
  }]
}