Generating Typescript Types with Apollo Codegen - Part 9

preview_player
Показать описание
Learn how to generate Typescript types for your GraphQL mutations and queries using Apollo Codgen.

Links from video:

----

----

----
Follow Me Online Here:

#benawad

----
#benawad
Рекомендации по теме
Комментарии
Автор

They've updated apollo-codegen. I think your global version is out of date (your local dev copy is new??). The --output parameter is required and ts-modern is no longer. If you notice in the error message it says: Choices: "swift", "scala", "json", "ts-legacy", "ts", "typescript-legacy", "typescript", "flow-leagcy", "flow". I ended up doing

apollo-codegen generate src/**/*.tsx --schema schema.json --target typescript --output src/operation-result-types.ts

billb
Автор

So I encountered a type error that prevented me from rebuilding the controller package which goes like this:
"Argument of type 'typeof C' is not assignable to parameter of type 'ComponentType<Props & Partial<DataProps<RegisterMutation, RegisterMutationVariables>> &...."

I figured out that the graphql HOC now expects a 4th parameter which I didn't know what I should add in. So I just temporarily added "any".

What should be the right type for this? Here's what I temporarily did to get rid of the warning to be able to proceed:

export const RegisterController = graphql<
Props,
RegisterMutation,
RegisterMutationVariables,
any This part. It's expecting a TChildProps
>(registerMutation)(C);

ononaoyaa
Автор

The type-gen probably doesn't work in the npm scripts it tries to run a local (older) version of the apollo-codegen (it's probably a dependency of one of your dependencies. You can verify this with 'yarn why apollo-codegen'). At any rate, it's better to have all the stuff you need as dev dependencies if possible, instead of relying on global installations. So install apollo-codegen as a devDependency (yarn add -D apollo-codegen) and it should work.

bengr
Автор

Is it because you used 'ts-modern' rather than 'typescript' when you included "--output The example uses typescript as the --target.

philbland
Автор

Apollo Codegen still works OK but it's quite outdated. So you can update with new Apollo Tooling CLI v2.7.0 like this

"generate": "apollo codegen:generate --localSchemaFile schema.json --target typescript __generated__",

nightingale
Автор

In 2020, I change these two lines and it's work for me:

amanlearnscode
Автор

Hi Ben. I’ve had quite a few problems with codegen and thought I would ask your advice. If I run it from node script I get the error “cannot find @babel/generates” and when I run it in the bash script it seems to work fine. Also I’m not able to run it without the —output tag and so it always wants an output file and won’t put it in the __generate__ folder. I wonder if you had any advice on what I’m doing wrong?

DrPanesar
Автор

Have you tried to use enums with typeOrm. I can’t match types between TS enum and graphQL enums. Nobody have an idea of how I can do that?

soulsauveur
Автор

Hi! I'm using this series as a basis for a project i'm working on right now. When i generate the schema.d.ts file with your script i allways get a typescript error for the generated code on my mutations:

Type 'string | null | undefined' is not assignable to type 'string | undefined'

Do you now how to avoid this behavior?

ware
Автор

apollo-codegen generate src/**/*.tsx --schema schema.json --target typescript --output src/operation-result-types.ts

This line works fine, it's probably the target, because when I changed it to typescript instead of ts-modern it worked, odd that --output is unpredictable.

devilmanscott