# codegen-functions

# Type: boolean

Sgts can generate all your methods fully typed and cancellabled; It uses Apollo Client behing the hood and as a dependency

WARNING

Support for Subcriptions in development

# Exemple

sgts -e https://json-placeholder-graphql.herokuapp.com/graphql --codegen-functions

You just need to pass yout apolloClient instance to the root constructor of sgts

Generated result exemple

{
  posts(): FragmentableQueryWithOptionalArgs<Post[], postsArgs> {
    return {
      $fragment: (fragment: string | DocumentNode) => {
        const { isString, isFragment, fragmentName } = guessFragmentType(fragment);
        const query = gql`
          query posts ($userId: Int) {
            posts(userId: $userId) {
              ${isString ? fragment : '...' + fragmentName}
            }
          } ${isFragment ? fragment : ''}
          `;

        return abortableQuery(query, true);
      }
    };
  },
}

# Usage

TIP

apiProvider is generated by sgts on the same files as your models with the option --codegen-functions

TIP

You can specify the apollo version you use (2 or 3) with the option --apolloVersion; It will default to 3.

import { apiProvider } from '~/generated.ts';
import ApolloClient from '@apollo/client/core';

const apolloClient = new ApolloClient({
  ...
});

const sgts = apiProvider(apolloClient);


// You can either use a GraphQL fragment

const commentFragment = gql`
  fragment commentFragment on Comment {
    id
    name
    body
  }
`
// Or a simple string

const commentFragment = `
  id
  name
  body
`

const commentsQuery = sgts.comments(commentFragment);
// Or
const commentsQuery = sgts.comments(); // This will take the auto-generated fragment by default


const result = await commentsQuery.$args({postId: 5}).$fetch();

// You can cancel your request any time

commentsQuery.$abort();

TIP

Also, Sgts will generate all fragments by default

Sgts handle the fragment generation,

Exemple from generated project in production

export const ReportConnectionFragment = gql`
  fragment ReportConnectionFragment on ReportConnection {
    edges {
      node {
        uuid
        category
        status
      }
      cursor
    }
    aggregate {
      count
    }
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
  }
`;

return {
  /** Get list of reports */
  reports(): FragmentableQueryWithArgs<ReportConnection, reportsArgs> {
    const defaultQuery = gql`
      query reports($where: ReportWhereInput!, $first: Int, $after: ID) {
        reports(where: $where, first: $first, after: $after) {
          ...ReportConnectionFragment
        }
      }
      ${ReportConnectionFragment}
    `;
    return {
      $fragment: (fragment: string | DocumentNode) => {
        const { isString, isFragment, fragmentName } = guessFragmentType(fragment);
        const query = gql`
            query reports ($where: ReportWhereInput!,$first: Int,$after: ID) {
              reports(where: $where,first: $first,after: $after) {
                edges { node {${
                  isString ? fragment : '...' + fragmentName
                }} cursor } aggregate { count } pageInfo { hasNextPage hasPreviousPage startCursor endCursor } 
              }
            } ${isFragment ? fragment : ''}
            `;
        return abortableQuery(query, true, false);
      },
      ...abortableQuery(defaultQuery, true, false),
    };
  },
};