Steps for configuring Types in Graphql Codegen

I have successfully implemented a Vue 3 component that utilizes Urql to query a Hasura graphql endpoint. The query is functioning properly, but I am now focused on enhancing the type safety of the component.

My approach involves using graphql Codegen to generate types. However, I am encountering errors in the generated file that need to be addressed.

Although relatively new to Typescript, I am eager to learn and rectify any issues identified in the types file obtained from the code generation process.

In order to seek assistance, I have provided the following:

  1. A copy of the functional ListTodos.vue component.
  2. An abbreviated version of the Generated Types file (graphql.d.ts) highlighting errors found in lines 975 - 989 for easier reference.
  3. A summary of the reported errors.
  4. The configuration file for my code generator (codegen.js).

Your suggestions and guidance would be highly appreciated.

1. ListTodos.vue

<script setup lang="ts">
    import { useQuery, gql } from '@urql/vue'

    const FETCH_TODOS = gql`
        query {
            todos {
                title
                user {
                    name
                }
                is_public
            }
        }
    `
    const result = useQuery({ query: FETCH_TODOS })
</script>

<template>
    <div v-if="result.fetching.value">Loading..</div>
    <div v-else-if="result.error.value">{{ result.error.value }}</div>
    <div v-else>
        <ul>
            <li v-for="todo of result.data.value.todos">
                <h4>{{ todo.title }}</h4>
                <p>{{ todo.user.name }}</p>
            </li>
        </ul>
    </div>
</template>

2. Generated Types (abbreviated) with error indicators https://i.sstatic.net/5wjvT.png

3. Errors

https://i.sstatic.net/DegOo.png

4. Codegen.js configuration file

module.exports = {
    overwrite: true,
    generates: {
        './src/generated/graphql.d.ts': {
            schema: [
                {
                    'https://sample-backend-for-hasura-tutorial.hasura.app/v1/graphql': {
                        headers: {
                            'x-hasura-role': 'admin',
                            'x-hasura-admin-secret': 'secret',
                        },
                    },
                },
            ],
            documents: ['./src/**/*.vue'],
            plugins: ['typescript', 'typescript-operations', 'typescript-vue-urql'],
            config: {
                preResolveTypes: true,
                skipTypename: false,
                enumsAsTypes: true,
                constEnums: true,
            },
        },
    },
}

Answer №1

Good news! The issue has been successfully resolved.

To fix it, I made the decision to remove the 'typescript-vue-urql' plugin from the configuration. Surprisingly, this action led to the generated file functioning without any errors.

In addition, I included a designated name for the query, imported this 'name' from the Types file, and then proceeded to type the useQuery function accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Include a control within a form based on the Observable response

I am looking to enhance my form by adding a control of array type, but I need to wait for the return of an Observable before mapping the values and integrating them into the form. The issue with the current code is that it inserts an empty array control e ...

Encountering an issue with Tiptap2 floating menu while using script setup in Vue 3

Currently, I am trying to integrate a floating menu in Vue 3 by following the guidelines in the tiptap documentation. However, I am facing difficulties as the floating menu does not seem to render properly. I am using script setup and not sure if that is c ...

What are the best practices for establishing a secure SignalR client connection?

While tackling this issue may not be solely related to SignalR, it's more about approaching it in the most efficient way. In C#, creating a singleton of a shared object is achievable by making it static and utilizing a lock to prevent multiple threads ...

Error: The class you are attempting to access is

Currently, I am utilizing Vite.js along with TypeScript. My TypeScript file is located in 'src/vmodel/VGraph'. Within the index.html file, I import the aforementioned file as follows: <script type="module" src="/src/vmodel/VGrap ...

Encountered an error stating "Cannot find module node:fs" while using eslint-typescript-import, eslint-import-resolver-typescript,

Challenge My attempt to configure path alias in my TypeScript project was met with failure. Two errors arose during the execution of npm start: Module not found: Error: Can't resolve '~/App' in 'D:\work\workbench\templa ...

