dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.86k stars 1.33k forks source link

[typescript-resolvers] Missings args types in QueryResolvers #5968

Open olso opened 3 years ago

olso commented 3 years ago

Describe the bug

Args (my case `limit`) are not generated on `QueryResolvers["landing"]` They are generated on `LandingResolvers` tho The query works in runtime of course **To Reproduce** Steps to reproduce the behavior:
  1. My GraphQL schema:
type City implements Node {
    id: ID!
    isFeatured: Boolean!
    name: String!
  }

type Query {
    landing: Landing
}

type Landing {
    # This is generated for LandingResolvers, but not for QueryResolvers["landing"]
    featuredCities(limit: Int!): [City!]!
  }
  1. My GraphQL operations:
query {
  landing {
    featuredCities(limit: 2) {
      code
    }
  }
}
  1. My codegen.yml config file:
schema: http://localhost:8081/graphql

config:
  noSchemaStitching: true
  optionalInfoArgument: true
  federation: false
  useTypeImports: true
  # useIndexSignature: true
  maybeValue: T | null | undefined
  contextType: ./ApolloContext#ApolloContext
  # also check web repo codegen.yml for scalars
  scalars:
    DateTime: string
    Date: string
    Time24H: string
    Money: string
  mappers:
    City: ./GeneratedDb#City as CityDb

generates:
  src/types/GeneratedGql.ts:
    plugins:
      - typescript
      - typescript-resolvers
  1. Generated types:
export type QueryResolvers<ContextType = ApolloContext, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = ResolversObject<{
  landing?: Resolver<Maybe<ResolversTypes['Landing']>, ParentType, ContextType>;
}>;

/** Mapping between all available schema types and the resolvers types */
export type ResolversTypes = ResolversObject<{
  Landing: ResolverTypeWrapper<Omit<Landing, 'featuredCities'> & { featuredCities: Array<ResolversTypes['City']> }>;
}>;

/**
 * Here the args are missing
 */
export type LandingResolvers<ContextType = ApolloContext, ParentType extends ResolversParentTypes['Landing'] = ResolversParentTypes['Landing']> = ResolversObject<{
  featuredCities?: Resolver<Array<ResolversTypes['City']>, ParentType, ContextType, RequireFields<LandingFeaturedCitiesArgs, 'limit'>>;
}>;
export const query: QueryResolvers["landing"] = () => ({
  /**
   * Here limit is any / untyped
   */
  featuredCities: ({ limit }) =>
    getCityBase().where({ isFeatured: true }).limit(makeLimit(limit)).orderBy("name", "asc"),

Expected behavior

Environment:

Additional context

Urigo commented 3 years ago

Hi @olso and thank you for the report!

Sorry but I'm not adding a lot here but just labeling it according to our new Contribution Guide and issue flow.

It seems like we are on stage 0. Now in order to advance to stage 1 we'll need an easily running reproduction, do you think you can create that on code sandbox?

Thank you and sorry that this comment is not a complete solution (yet).

dotansimha commented 3 years ago

Please provide a live reproduction to move forward. As far as I see, there is RequireFields<LandingFeaturedCitiesArgs, 'limit'>> used there, and if you'll use Resolvers type as root for your resolvers object, it will be fine.