JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2183
  • Score
    100M100P100Q124438F
  • License MIT

A GraphQL code generator plug-in that automatically generates utility functions for SWR.

Package Exports

  • graphql-codegen-plugin-typescript-swr

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (graphql-codegen-plugin-typescript-swr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

graphql-codegen-plugin-typescript-swr

A GraphQL code generator plug-in that automatically generates utility functions for SWR.

Example

# codegen.yml
# Add `plugin-typescript-swr` below `typescript-graphql-request`
generates:
  path/to/graphql.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-graphql-request
      - plugin-typescript-swr
config:
  rawRequest: false
// sdk.ts
import { GraphQLClient } from 'graphql-request'
import { getSdkWithHooks } from './graphql'

const sdk = getSdkWithHooks(
  new GraphQLClient(`${process.env.API_URL}/graphql`, { cache: typeof window === 'object' ? 'default' : 'no-cache' })
)

export default sdk
// Article.tsx
import sdk from '../sdk'

type StaticPropsParams = { slug: string }
export const getStaticProps: GetStaticProps<StaticProps, StaticPropsParams> = async ({
  params,
  preview = false,
}) => {
  if (!params) {
    throw new Error('Parameter is invalid!')
  }

  const { articleBySlug: article } = await sdk.GetArticle({
    slug: params.slug,
  })

  if (!article) {
    throw new Error('Article is not found!')
  }

  return {
    props: {
      slug: params.slug,
      preview,
      initialData: {
        articleBySlug: article
      }
    }
  }
})

export const Article = ({ slug, initialData, preview }: ArticleProps): JSX.Element => {
  const { data: { article } } = sdk.useGetArticle(
    'UniqueKeyForTheRequest', { slug }, { initialData }
  )
  // ...
}