apollo-tooling: Cannot convert undefined or null to object

Upgraded 2.0.5 from 1.9.2 which worked (apart from having to delete the apollo/node_moldules/grapqhl to get past the __typename error).

Updated the config to take into account the change and am downloading the schema with no issues however run into a problem when generating the query files:

 ✔ Saving schema to schema.json
$ apollo codegen:generate --target=flow --useFlowExactObjects --addTypename
 ✔ Loading Apollo Project
 ✖ Generating query files with 'flow' target
   → Cannot convert undefined or null to object
TypeError: Cannot convert undefined or null to object
    at Function.values (<anonymous>)
    at write (~/node_modules/apollo/lib/commands/client/codegen.js:55:55)
    at Task.task (~/node_modules/apollo/lib/commands/client/codegen.js:83:46)

Config:

module.exports = {
  client: {
    name: 'Dashboard'
  },
  service: {
    name: 'Dashboard',
    endpoint: {
      url: 'localhost/graphql'
    }
  },
  queries: [
    {
      schema: 'Dashboard',
      includes: ['**/*.graphql'],
      excludes: ['node_modules/**']
    }
  ]
};

Is this because I have missed something in the config conversion or is there something else I am missing (perhaps some breaking changes docs should be added to the change log)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 14
  • Comments: 29 (8 by maintainers)

Most upvoted comments

This is an example of usage of schema:download and client:codegen: https://github.com/apollographql/apollo-tooling/issues/671#issuecomment-437675051

For schema:download you do not need any configuration.

For an example of configuration that doesn’t use a local schema file you can have a configuration like:

module.exports = {
  client: {
    name: 'GitHubClient',
    service: {
      name: 'GitHub',
      url: 'https://api.github.com/graphql',
      headers: {
        'Authorization': 'bearer ***'
      }
    },
    includes: ['*.graphql']
  }
};

and execute:

apollo client:codegen --target=typescript

@trevor-scheer it would be helpful if someone would put together an example config for client and server and post it in documentation. It is not so easy for beginners to read through type definitions to reverse engineer their own valid config file. I am not a beginner and figuring out how to setup a config file has been challenging. I would implore someone with an intimate understanding of Apollo to spend a few minutes to sketch out some reasonable config files for the community to reference. It would be very much appreciated.

I have the same issue on 2.0.15. In addition, --localSchemaFile is ignored in the command line if apollo.config.js exists.

The below works when generating running: apollo client:codegen --queries="./src/**/*.ts?(x)" --target=typescript

module.exports = {
    client: {
        includes: ['src/shared/**/*.{ts,tsx,js,jsx,graphql}'],
        localSchemaFile: './schema.graphql'
    }
};

However, if i leave in the service section of the apollo.config.js:

module.exports = {
    client: {
        includes: ['src/shared/**/*.{ts,tsx,js,jsx,graphql}'],
        localSchemaFile: './schema.graphql'
    },
    service: {
        name: 'test',
        localSchemaFile: './schema.graphql'
    }
};

Then this shows:

  ✔ Loading Apollo Project
  ✖ Generating query files with 'typescript' target
    → Cannot convert undefined or null to object
TypeError: Cannot convert undefined or null to object
    at Function.values (<anonymous>)
    at write (/usr/local/lib/node_modules/apollo/lib/commands/client/codegen.js:55:55)
    at Task.task (/usr/local/lib/node_modules/apollo/lib/commands/client/codegen.js:83:46)

@yongnicholaskim That is because --header can be used multiple times to specify multiple headers from the CLI.

As for the other issues in this thread, I think most of them have been solved (at least the original issue). As a summary:

  • apollo client:codegen ONLY supports CLIENT projects, so if you’re trying to use this for codegen of server types, that’s completely unsupported right now.
  • The config file shape right now has a config.client.service key. If you’re creating a config with a top-level service key and trying to use codegen, you will run into issues (as the cli will treat that config as a service project).
  • If you’re using a localSchemaFile, don’t also use a name. The name is the field that the config looks at for an engine service ID and will try to load a schema from apollo engine instead of the local schema file.

I’m going to close this issue before it goes more off-topic. If there are any remaining issues, just open a new one, please 😃

This is kind of crippling for productivity. Any word on the correct resolution to this issue?

I switched over to https://graphql-code-generator.com. Not to take away from this tool, but it is much better for generating types, is customizable through plugins and the client side types are more of what you would expect.

I got the same error when provide ENGINE_API_KEY in environment. (v 2.12.5)

Now, it’s solved. Hope it will helps you too.

FYI: This is a good one for an example setup

https://github.com/abumalick/persisted-queries-showcase

I have little experience with graphql, so I’m not sure. I know there is a different configuration (that has service as root field instead of client), but I haven’t used that yet. It is probably worthy of a separate issue though.