github dotansimha/graphql-code-generator @graphql-codegen/visitor-plugin-common@2.2.0

latest releases: release-1730117942954, @graphql-codegen/visitor-plugin-common@5.5.0, @graphql-codegen/typescript@4.1.1...
3 years ago

Minor Changes

  • d6c2d4c: Allow declaring Argument and InputType field mappings based on directive annotations.

    WARNING: Using this option does only change the type definitions.

    For actually ensuring that a type is correct at runtime you will have to use schema transforms (e.g. with @graphql-tools/utils mapSchema) that apply those rules! Otherwise, you might end up with a runtime type mismatch which could cause unnoticed bugs or runtime errors.

    Please use this configuration option with care!

    plugins:
      config:
        directiveArgumentAndInputFieldMappings:
          asNumber: number
    directive @asNumber on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
    
    input MyInput {
      id: ID! @asNumber
    }
    
    type User {
      id: ID!
    }
    
    type Query {
      user(id: ID! @asNumber): User
    }

    Usage e.g. with typescript-resolvers

    const Query: QueryResolvers = {
      user(_, args) {
        // args.id is of type 'number'
      },
    };
  • 5086791: Allow overwriting the resolver type signature based on directive usages.

    WARNING: Using this option does only change the generated type definitions.

    For actually ensuring that a type is correct at runtime you will have to use schema transforms (e.g. with @graphql-tools/utils mapSchema) that apply those rules! Otherwise, you might end up with a runtime type mismatch which could cause unnoticed bugs or runtime errors.

    Example configuration:

    config:
      # This was possible before
      customResolverFn: ../resolver-types.ts#UnauthenticatedResolver
      # This is new
      directiveResolverMappings:
        authenticated: ../resolvers-types.ts#AuthenticatedResolver

    Example mapping file (resolver-types.ts):

    export type UnauthenticatedContext = {
      user: null;
    };
    
    export type AuthenticatedContext = {
      user: { id: string };
    };
    
    export type UnauthenticatedResolver<TResult, TParent, _TContext, TArgs> = (
      parent: TParent,
      args: TArgs,
      context: UnauthenticatedContext,
      info: GraphQLResolveInfo
    ) => Promise<TResult> | TResult;
    
    export type AuthenticatedResolver<TResult, TParent, _TContext, TArgs> = (
      parent: TParent,
      args: TArgs,
      context: AuthenticatedContext,
      info: GraphQLResolveInfo
    ) => Promise<TResult> | TResult;

    Example Schema:

    directive @authenticated on FIELD_DEFINITION
    
    type Query {
      yee: String
      foo: String @authenticated
    }

Patch Changes

  • feeae1c: Do not throw an error when trying to merge inline fragment usages.

Don't miss a new graphql-code-generator release

NewReleases is sending notifications on new releases.