graphqlgen icon indicating copy to clipboard operation
graphqlgen copied to clipboard

Missing custom types properties in suggested model types.

Open otrebu opened this issue 5 years ago • 7 comments

My schema.graphql contains:

type Post {
    id: ID!
    title: String!
    content: String!
    comments: [Comment!]
    user: User!
}

When I generate the types the suggested type for Post is:

export interface Post {
    id: string;
    title: string;
    content: string;
    isAvailable: boolean;
}

I am not sure why comments and user are missing?

Steps to reproduce

Expected results

Actual results

Versions

  • graphqlgen:
  • OS name and version:

otrebu avatar Feb 11 '19 15:02 otrebu

Hey @otrebu, these model types represent what your server will fetch from the backend(s) (DB, microservice, etc). A model type is something that a resolver implementation can return. The missing types you pointed out are relations. What would you ideally expect graphqlgen to generate for you?

jasonkuhrt avatar Feb 11 '19 22:02 jasonkuhrt

Hi @jasonkuhrt yes they are relations, but is it not normal to want to return them?

Otherwise the strongly typing becomes a pain, as you return they prisma type which contains the relations as promises, which will be resolved anyway. (This is where I experienced that weirdness that I shared in slack).

So personally I would expect them to be part of the types returned by graphqlgen.

otrebu avatar Feb 12 '19 09:02 otrebu

What if a query field resolver fetches it’s scaler data from one backend data source but fetches the relations from another source. A model maps to/specifies what the return value of some resolver will be. Resolvers should do as little work as possible. If client doesn’t include comments in selection set then server should not wastefully fetch them—to encode this in model types means keeping type relations at best shallow (list of comment IDs, author I’d, etc.

I need to make a diagram for this.

jasonkuhrt avatar Feb 12 '19 13:02 jasonkuhrt

we might want to merge this thread into https://github.com/prisma/graphqlgen/issues/181

jasonkuhrt avatar Feb 12 '19 13:02 jasonkuhrt

@jasonkuhrt is it not what the resolvers on a type, for a specific field are for?

otrebu avatar Feb 12 '19 15:02 otrebu

is it not what the resolvers on a type, for a specific field are for?

If you can, take a look at the generated resolver types:

  • root field resolvers are (generally) expected to return models (but can, of course, return scalers if the schema is setup that way)
  • non-root field resolvers receive models as parent
  • field resolvers, that are schema relations, are expected to return models

jasonkuhrt avatar Feb 12 '19 15:02 jasonkuhrt

Hi @jasonkuhrt, I think I must have been taking a wrong approach and got confused when using it with prisma. You probably don't need them...

otrebu avatar Feb 13 '19 15:02 otrebu