简体   繁体   中英

typescript type from schema

I've got the following auto-generated GraphQL schema mapping:

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',
      },
    },
    ...
  }
}

I'd like to create a properly typed object I can hack around with inside my implementation corresponding to the __args object seen above.

Is this possible in typescript? In my main logic I've tried the following:

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

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

Which Vetur does seem to 'understand' what type we are using as it gives the tips: 在此处输入图像描述

But then lights up with the following error when I try to actually assign values to this type:

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

I'm hoping there's something very basic about Typescript I'm just missing here.

EDIT:

So looking more at the auto-generated GraphQL schema, I see a more likely candidate for the actual datatype I want to use externally:

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>;
  ...
}

Any idea what magic is necessary to import THAT 'args' object as a usable type in a calling program? The goal is to import something such that I'd have a usable object in my calling program like the manually declared:

    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>;
    }

Thanks for the heads up, this feature is missing, and I just opened an issue tracking this feature: https://github.com/gqless/gqless/issues/178

In the meantime, we can use:

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

This makes SuperArgs a fully usable type in the calling program without all the manual declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM