graphql-mesh icon indicating copy to clipboard operation
graphql-mesh copied to clipboard

strictScalars and scalars map for graphql source codegen

Open leemhenson opened this issue 3 years ago • 4 comments

Is your feature request related to a problem? Please describe.

I am stitching together several graphql sources. They define custom scalar types, and I would like to be able to tell the codegen process what the equivalent typescript type should be for those scalars via the scalars map as documented here.

I would then additionally like to be able to enable the strictScalars flag so that the build process fails if I haven't provided a Typescript mapping for a scalar.

Describe the solution you'd like

I guess this would make sense in the source config:

sources:
  - name: MyGraphQLApi
    handler:
      graphql:
        endpoint: http://my-service-url:3000/graphql
        operationHeaders:
          Authorization: Bearer {env.MY_API_TOKEN}
        scalars:
          ScalarTypeA: string
          ScalarTypeB: number
        strictScalars: true

Describe alternatives you've considered

Leaving it as it is, which applies the type any to any unknown scalar. This isn't great as it will lead to bugs.

leemhenson avatar Jan 20 '22 16:01 leemhenson

Perhaps it make make things a little more future-proof if the graphql source config block just forwarded a codegen config block onto graphql-codegen directly, e.g.:

sources:
  - name: MyGraphQLApi
    handler:
      graphql:
        endpoint: http://my-service-url:3000/graphql
        operationHeaders:
          Authorization: Bearer {env.MY_API_TOKEN}
        codegen:
          scalars:
            ScalarTypeA: string
            ScalarTypeB: number
          strictScalars: true

leemhenson avatar Jan 20 '22 19:01 leemhenson

You can add custom codegen config like below;

codegen:
  scalars:
    ...

ardatan avatar Mar 13 '22 18:03 ardatan

Hi. That works for the types emitted for the meshed schema, but it doesn't emit config for the source schemas.

e.g. say in my source schema there is scalar DateTime, if i add:

codegen:
    scalars:
        DateTime: string

then in the emitted .mesh/index.ts I get:

// at the top of the file

export type Scalars = {
    ID: string;
    String: string;
    Boolean: boolean;
    Int: number;
    Float: number;
    DateTime: string;  // <-- looks good
}

// lower down in the file

namespace MySourceTypes {
  export type Scalars = {
    ID: string;
    String: string;
    Boolean: boolean;
    Int: number;
    Float: number;
    DateTime: any;  // <-- uh oh
  }
}

If I try to add a codegen yaml block inside the graphql source block then I get errors about excess properties not being allowed.

leemhenson avatar Mar 16 '22 17:03 leemhenson

For now, it is not possible to configure InContext SDK type generation.

ardatan avatar Mar 16 '22 17:03 ardatan