Apollo-tooling: Typescript generation error "Cannot read property 'getType' of undefined"

Created on 6 Sep 2018  Â·  16Comments  Â·  Source: apollographql/apollo-tooling

Getting a confusing error when generating typescript files, as it is a bit in the bowels I'm not sure if I'm messing something up or if I'm using bad options. I can provide the schema if interested.

apollo codegen:generate --addTypename --target typescript graphql

√ Loading Apollo config
√ Scanning for GraphQL queries (458 found)
× Generating query files with 'typescript' target
→ Cannot read property 'getType' of undefined
TypeError: Cannot read property 'getType' of undefined
at Object.NamedType (C:/Users//AppData/Roaming/npm/node_modules/apollo/node_modules/graphql/validation/rules/KnownTypeNames.js:64:25)

🤖 codegen

Most helpful comment

I read @philfi comments. My schema was at the root directory of the project. I moved it to live in the save directory as my AppDelegate.swift file and the error went away. If the script is already running through the project looking through .graphql files it would be nice if it could also scan for the "schema.json" file. I realize there could be more than one but then but it could at least flag an error about multiple "schema.json" files and leave it up to the user to look for the duplicates in an Xcode project (which isn't a hard thing to do). I also want to add that if all goes well you'll find the generated API.swift file in the same directory which you will need to manually add to your Xcode project.

All 16 comments

I've encountered an issue like this before. After banging my head against it for awhile I realized that I wasn't providing the correct path to the schema and fixing that resolved the issue. Maybe that's the issue here too?

I know this error is sometimes thrown when the module is unable to call the remote API. Does the schema generation command work?

Also ran into this. Any fix for this?

I had this issue when it couldn't access the endpoint defined in the "apollo" section of my package.json

I read @philfi comments. My schema was at the root directory of the project. I moved it to live in the save directory as my AppDelegate.swift file and the error went away. If the script is already running through the project looking through .graphql files it would be nice if it could also scan for the "schema.json" file. I realize there could be more than one but then but it could at least flag an error about multiple "schema.json" files and leave it up to the user to look for the duplicates in an Xcode project (which isn't a hard thing to do). I also want to add that if all goes well you'll find the generated API.swift file in the same directory which you will need to manually add to your Xcode project.

You will encounter this issue if your apollo server is not running.

Why would apollo codegen:generate need a running server? Doesn't it just parse the schema and generate code from it? That's what the documentation says. (The official documentation is admittedly not up to date with the new CLI and codegen:generate; it uses apollo-codegen.)

maybe it's just how the team I recently joined is using it, but unless I have it running it fails.

Running server does not change results. It is litterally in the same powershrll script that I run these commands; so the server is up.

Same thing. I have a local schema and no server at all (only using apollo-link-rest) and I'm getting this issue no matter how I run it.

Same here. Using a local schema.graphql file, no apollo server, I always get this error

Thanks @xdeleon

+1

Would anyone experiencing this issue on the latest version please paste their config and error here? This is a stale issue, so I'm going to close it in a week unless anyone's still seeing it. Thanks!

This is no longer a problem for me after patching last week (5/10/2019) (. I had been hesitant to keep up on patching as it took a while for me to get my limp along solution. But I have more time now and it was fairly painless and works great now. This went along with updating ~50 other packages and following a different tutorial so unfortunately, I don't have a great way to document what I changed.

The new tutorial ran everything trough yarn, and I did not have that installed before. It worked like a charm. If you'd like the diff anyway send me a message and I'll give it to you.

@BasHamer thanks for getting back to me, glad to hear it's all resolved! I'll wait for any other responses then close this out. No need to send the diff 😄

Was this page helpful?
0 / 5 - 0 ratings