Tips for retrieving a nested data value within an array

I am currently puzzled by the undefined error I encounter when attempting to access a value using dot notation. The following illustrates my point: My goal is to retrieve the value from within the nested object in the headline color array: ...

The properties are not found in the type 'Observable<Todo[]>'

I'm currently following a YouTube tutorial and I've hit a roadblock trying to figure out where the error is originating from? Error Message: Type 'Observable' is missing properties such as length, pop, push, concat, and 25 more.ts(2740 ...

Vue 3 Option api: Issue with v-model input not propagating from child component to parent element

I am currently working on a new project using Nuxt 3, and I have encountered an issue with a contact form where the input values from a child component are not being received by the parent element. Let's dive into the code breakdown: Parent Component ...

Changing the port of an https (http) URL: A step-by-step guide

Initially, I have a website set up on nginx and Ubuntu 20.04 running on port 80 for http and port 443 for https, accessible through the URL https://mysite.cc (It is functioning correctly). Now, I am looking to add another site using Spring Cloud (Docker) ...

'Unable to locate the identifier "system" in Angular 2' - troubleshoot this unique error

My routes file contains a reference to 'system' which is causing an error in my Visual Studio Code. The error states that it cannot find the name 'system'. I have attached a screenshot for reference: https://i.sstatic.net/W2oD2.png ...

Typescript error: Object may be 'undefined' when using object literals

I am encountering an issue when trying to run the same code in my Typescript application that works fine in the browser console: [{ssid: 'Test Network 1', rssi: 54},{ssid: 'Test Network 2', rssi: 60}].find((rec) => 'Test Network ...

Failure to invoke Jest Spy

Currently, I am attempting to conduct a test utilizing the logging package called winston. My objective is to monitor the createlogger function and verify that it is being invoked with the correct argument. Logger.test.ts import { describe, expect, it, je ...

What is the best way to display user data exclusively in Angular?

How can I ensure that users only have access to their own information rather than the entire database? I attempted to use an if-else statement to filter out the data I need, as shown in the example below, but it was unsuccessful. custom-history.component ...

CompositeAPI: Referencing HTML Object Template - Error TS2339 and TS2533 when using .value to access Proxy Object

Having trouble referencing an element in VueJS 3 CompositeAPI. In my current implementation, it looks like this: <div ref="myIdentifier"></div> setup() { const myIdentifier = ref(null); onMounted(() => { console.log(myIden ...

Avoid These Mistakes When Combining Link URLs in VueJS

I'm embarking on a new project and I'm faced with an issue regarding rendering a list of links. The URLs of my links are stored in a table in the format of "www.sitename.com". They display correctly initially, but upon clicking on one of them, in ...

Simple methods for ensuring a minimum time interval between observable emittance

My RxJS observable is set to emit values at random intervals ranging from 0 to 1000ms. Is there a way to confirm that there is always a minimum gap of 200ms between each emission without skipping or dropping any values, while ensuring they are emitted in ...

Despite the session being expired, the Laravel API call still goes through successfully

My application is built as a Single Page Application using Laravel 5.8 and Vue 2.0. Currently, everything seems to be functioning smoothly, perhaps a bit too well. I noticed that even after deleting the session, I am able to perform actions such as saving ...

What is the reason behind Typescript flagging a potential undefined value when checking for array length using a greater than comparison but not with an

Consider the following excerpt from a React component: const AccountInformation = (props: { readonly accountData: AccountData | undefined | null }) => { const hasMultipleAccounts: boolean = props.accountData?.customerAccounts?.length === 1 ? false : t ...

The issue of updating a GLSL uniform variable during an animation in three.js using TypeScript remains unresolved

Running a three.js TypeScript application, I developed custom GLSL shaders using the THREE.ShaderMaterial() class. Now, I aim to enhance my code by switching to the THREE.MeshStandardMaterial class. This is an entirely new experience for me, and despite e ...