TypeScript Resolver Types for GraphQL Yoga (utilizing Prisma2 in the context)

Apologies for my English

I am struggling to create types for resolvers on GraphQL yoga. I attempted to generate them using graphql-codegen, but the resulting types are behaving strangely. Additionally, they do not include context types for Prisma2.

Here is my code snippet from codegen.yml:

schema: ./src/schema.graphql
generates:
  ./src/resolvers/resolvers-types.ts:
    plugins:
      - typescript
      - typescript-resolvers
config:
  contextType: './context#Context'

In my context.ts file, I have defined the context type as follows:

import {PrismaClient} from '@prisma/client'

export type Context = { prisma:PrismaClient }

And in my resolvers.ts file, here is how I import and define the resolvers with StringIndexed:

import {IResolvers, Mutation} from './resolvers-types'

interface StringIndexSignatureInterface {
    [index: string]: any
}

type StringIndexed<T> = T & StringIndexSignatureInterface

const resolvers: StringIndexed<IResolvers> = {
Query: {
        users: (parent, args, ctx, info) => {
            return ctx.prisma.user.findMany
        }
    }
};

I am encountering an error related to type incompatibility when I exclude StringIndexed. It seems necessary for the resolvers to work properly. Can someone please advise me on how to resolve this issue?

Answer №1

I have come up with a solution by adding the contextType to codegen.yml.

codegen.yml

generates:
  ./src/resolvers/resolvers-types/resolvers-types.ts:
    plugins:
      — typescript
      - typescript-resolvers
      
config:
  contextType: './context#Context'
  enumsAsTypes: true

context.ts

import {PrismaClient} from '@prisma/client'

export type Context = { prisma: PrismaClient }

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

You are able to use a null type as an index in angular.ts(2538) error message occurred

onClick() { let obj = { fName: "ali", LName: "sarabi", age: "19", } let fieldName = prompt("field"); alert(obj[fieldName]); } I encountered an issue with the code above where alert(obj[fieldName] ...

Discovering the cause of the "Module not found" error for nrwl packages

In my project, I have integrated nrwl.io. I have set up multiple libraries: ng g lib rest //successfully created ng g lib services //successfully created ng g lib models //created without errors, but encountering "Cannot find module" issue later on! Ev ...

Developing a hidden entity that adopts an interface with multiple functions that have been overloaded

My TypeScript interface includes a single function named "send" with two different allowed signatures. export interface ConnectionContext { send(data: ConnectionData): void; send(data: ConnectionData, timeout: number): Promise<ConnectionData> ...

Cannot assign a value of type X to type T

export function retrieveEmpty<T extends { data: any[] }>(): Observable<T> { const response: T = { data: [] }; return of(response); } encountering a compilation error stating Type '{ data: undefined[]; }' is not compatible with ...

Combinations of Typescript dependent unions

I'm struggling with calling the given union that wraps a function and its argument. Is there a way to call it without having to cast? type Wrapper = { fn: (a: string) => void arg: string } | { fn: (a: number) => void arg: number } let f ...

Conflicts arise when trying to create several objects with different material types in ThreeJS within the

Adding a star to the scene caused all objects in the scene to turn white and the perspective of the objects to glitch. Switching the materialStar to new THREE.MeshBasicMaterial fixed the rendering issue. It appears that the problem stems from having multip ...

Changing the value of a property in an object based on the object's attribute in JavaScript

I have a JSON data set like this: inputData = [ { id : 179849, name : alex , lastname: sanchez}, { id : 788539, name : Paul, lastname: bearer}, { id : 282169, name : Jean, lastname: nobel}, ... { id : 632785, name : Maria, lastname: parak} ] I am looking ...

The NX monorepo from @nrwl is unable to locate the .svgr configuration file within the lib directory

Recently, I started working with NX Monorepo that consists of 2 separate react applications. In order to share icons between these apps, I decided to create an icons library. I made changes to the project.json file of the icons library and added a svg com ...

The most effective method for implementing a loading or busy modal in Angular 2

Just a small inquiry - what is the most effective way to implement a loading/busy modal that can be used across multiple screens in your application? Would it be better to create a component with ngStyle to display it, and should you include the logic fo ...

Despite declaring a default export, the code does not include one

Software decays over time. After making a small modification to a GitHub project that was three years old, the rebuild failed due to automatic security patches. I managed to fix everything except for an issue with a default import. The specific error mess ...

Dealing with Errors in a POST Request using Angular

I have been following the instructions provided in the official Angular documentation found at https://angular.io/guide/http. I encountered an issue with the handleError function within the post request, as it requires 2 arguments and is displaying an err ...

Converting retrieved data into table cells through mapping

I'm facing an issue where I need to display specific addresses for each individual in a table row. For simplicity, let's focus on showing the city specifically as described in this code snippet: https://i.stack.imgur.com/bJmsD.png Currently, whe ...

What is the best way to retrieve a string from a lambda expression?

I am trying to call the function myFunction() and retrieve the source._id value, but I'm encountering issues with the current code. Even though source._id is correctly filled, I am unsure of how to successfully return it in its entirety. Ideally, some ...

Finding a way to detect the mouse leaving two elements simultaneously in Angular4

Are you looking for the Angular4 equivalent of a particular jQuery functionality? Do you need to add multiple elements dynamically and are experiencing issues with the settimeout function in Angular4? Any thoughts on how to solve this? http://jsfiddle.net ...

Issue with synchronization: Delay execution until all fs.readFile calls have finished

I am facing a challenge where I need to extract columns from multiple CSV files and combine them into an array. My current approach involves using fs.readFile to read the CSV files and a callback function that processes the data and adds a new element to t ...

How to generate a prop with an ID-value pair in Vue using JavaScript

When working with TypeScript in Vue components, I have come across the following way to initialize props: @Prop({ type: Object }) tabDetails: tabDetailsTypes The structure of the tabDetailsTypes looks like this: export interface tabDetailsTypes { ...

Challenges faced with implementing Tailwind CSS within the pages directory of NextJS websites

Issue with Tailwind Styles I've encountered a problem where the Tailwind styles are not being applied to components in the /pages directory of my NextJS project. Oddly enough, the same component works fine when used outside the pages directory. When ...

Delete some words from the phrase

Within my programming, I am utilizing an Azure Function to retrieve data from AzureSQL. The returned string in a specific column appears as follows -> Word, a. s.\1002: SomeWord\7010: AnotherWord\7300: AnotherOneWord\7304: LastWord. ...

Why is it that the HttpClient constructor in Angular doesn't require parameters when instantiated through the constructor of another class, but does when instantiated via the 'new' keyword?

I am trying to create a static method for instantiating an object of a class, but I have encountered a problem. import { HttpClient } from '@angular/common/http'; export MyClass { // Case 1 public static init(): MyClass { return this(new ...

Troubleshooting Angular testing using Jest: Problem encountered with triggerEventHandler

I'm currently working on an Angular application and testing it with JEST. Everything seems to be running smoothly until I encounter an issue with event interactions in Angular. The test below works as expected: it('should delegate click to comp ...