Resolver scaffolding layouts
There are many ways/opinions on how to set up and lay out the structure of your resolvers. The goal for the resolver scaffolding feature of graphqlgen is to allow choosing between different "layouts". This is how the graphqlgen.yml config would look like:
resolver-scaffolding:
output: ./src/tmp-resolvers/
layout: single-file
For the first implementation I suggest the following layouts:
single-filesingle-file-classesfile-per-typefile-per-type-classes
Example single-file
./src/tmp-resolvers/resolvers.ts
import {
Resolvers,
PostResolvers,
UserResolvers
} from './generated/graphqlgen';
export const resolvers: Resolvers = {
Query: {
feed: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
drafts: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
post: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
},
Mutation: {
createDraft: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
deletePost: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
publish: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
},
Post: {
...PostResolvers.defaultResolvers,
author: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
},
User: {
...UserResolvers.defaultResolvers,
posts: (parent, args, ctx) => {
throw new Error('Resolver not implemented')
},
}
};
Example file-per-type
./src/tmp-resolvers/index.ts
import { Resolvers } from '../generated/graphqlgen'
import { Query } from './Query'
import { Mutation } from './Mutation'
import { Post } from './Post'
import { User } from './User'
export const resolvers: Resolvers = {
Query,
Mutation,
Post,
User,
}
src/tmp-resolvers/Query.ts
import { QueryResolvers } from './src/generated/graphqlgen.ts'
export const Query: QueryResolvers.Type = {
...QueryResolvers.scalars,
feed: parent => {
throw new Error('Resolver not implemented')
},
drafts: parent => {
throw new Error('Resolver not implemented')
},
post: (parent, args) => {
throw new Error('Resolver not implemented')
},
}
Same for src/tmp-resolvers/Mutation.ts, src/tmp-resolvers/Post.ts, src/tmp-resolvers/User.ts
@schickling For now it's always file-per-type right ? In the README.md there is a snippet of graphqlgen.yml with single-file and it seems to fallback to file-per-type. Is it attentional ?
Furthermore, what a big refactor 😄, I was discussing on an issue like one week ago, and it feels now a whole new thing 😄. So I've also a question related to theses resolvers, what is the defaultResolvers which is an empty object for now in generated/graphqlgen.ts and used in generated resolvers ?
EDIT : I understood how it was working when I saw a not empty object like :

But I've a TypeScript error around one of my model :
Resulted in
The error :

=> The token wasn't generated. My AuthPayload is the same used in https://github.com/prisma/prisma-examples/tree/master/typescript-graphql-auth
Btw, can't wait to have these prisma-examples updated with the new graphqlgen 😍
@schickling I've also seen an issue around this scaffolding :
resolver-scaffolding:
output: ./src/tmp-resolvers/
layout: single-file
outputs :
// ~/src/tmp-resolvers/index.ts
import { Resolvers } from "./src/generated/graphqlgen.ts";
...
It should be
// ~/src/tmp-resolvers/index.ts
import { Resolvers } from "../generated/graphqlgen";
...
cause tmp-resolvers is in the src folder as specified in the configuration file (otherwise it points to ~/src/tmp-resolvers/src/generated/graphqlgen).
As your example looks like having the good paths, but the generation not, I don't think the generation of the good paths was done or is not working as expected ?
I also removed the .ts which was considered as an error in my VSCode (maybe it's about my typescript/tslint config)
Can confirm that it seems to pick the file-per-type layout even when single-file is specified
And that the generated import is wrong :-)
The import pathologie issue is mentionned here : #117
And my token property missing might be the issue mentionned in #119 and should be fixed by #140. I'm kind of confused as PR's are merged but not mentioning issues as fixed to close it.