0

I wondering if it possible to get a type of single resolver function generated by graphql-codegen?

I also use graphql-modules with graphql-modules-preset. All of these libraries provide me a list of automatically generated types, but unfortunately I can't find a type of a single resolver function.

I would expected something like this:

const createRepository: CreateRepositoryMutationResolver = (parent, args, context, info) => {
  // Code goes here
}

where all arguments of the function (parent, args, context and info) are strongly typed.

Instead, I could only find this way of providing types

const createRepository: ResolverFn<{}, MutationCreateRepositoryInput, GraphQLContext, GraphQLInfo> = (parent, args, context, info) => {
  // Code goes here
}

I would like to skip this step where I need to fill in generics of ResolverFn

Any ideas?

P.S. If I declare all resolvers in single file, then types work as expected.

const resolvers: RepositoryModule.Resolvers = {
  Mutations: {
    createRepository: (parent, args, context, info) => {
       // all types of all arguments work as expected
    },
    removeRepository: (parent, args, context, info) => {
       // all types of all arguments work as expected
    }
  }
}

But I want to move each resolver into separate file

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68

1 Answers1

0

I did a little research on the types that were generated by graphql-codegen and found that all properties of Resolvers interface as well as all of its children are optional. It means that I can re-use this interface, but specify only resolvers that I need in the separate files

First file

// create-repository.resolver.ts

import { RepositoryModule } from '../../generated-types/module-types'

const resolvers: RepositoryModule.Resolvers = {
  Mutation: {
    createRepository: async (parent, args, context, info) => { 
      // code goes here
    }
  }
}

export default resolvers

second file

// create-repository.resolver.ts

import { RepositoryModule } from '../../generated-types/module-types'

const resolvers: RepositoryModule.Resolvers = {
  Mutation: {
    removeRepository: async (parent, args, context, info) => { 
      // code goes here
    }
  }
}

export default resolvers

and after, in a module, I can join all resolvers by loadFileSync

import { loadFilesSync } from '@graphql-tools/load-files'
import { createModule } from 'graphql-modules'
import { join } from 'path'

const resolvers = loadFilesSync(join(__dirname, './**/*.resolver.ts'), { useRequire: true })


export const repositoryModule = createModule({
  id: 'repositoryModule-module',
  dirname: __dirname,
  resolvers: resolvers,
  ...
})

I'm not sure if this is the correct way to use this interface, but it works for me

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68