Search code examples
javascriptgraphql-jsrelayjsrelay

How can I fix Could not create Relay Container for `Link`


I try to follow the tutorial about GraphQL and Relay.

And when I run it (or run yarn test):

Invariant Violation: Could not create Relay Container for `Link`. Expected a set of GraphQL fragments, got `function () {
      const node = require("./__generated__/Link_link.graphql");

      if (node.hash && node.hash !== "4db5e0eb9875bd761c8f8ef68ea507f3") {
        console.error("The definition of 'Link_link' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data.");
      }

      return require("./__generated__/Link_link.graphql");
    }` instead.

      16 | }
      17 |
    > 18 | export default createFragmentContainer(Link, graphql`
         |                ^
      19 |     fragment Link_link on Link {
      20 |         id
      21 |         description

      at invariant (node_modules/fbjs/lib/invariant.js:40:15)

I tried to modify graphql.schema:

type Link implements Node {
  id: ID!
  description: String!
  url: String!
}

to match

export default createFragmentContainer(Link, graphql`
    fragment Link_link on Link {
        id
        description
        url
    }
`)

yet I receive the same error. Also I tried to remove __generated__ folder and rerun relay-compiler with no success.

How can I fix it?


Solution

  • I had a similar problem when upgrading to v4.0.0. It seems that they've removed support for passing the graphql literal as the second argument, so instead you should pass an object like

    export default createFragmentContainer(Link, {
      link: graphql`
        fragment Link_link on Link {
          id
          description
          url
        }
      `
    })
    

    It's annoying but it should work. I hope they fix this issue for future versions.