Relay: [Modern] Error loading schema, Error detail: Cannot read property '__schema' of undefined

Created on 18 Oct 2017  Â·  6Comments  Â·  Source: facebook/relay

Trying to create a basic application with React Modern. Have a GraphQL server running Graphene. Generated this schema.json - https://gist.github.com/llevar/86a308f5836af1f69b8606aaa47e6d94
via schema.introspect() and json.dump().

Running yarn run relay results in:

ERROR:
Error loading schema. Expected the schema to be a .graphql or a .json
file, describing your GraphQL server's API. Error detail:

TypeError: Cannot read property '__schema' of undefined
    at buildClientSchema (/Users/siakhnin/Documents/workspace/react-test2/node_modules/relay-compiler/node_modules/graphql/utilities/buildClientSchema.js:48:42)
    at _getSchema (/Users/siakhnin/Documents/workspace/react-test2/node_modules/relay-compiler/bin/relay-compiler:219:29)
    at Object.getSchema (/Users/siakhnin/Documents/workspace/react-test2/node_modules/relay-compiler/bin/relay-compiler:103:19)
    at CodegenRunner.<anonymous> (/Users/siakhnin/Documents/workspace/react-test2/node_modules/relay-compiler/bin/relay-compiler:5237:53)
    at next (native)
    at step (/Users/siakhnin/Documents/workspace/react-test2/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /Users/siakhnin/Documents/workspace/react-test2/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
    at Promise.F (/Users/siakhnin/Documents/workspace/react-test2/node_modules/core-js/library/modules/_export.js:35:28)
    at CodegenRunner.<anonymous> (/Users/siakhnin/Documents/workspace/react-test2/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12)
    at CodegenRunner.write (/Users/siakhnin/Documents/workspace/react-test2/node_modules/relay-compiler/bin/relay-compiler:5329:21)
error Command failed with exit code 100.

Most helpful comment

Yep, I have just solved this issue. The Cannot read property '__schema' of undefined error raised when relay-compiler tries to get __schema property from a variable which contains undefined value:

> const o = Object()
> const data = o.data
< undefined
> const schema = data['__schema']
< Uncaught TypeError: Cannot read property '__schema' of undefined

So obvious, isn't it? :)

Therefore, it's necessary to edit your schema.json as follows, before processing:

{
  "data": {
    "__schema": { ... }
  }
}

Then if you run the command yarn relay-compiler --src ./src --schema ./schema.json in the terminal, you will get something like this:

Watchman:  Watchman was not found in PATH.  See https://facebook.github.io/watchman/docs/install.html for installation instructions
HINT: pass --watch to keep watching for changes.
Parsed default in 0.47s

Writing default
Writer time: 1.55s [1.55s compiling, 0.00s generating, 0.00s extra]
Unchanged: 0 files
Written default in 1.77s
Done in 7.58s.

Hope this helps someone!

All 6 comments

It looks like it's just not able to load the schema file. I'd have an extra look at your relay command configuration and make sure that --schema points to the correct schema location. I think the path needs to be relative to your package.json file. yarn relay-compiler --src ./src --schema ./config/schema.json is an example, with your source files in src and the schema saved to a config directory.

I don't think it's a path issue. When I converted the same file to a .graphql format it is processed without issues on the same path (see output below). Both files have the same permissions, etc.

$ node ./setup --download-schema && relay-compiler --src ./src --schema ./schema.json
ERROR: Unknown option --no-pretty

Watchman:  watchman --no-pretty get-sockname returned with exit code=1, signal=null, stderr= ERROR: Unknown option --no-pretty

HINT: pass --watch to keep watching for changes.
Parsed default in 0.01s

Writing default
ERROR:
Error loading schema. Expected the schema to be a .graphql or a .json
file, describing your GraphQL server's API. Error detail:

TypeError: Cannot read property '__schema' of undefined
$ node ./setup --download-schema && relay-compiler --src ./src --schema ./schema.graphql
ERROR: Unknown option --no-pretty

Watchman:  watchman --no-pretty get-sockname returned with exit code=1, signal=null, stderr= ERROR: Unknown option --no-pretty

HINT: pass --watch to keep watching for changes.
Parsed default in 0.01s

Writing default
Writer time: 0.25s [0.23s compiling, 0.02s generating, 0.00s extra]
Unchanged: 5 files
Written default in 0.27s
✨  Done in 1.68s.

I'm getting his error as well.
I have generated both, and if I just replace .json with .graphql, it works.

Yep, I have just solved this issue. The Cannot read property '__schema' of undefined error raised when relay-compiler tries to get __schema property from a variable which contains undefined value:

> const o = Object()
> const data = o.data
< undefined
> const schema = data['__schema']
< Uncaught TypeError: Cannot read property '__schema' of undefined

So obvious, isn't it? :)

Therefore, it's necessary to edit your schema.json as follows, before processing:

{
  "data": {
    "__schema": { ... }
  }
}

Then if you run the command yarn relay-compiler --src ./src --schema ./schema.json in the terminal, you will get something like this:

Watchman:  Watchman was not found in PATH.  See https://facebook.github.io/watchman/docs/install.html for installation instructions
HINT: pass --watch to keep watching for changes.
Parsed default in 0.47s

Writing default
Writer time: 1.55s [1.55s compiling, 0.00s generating, 0.00s extra]
Unchanged: 0 files
Written default in 1.77s
Done in 7.58s.

Hope this helps someone!

I got the same but I doubt it will solve it coz I made a noob mistake
The solution was to 'cd' to the .json location

this is a problem when schema is a wrong path

Was this page helpful?
0 / 5 - 0 ratings