Relay: Assertion error with a query including inline fragments

Created on 12 Jun 2017  路  4Comments  路  Source: facebook/relay

Given ./foo.js:

import { graphql } from 'relay'

export const query = graphql`
  query fooQuery {
    foo {
      ... on Bar {
        bar
      }
      ... on Baz {
        baz
      }
    }
  }
`

and ./schema.graphql:

type Bar {
        bar: String!
}

type Baz {
        baz: String!
}

union Foo = Bar | Baz

type Query {
        foo: Foo!
}

Then, run the following command.

relay-compiler --src . --schema schema.graphql

And, I got errors.

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

Writing default
Error: Expected undefined to be a GraphQL leaf type.
    at invariant (/Users/me/.config/yarn/global/node_modules/graphql/jsutils/invariant.js:19:11)
    at assertLeafType (/Users/me/.config/yarn/global/node_modules/graphql/type/definition.js:93:27)
    at generateIDSelections (/Users/me/.config/yarn/global/node_modules/relay-compiler/bin/relay-compiler:6588:20)
    at transformField (/Users/me/.config/yarn/global/node_modules/relay-compiler/bin/relay-compiler:6531:23)
    at /Users/me/.config/yarn/global/node_modules/relay-compiler/bin/relay-compiler:6514:15
    at Array.map (native)
    at transformNode (/Users/me/.config/yarn/global/node_modules/relay-compiler/bin/relay-compiler:6512:37)
    at /Users/me/.config/yarn/global/node_modules/relay-compiler/bin/relay-compiler:6506:28
    at Array.reduce (native)
    at transform (/Users/me/.config/yarn/global/node_modules/relay-compiler/bin/relay-compiler:6505:21)

Is this a bug of the compiler or am I missing something?

Most helpful comment

I found a hacking solution for this of adding some type which contains a field of an ID type like the below.

type Hack {
        id: ID
}

...

The problem is that bin/relay-compiler calls context.schema.getType(ID_TYPE) internally.
However, GraphQLSchema class seems to omit unused types from its type map.
The author of relay-compiler may have assumed that every built-in type is available by GraphQLSchema.getType().

All 4 comments

I found a hacking solution for this of adding some type which contains a field of an ID type like the below.

type Hack {
        id: ID
}

...

The problem is that bin/relay-compiler calls context.schema.getType(ID_TYPE) internally.
However, GraphQLSchema class seems to omit unused types from its type map.
The author of relay-compiler may have assumed that every built-in type is available by GraphQLSchema.getType().

Thanks for the input here. We're currently going through old issues that appear to have gone stale (ie. not updated in about the last 3 months) because the volume of material in the issue tracker is becoming hard to manage. If this is still important to you please comment and we'll re-open this.

Thanks once again!

I am seeing this problem with the relay-compiler, although the query works in GraphiQL. We have a Union inside of a fragment that gives this same error (Expected undefined to be a GraphQL leaf type) however with no stack trace. Can this issue be reopened? @wincent

(This was tested with relay and compiler on v1.4.1)

export default createRefetchContainer(
  ScheduleDetails,
  graphql`
    fragment ScheduleDetailsAssignments on ScheduleAssignment {
      id
      target {
        ... on Rotation {
          id
          name
        }
      }
    }

  `,
  graphql`
    query ScheduleDetailsQuery($id: String!) {
      schedule(id: $id) {
        assignments {
          ...ScheduleDetailsAssignments
        }
      }
    }
  `
)

I'm experiencing the same issue:

Writing default
ERROR:
Expected undefined to be a GraphQL leaf type.
From: CodegenRunner.write
    at invariant (./node_modules/graphql/jsutils/invariant.js:18:11)
    at assertLeafType (./node_modules/graphql/type/definition.js:102:47)
    at generateIDSelections (./node_modules/relay-compiler/bin/relay-compiler:7806:20)
    at transformField (./node_modules/relay-compiler/bin/relay-compiler:7762:23)
    at ./node_modules/relay-compiler/bin/relay-compiler:7747:15
    at Array.map (native)
    at transformNode (./node_modules/relay-compiler/bin/relay-compiler:7745:37)
    at transformField (./node_modules/relay-compiler/bin/relay-compiler:7760:26)
    at ./node_modules/relay-compiler/bin/relay-compiler:7747:15
    at Array.map (native)

I have no idea what this means. I'm just trying to query a union inside of an object:

some_attr {
  attribute {
    ... on SomeObject {
      something
    }
    ... on AnotherObject {
      anotherthing
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings