1

我有以下自动生成的 GraphQL 模式映射:

export const generatedSchema = {
  query: {
    __typename: { __type: 'String!' },
    account_sample: {
      __type: '[account_sample!]!',
      __args: {
        distinct_on: '[account_sample_select_column!]',
        limit: 'Int',
        offset: 'Int',
        order_by: '[account_sample_order_by!]',
        where: 'account_sample_bool_exp',
      },
    },
    ...
  }
}

我想创建一个正确类型的对象,我可以在我的实现中使用与上面看到的 __args 对象相对应的方法。

这在打字稿中可能吗?在我的主要逻辑中,我尝试了以下方法:

import { generatedSchema } from '../generated/graphql';

let parms: typeof generatedSchema.query.account_sample.__args;
parms.limit = 400;

哪个 Vetur 似乎确实“理解”了我们使用的类型,因为它给出了提示: 在此处输入图像描述

但是当我尝试将值实际分配给这种类型时,会出现以下错误:

(property) limit: "Int"
Cannot assign to 'limit' because it is a read-only property.  Vetur(2540)

我希望我只是在这里缺少关于 Typescript 的一些非常基本的东西。

编辑:

因此,更多地查看自动生成的 GraphQL 模式,我发现我想在外部使用的实际数据类型更有可能是候选者:

export interface Query {
  __typename: 'Query' | undefined;
  account_sample: (args?: {
    distinct_on?: Maybe<Array<account_sample_select_column>>;
    limit?: Maybe<Scalars['Int']>;
    offset?: Maybe<Scalars['Int']>;
    order_by?: Maybe<Array<account_sample_order_by>>;
    where?: Maybe<account_sample_bool_exp>;
  }) => Array<account_sample>;
  ...
}

知道在调用程序中将“args”对象作为可用类型导入需要什么魔法吗?目标是导入一些东西,以便在我的调用程序中有一个可用的对象,比如手动声明的:

    interface SuperArgs {
      distinct_on?: Maybe<Array<account_sample_select_column>>;
      limit?: Maybe<Scalars['Int']>;
      offset?: Maybe<Scalars['Int']>;
      order_by?: Maybe<Array<account_sample_order_by>>;
      where?: Maybe<account_sample_bool_exp>;
    }
4

1 回答 1

1

感谢您的提醒,缺少此功能,我刚刚打开了一个跟踪此功能的问题:https ://github.com/gqless/gqless/issues/178

同时,我们可以使用:

type SuperArgs = Parameters<typeof Query.account_sample>[0]

这使得 SuperArgs 成为调用程序中完全可用的类型,无需所有手动声明。

于 2021-04-07T13:27:42.320 回答