Skip to content

A type error occurs when options autogenSWRKey and useSWRInfinite. #204

@yoshi6jp

Description

@yoshi6jp
  • I'm submitting a ...
    [x] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    A type error occurs when options autogenSWRKey and useSWRInfinite.

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

# schema.graphql
type Post {
  id: Int!
  title: String!
}

type Query {
  allPosts(q: String): [Post]!
}
# client.graphql
query AllPosts($q: String) {
  allPosts(q: $q) {
    id
    title
  }
}
# codegen.yaml
overwrite: true
generates:
  sdk.ts:
    schema: ./schema.graphql
    documents: ./client.graphql
    plugins:
      - typescript
      - typescript-operations
      - typescript-graphql-request
      - plugin-typescript-swr
config:
  autogenSWRKey: true
  useSWRInfinite:
    - AllPosts
npx tsc --noEmit sdk.ts
sdk.ts:95:80 - error TS2345: Argument of type 'Key' is not assignable to parameter of type 'string'.
  Type 'boolean' is not assignable to type 'string'.

95         utilsForInfinite.generateGetKey<AllPostsQuery, AllPostsQueryVariables>(genKey<AllPostsQueryVariables>('AllPosts', variables), getKey),
                                                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in sdk.ts:95
# sdk.ts
import { GraphQLClient } from 'graphql-request';
import * as Dom from 'graphql-request/dist/types.dom';
import gql from 'graphql-tag';
import { ClientError } from 'graphql-request/dist/types';
import useSWR, { SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';
import useSWRInfinite, { SWRInfiniteConfiguration } from 'swr/infinite';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
  ID: string;
  String: string;
  Boolean: boolean;
  Int: number;
  Float: number;
};

export type Post = {
  __typename?: 'Post';
  id: Scalars['Int'];
  title: Scalars['String'];
};

export type Query = {
  __typename?: 'Query';
  allPosts: Array<Maybe<Post>>;
};


export type QueryAllPostsArgs = {
  q?: InputMaybe<Scalars['String']>;
};

export type AllPostsQueryVariables = Exact<{
  q?: InputMaybe<Scalars['String']>;
}>;


export type AllPostsQuery = { __typename?: 'Query', allPosts: Array<{ __typename?: 'Post', id: number, title: string } | null> };


export const AllPostsDocument = gql`
    query AllPosts($q: String) {
  allPosts(q: $q) {
    id
    title
  }
}
    `;

export type SdkFunctionWrapper = <T>(action: (requestHeaders?:Record<string, string>) => Promise<T>, operationName: string, operationType?: string) => Promise<T>;


const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action();

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
  return {
    AllPosts(variables?: AllPostsQueryVariables, requestHeaders?: Dom.RequestInit["headers"]): Promise<AllPostsQuery> {
      return withWrapper((wrappedRequestHeaders) => client.request<AllPostsQuery>(AllPostsDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'AllPosts', 'query');
    }
  };
}
export type Sdk = ReturnType<typeof getSdk>;
export type SWRInfiniteKeyLoader<Data = unknown, Variables = unknown> = (
  index: number,
  previousPageData: Data | null
) => [keyof Variables, Variables[keyof Variables] | null] | null;
export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
  const sdk = getSdk(client, withWrapper);
  const utilsForInfinite = {
    generateGetKey: <Data = unknown, Variables = unknown>(
      id: string,
      getKey: SWRInfiniteKeyLoader<Data, Variables>
    ) => (pageIndex: number, previousData: Data | null) => {
      const key = getKey(pageIndex, previousData)
      return key ? [id, ...key] : null
    },
    generateFetcher: <Query = unknown, Variables = unknown>(query: (variables: Variables) => Promise<Query>, variables?: Variables) => (
        id: string,
        fieldName: keyof Variables,
        fieldValue: Variables[typeof fieldName]
      ) => query({ ...variables, [fieldName]: fieldValue } as Variables)
  }
  const genKey = <V extends Record<string, unknown> = Record<string, unknown>>(name: string, object: V = {} as V): SWRKeyInterface => [name, ...Object.keys(object).sort().map(key => object[key])];
  return {
    ...sdk,
    useAllPosts(variables?: AllPostsQueryVariables, config?: SWRConfigInterface<AllPostsQuery, ClientError>) {
      return useSWR<AllPostsQuery, ClientError>(genKey<AllPostsQueryVariables>('AllPosts', variables), () => sdk.AllPosts(variables), config);
    },
    useAllPostsInfinite(getKey: SWRInfiniteKeyLoader<AllPostsQuery, AllPostsQueryVariables>, variables?: AllPostsQueryVariables, config?: SWRInfiniteConfiguration<AllPostsQuery, ClientError>) {
      return useSWRInfinite<AllPostsQuery, ClientError>(
        utilsForInfinite.generateGetKey<AllPostsQuery, AllPostsQueryVariables>(genKey<AllPostsQueryVariables>('AllPosts', variables), getKey),
        utilsForInfinite.generateFetcher<AllPostsQuery, AllPostsQueryVariables>(sdk.AllPosts, variables),
        config);
    }
  };
}
export type SdkWithHooks = ReturnType<typeof getSdkWithHooks>;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions