New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Scalars? #153
Comments
@garrettm What do you think? |
Correct me if I'm wrong, here's what I'm imagining, given what you said:
import * as Extern from './extern' export type ScalarName = Extern.ScalarName The user can export scalar types from this |
@garrettm yeah it makes sense. |
@dotansimha sounds good. If you're interested in letting someone else do the work, point me in the direction when ready and I can take a look. |
I am curious if there is any progress on this? Would love to be able specify types for my scalars :) |
@dotansimha facing same issue here. In my case I have a simple scalar |
I see, @pleerock . I am thinking about the right solution for this. We can easily pass custom configuration, so maybe a map like that:
And if you want to use a custom TypeScript type you wrote, for example:
And it will also add the import on top of the file. What do you think? |
It will definitely resolve most of issues. |
I like the typescript solution. I actually came across a a use-case with scalars today that it would solve. My other idea was to patch the generated code to add an import and replace the generated type alias with a regex, but this solution is of course much nicer :-). One note about the import. Lets say you want to do a named import, how would that work? Perhaps something like this could work:
|
@jonaskello yeah you are right, your examples supports more use cases (such as named import, default import or any other way). |
For query/mutation code generation, I use apollo-cli, which has the flag // with --customScalarsPrefix=GQL
interface SomeType {
date: GQLDate;
}
declare type GQLDate = Date; Think this is a nice solution for people who either already have their types globally available, and/or want to avoid name collisions with similarly named types
|
@dotansimha Since new #444 was released, there seems to be an issue with my JSON scalar type. schema scalar JSON
type foo {
bar: JSON!
} generated types export type JSON = any; // as before
export interface foo {
bar: Json; // not as before (previously, i would see JSON here)
} I'm aware of the new export type JSON = MyFancyType; // ok
export interface foo {
bar: Json; // Json != JSON
} |
I published a canary version: Could you check if it works for you?
|
@kamilkisiela with export type Json = any;
export interface foo {
bar: Json;
} This works but why can't it be scalar JSON |
Is this still waiting for release? |
@elie222 You can use ScalarsMap now. |
I'm using typescript, typescript-apollo-angular plugins to generate the client Does anyone know if adding the following:
will mean that any field returned from the fetch of DateTime type will be an instance of the JS Date class? |
@teebszet yes, but note that you can't really send JS |
@dotansimha yeah that makes sense. I guess it would be up to the typescript-apollo-angular plugin to initialise those DateTime fields as Date objects in the client. But I'm not sure if that is a feature that exists or not |
@teebszet I think it should be done in Apollo client, during the runtime, and then you can reflect the status of data by using custom scalars in the codegen. |
@dotansimha Do you have some useful resources on how it can be done universally with Apollo Link? I've found this pretty old (but still active) feature request for Apollo client and it almost seems like it's still not possible. It would be really lovely to get ISO date automatically parsed and converted and along with correct typings, it would be a breeze. This has especially become an issue with date-fns v2 which no longer supports passing a string. Only a |
Can anyone update on this? I am still not able to convert custom scalars. This is my
|
@finaxar-arthur What do you mean by converting custom scalars? If you have any issues, could you please create a new issue with a minimal reproduction? |
@finaxar-arthur the config must be at the top, like overwrite: true
schema: 'schema.graphql'
config:
scalars:
Long: number
BigDecimal: number |
Is there an example on how to configure custom scalars mapping them to custom types? How do you specify where should the custom types be imported from? |
You can use mappers format for scalars, we use it in other places as well, should look like that:
|
@dotansimha I'm new to this plugin, could you share what |
@nandorojo it could be just a simple TypeScript file that exports a type which name you would use behind For example: // file: my-custom-type.ts
export type MyCustomType = {
id: string
} then in codegen.yml you could do: generates:
types.ts:
config:
scalars:
Date: "path/to/file/my-custom-type#MyCustomType" |
Noticing that this doesn't work for the |
@tjmgregory that's actually true. I ended up declaring the type globally. declare global {
declare type JsonObject = Prisma.JsonObject
} |
I know resurrecting old issues is frowned upon but I stumbled on this issue needing to do exactly this today and I have to say it's an extremely powerful feature for a certain class of problems, so thank you so much for implementing it. |
@tjmgregory I was able to fix the issue by adding the path to scalar types to |
In Typescript, custom scalar types are emitted like:
How can I make the emitted Foo refer to my existing types?
For example, if somewhere else in my code base, I have:
I know this supports templating, but even with templating, I'm not sure how to make Foo in the generated types refer to my project's Foo. It seems like I'd need to import existing scalar definitions into my schema template? Or I'd need to define all my custom scalars in templates? Both of these sound pretty unmaintainable. Is there some convenient way to do this? Or is there an example for solving this sort of problem?
PS. Thank you for this package!
The text was updated successfully, but these errors were encountered: