graphqlintellisensejavascript-intellisenseprismaprisma-graphql

Q: Intellisense when using context.prisma


I'm creating my graphql layer with prisma. I have a question about using prisma with typescript in the resolvers.

In the documentation it is suggested to import prisma to get intellisense:

import { prisma } from '../generated/prisma-client'

If you do so, when you are writing a resolver like this one, you won't get any suggestions.

const user = (parent, args, context, info) => context.prisma.bodyweight({id: parent.id}).user()

To get the suggestions you would have to write it without referencing the context adding the reference later, which predisposes you to forget it and create bugs.

Is there a way to fix it (maybe in the tsconfig)?


Solution

  • You can get intellisense by typing the context:

    import { Prisma } from '../generated/prisma-client';
    export interface Context{
      prisma: Prisma;
    }
    
    const user = (parent, args, context: Context, info) => context.prisma.bodyweight({id: parent.id}).user()
    

    You should also checkout graphqlgen. That will allow you to automatically type the resolvers. https://github.com/prisma/graphqlgen#graphqlgen