Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to generate types #313

Closed
XPOL555 opened this issue Jan 27, 2023 · 3 comments
Closed

how to generate types #313

XPOL555 opened this issue Jan 27, 2023 · 3 comments
Labels
question Further information is requested

Comments

@XPOL555
Copy link

XPOL555 commented Jan 27, 2023

In documentation there is a reference to types: https://strapi.nuxtjs.org/usage#types

Also in asyndata https://strapi.nuxtjs.org/advanced#async-data
there is a reference to ~/types

<script setup lang="ts">
import type { Restaurant } from '~/types'

const route = useRoute()
const { findOne } = useStrapi4()

const { data, pending, refresh, error } = await useAsyncData(
  'restaurant',
  () => findOne<Restaurant>('restaurants', route.params.id)
)
</script>

Where is this file generated? Documentation is totally unclear in this part for me.

@XPOL555 XPOL555 added the question Further information is requested label Jan 27, 2023
Copy link
Member

benjamincanac commented Jan 27, 2023

Hello @XPOL555,

In the documentation the types are an example of what you might have in your nuxt app. Those are not auto-generated, you have to define them yourself by creating a types/index.ts for example:

export interface Restaurant {
  id: number
  name: string
}

@XPOL555
Copy link
Author

XPOL555 commented Jan 27, 2023

Thank you @benjamincanac

I've used graphql-codegen a lot and this might be interesing. I hate passing through graphql but it helps in this case.

This is a basic codegen.ts file

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  overwrite: true,
  schema: "schema.graphql",
  documents: "graphql/**/*.graphql",
  generates: {
    "gql.ts": {
      plugins: [
          "typescript"
      ]
    }
  }
};

export default config;

Then you run yarn codegen and it will generate a gql.ts file

So you can import the type from that file like this

<script lang="ts" setup>

import {Frontpage} from "~/gql";

const { findOne } = useStrapi<Frontpage>()
const {data} = await findOne<Frontpage>('frontpage')
console.log(data)


</script>

ℹ️ Be sure to have the strapi graphql plugin enabled and the route /graphql exposed.

You can use a schema url also (see the docs)

@XPOL555 XPOL555 closed this as completed Jan 27, 2023
Copy link
Member

@XPOL555 Thanks for your example! This would be a nice addition to the advanced section of the documentation if you have some spare time 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants