Apollo-tooling: Custom scalars as a string instead of number

Created on 28 Jan 2018  路  14Comments  路  Source: apollographql/apollo-tooling

Hi,

I have some custom scalars in my NodeJS backend.

package.json
"graphql": "0.11.7",
"graphql-server-express": "1.3.2",
"graphql-tools": "2.14.1",

One of them is Tel which represents a phone number.

See an example:

// This is an output from apollo-codegen
communications:  {
      phones: Array< string | null > | null,
}

// But I would expect this with my custom scalar Tel
communications:  {
      phones: Array< number | null > | null,
}

Is it possible?

Most helpful comment

If you use the option --passthroughCustomScalars, apollo-cli will use the name of your custom scalar as its flow/typescript name, so in your case Tel.

You can then define a global type Tel in your app. If you want to avoid name conflicts, also use --customScalarsPrefix=customScalarsPrefix to define a prefix. Using the prefix GraphQL_ would write your type as GraphQL_Tel.

All 14 comments

There is a path forward where we generate global type identifiers that we expect consumers to implement (and put in the type search path of flow).

would that suffice?

wouldn't it be better if the generator stops at each scalar and make you provide a mapping as parameter?

something like --map PhoneNumberType:Array<number|null>|null

@lewisf , I didn't understand your answer. Sorry!

@yuricamara Have you found the solution?
I've faced with the same issue.

Not yet, @alex-zer0

Hey, we would like to use the codegen feature but that issue is the only thing that breaks our interfaces.
Any news about it?

What language target is missing this feature? I know it鈥檚 existed in Flow and TypeScript for a long time now...

Im sorry, i cant seem to find documentation on it, any refrences please:)?

If you use the option --passthroughCustomScalars, apollo-cli will use the name of your custom scalar as its flow/typescript name, so in your case Tel.

You can then define a global type Tel in your app. If you want to avoid name conflicts, also use --customScalarsPrefix=customScalarsPrefix to define a prefix. Using the prefix GraphQL_ would write your type as GraphQL_Tel.

If you use the option --passthroughCustomScalars, apollo-cli will use the name of your custom scalar as its flow/typescript name, so in your case Tel.

You can then define a global type Tel in your app. If you want to avoid name conflicts, also use --customScalarsPrefix=customScalarsPrefix to define a prefix. Using the prefix GraphQL_ would write your type as GraphQL_Tel.

How to make it work when that typename is from a module (must be imported)?

@asfernandes you should be able to create a .d.ts file that defines your scalars:

globalScalars.d.ts

import { Foobar } from 'foobar'

type GraphQL_Foobar = Foobar
type GraphQL_Date = string
type GraphQL_DateTime = string

with

--passthroughCustomScalars --customScalarsPrefix=GraphQL_

@asfernandes you should be able to create a .d.ts file that defines your scalars:

globalScalars.d.ts

import { Foobar } from 'foobar'

@LinusU when you use import, your decl file become modular and the generated files do not import it.

I had resolved this problem using an additional file, but it's ugly.

@LinusU I have tried another approach and seems to work. Should it be declared this way?

import { LocalDate, LocalDateTime } from 'js-joda';

declare global {
    type GraphQLLong = number;
    type GraphQLLocalDate = LocalDate;
    type GraphQLLocalDateTime = LocalDateTime;
}

when you use import, your decl file become modular and the generated files do not import it.

Ah, right... Hmm, I wonder why it does work for me though? 馃

Anyhow, the correct way is probably to use /// <reference types="..." />

Was this page helpful?
0 / 5 - 0 ratings