Closed feychenie closed 1 year ago
Hi @feychenie! This is currently partially supported but I like your suggestions π
By chance, I've recently started working on https://github.com/eddeee888/graphql-code-generator-plugins/pull/117 which makes the integration with graphql-scalars
a bit more flexible.
Once it's done, we can use it for your first suggestion.
We can opt-out by setting scalars config to false. Note that scalar resolver files are still created in corresponding schema folder, so it is possible to import/implement your own
// codegen.ts
//...
generates: {
"src/schema": defineConfig({ scalarsModule: false }) // default is `scalarsModule="graphql-scalars"`
}
I'll follow up with more granular cases soon
For your second suggestion ( override certain scalar but keep the rest from graphql-scalars
), you should be able to do it now with this (slightly verbose) config:
Let's say you have these scalars (which also exist in graphql-scalars
):
# schema.graphql
scalar DateTime
scalar Currency # Say we want to override this
Then , we can use externalResolvers
to manually choose a custon resolver implementation.
Optionally, we can use typesPluginsConfig.scalars
to override graphql-scalars
's codegen type.
// codegen.ts
// ...
generates: {
'src/schema':
defineConfig({
externalResolvers: {
Currency: 'your-module/CustomCurrencyResolver#default as CustomCurrencyResolver', // set this to use own implementation
},
typesPluginsConfig: {
scalars: {
Currency: 'string', // set this to override graphql-scalars' default codegen type
},
},
}),
},
There will be one option to override Scalar in the future, rather than having to do it in two places :)
Hi @feychenie ,
Better Scalars options will be available in v0.5: https://github.com/eddeee888/graphql-code-generator-plugins/pull/130
You can have a preview by installing the alpha version:
yarn add -DE @eddeee888/gcg-typescript-resolver-files@pr130-run207-1
Here's a codebase with the alpha version wired up: https://github.com/eddeee888/poc-gcg-server-preset-0-5-0/pull/1
Here's a quick explanation of the options:
scalarsModules
: can be set to false
to opt-out of using graphql-scalars
. Note that Scalars resolvers are generated in their schema module so.scalarsOverrides
: Better implementation of externalResolvers
+ typesPluginsConfig.scalars
.In your case, since you are trying to override the Currency scalar, the config would look something like this:
import type { CodegenConfig } from "@graphql-codegen/cli";
import { defineConfig } from "@eddeee888/gcg-typescript-resolver-files";
const config: CodegenConfig = {
schema: "**/schema.graphql",
generates: {
"src/schema": defineConfig({
scalarsOverrides: {
Currency: {
resolver: "path/to/custom/scalars/Currency#Currency",
type: "string", // Use this if you want to customise the Currency Scalar's type
},
},
}),
},
};
export default config;
Thanks @eddeee888! I just implemented a couple of scalars with the "current" method above, and it works well and solves my problem for now ππ» π I will play around a bit to see if I can find a way to make this nicer.
Looking forward to testing 0.5!
Note: maybe this is already supported (or there is a reasonable workaround) and I did not find how to do it, then it would be amazing and just a docs improvement.
Is your feature request related to a problem? Please describe. I would like to be able to use my own scalar definition for a scalar that already exists in
graphql-scalars
. As an example,Currency
provided by the library allows all existing currencies, but I would like to build my own version that would represent only the subset of currencies supported by my platform.Describe the solution you'd like Possible solutions for opting out, in my perceived order of complexity (any of these would work for me):
graphql-scalars
and use the codegen standardscalars
config for the TS type.Describe alternatives you've considered I considered using a different name like
CurrencyCode
, but then had the same requirement for other scalars, and trying to find more and more alternative names eventually ended up with ridiculous scalar names. Good naming is too important.