Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create a package for recommended GraphQL Scalars client types #1996

Open
eddeee888 opened this issue Jun 25, 2023 · 1 comment
Open

Create a package for recommended GraphQL Scalars client types #1996

eddeee888 opened this issue Jun 25, 2023 · 1 comment

Comments

@eddeee888
Copy link

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

Currently, it's hard to choose the right type for clients of servers that use graphql-scalars resolvers.

Here's the general process:

  • Check the return type of a serialize function in an arbitrary scalar
  • Set the type in clients (or tools like GraphQL Codegen)

Describe the solution you'd like

A separate package (published from this repo) that has the expected type of scalars that clients would receive. Example name: @graphql-scalars/client-types

Then, tools like GraphQL Codegen can simply do something like this:

const config = {
  plugins: ['typescript'],
  config: {
    scalars: {
      DateTime: '~@graphql-scalars/client-types#DateTime'
    }
  },
}
@ldrick
Copy link

ldrick commented Aug 2, 2023

Hi, I currently work around by collecting all the Return-Types like this:

import { type CodegenConfig } from "@graphql-codegen/cli";
import { GraphQLScalarTypeExtensions } from "graphql";
import { resolvers } from "graphql-scalars";

const scalars = Object.entries(resolvers).reduce<
    Record<string, GraphQLScalarTypeExtensions["codegenScalarType"]>
>((acc, [key, value]) => {
    const extensions = value.toConfig().extensions;
    if ("codegenScalarType" in extensions === false) {
        return acc;
    }
    acc[key] = extensions.codegenScalarType;
    return acc;
}, {});

const config: CodegenConfig = {
    generates: {
        "./gql/": {
            overwrite: true,
            documents: ["./**/*.ts?(x)"],
            schema: "https://myServer.com/graphql",
            preset: "client",
            config: {
                scalars: { ...scalars, Upload: "Blob" },
                strictScalars: true,
            },
        },
    },
    ignoreNoDocuments: true,
    hooks: {
        afterAllFileWrite: ["prettier --write"],
    },
};

export default config;

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

No branches or pull requests

2 participants