Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

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

Open
smmoosavi opened this issue Mar 4, 2020 · 12 comments
Open

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

smmoosavi opened this issue Mar 4, 2020 · 12 comments
Labels
enhancement New feature or request

Comments

@smmoosavi
Copy link

smmoosavi commented Mar 4, 2020

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
Copy link
Contributor

renanmav commented Mar 4, 2020

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

@sibelius
Copy link
Contributor

sibelius commented Mar 4, 2020

can you provide some example of this?

is this like enums #164?

or custom scalar #129?

@smmoosavi
Copy link
Author

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
Copy link
Author

smmoosavi commented Mar 4, 2020

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 sibelius added the enhancement New feature or request label Mar 4, 2020
@sibelius
Copy link
Contributor

sibelius commented Mar 4, 2020

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
Copy link
Author

smmoosavi commented Mar 4, 2020

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
Copy link

c0m1t commented Mar 20, 2020

facebook/relay#3051

@c0m1t
Copy link

c0m1t commented Mar 22, 2020

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

@sibelius
Copy link
Contributor

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

@c0m1t
Copy link

c0m1t commented Mar 22, 2020

@sibelius Where should I add this to?

@sibelius
Copy link
Contributor

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

@DamodarSojka
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants