relay-tools / relay-compiler-language-typescript

⛔️ Obsolete - A language plugin for Relay that adds TypeScript support, including emitting type definitions.
MIT License
241 stars 70 forks source link

question: how to generate globalTypes (like apollo) #175

Open smmoosavi opened 4 years ago

smmoosavi commented 4 years ago

we can generate global types (like enums) with apollo tooling. you can see --globalTypesFile option.

can we do the same thing with relay compiler and generate global types file.

tnx

renanmav commented 4 years ago

I’m not sure. Maybe @alloy can answer you.

sibelius commented 4 years ago

can you provide some example of this?

is this like enums https://github.com/relay-tools/relay-compiler-language-typescript/issues/164?

or custom scalar https://github.com/relay-tools/relay-compiler-language-typescript/issues/129?

smmoosavi commented 4 years ago

or custom scalar #129?

custom scalar is ok.

is this like enums #164?

this enum type is generated for Label_label fragment. but I need some global file that contains all graphql enum types

for example, apollo tooling is generating a file that contained all enum types.

schema:

enum Color {
  BLUE
  GREEN
  RED
}
enum Direction { 
  Right
  Left
}
apollo client:codegen --globalTypesFile=src/globalTypes.ts

the generated file is independent of any query or fragment in the code and only depends on the schema file.

generated filesrc/globalTypes.ts:

export enum GenerationType {
  BLUE = "BLUE",
  GREEN = "GREEN",
  RED = "RED",
}
export enum Direction {/*..*/}
// and all other enums defined in the schema file

for the relay, it would be like src/globalTypes.ts:

export type Color = "BLUE" | "GREEN" | "RED" | "%future added value";
export type Direction = "..."
smmoosavi commented 4 years ago

for example, the ColorSelector component doesn't have any fragment or query. but it needs Color enum. and it shouldn't depend on any of the other usages that have the enum definitions.

sibelius commented 4 years ago

you want something like this

https://github.com/facebook/relay/blob/master/packages/relay-compiler/core/RelayGraphQLEnumsGenerator.js

RelayGraphQLEnumsGenerator is only for Flow right now

can you try implement this to Typescript?

smmoosavi commented 4 years ago

you want something like this

yes. it is what I want (I assume schema argument is all defined types in graphql)

can you try to implement this to Typescript?

yes, I can. flow and typescript output is almost equal in this case. except for the header part.

but I don't know at which file it should be written. or where and how this function should be used. (integrated with relay-compiler-language-typescript and relay-compiler)

c0m1t commented 4 years ago

https://github.com/facebook/relay/issues/3051

c0m1t commented 4 years ago

Me and @smmoosavi wrote a package for this purpose: relay-enum-generator

sibelius commented 4 years ago

Great to hear, can you add this to relay documentation?

c0m1t commented 4 years ago

@sibelius Where should I add this to?

sibelius commented 4 years ago

Check Facebook/relay repo, there is a docusaurus there, this could be close to type emission guide

DamodarSojka commented 4 years ago

How about just type aliases? for example: export type UUID = string; The type is of course not imported by all the generated files making them fail the compilation.