graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

Confusing error message when posting incorrect data arity

Open ef4 opened this issue 5 years ago • 2 comments

If you accidentally POST { data: [{}] } instead of { data: {} } to a typical create endpoint that does MyResource.build(params), graphiti errors with:

TypeError (no implicit conversion of Symbol into Integer)

This seems like an opportunity to validate the structure earlier and give a better error message.

ef4 avatar Aug 04 '20 01:08 ef4

I definitely agree we should validate earlier and give a better error message. Another semi-common use case is accidentally nesting included inside data, instead of the same level.

We do have a request validator but it's called too late in these cases. We've also depended on open-source libs for this in the past, but since Graphiti extends JSON:API they haven't worked out.

Working on another project at the moment but will prioritize when I switch back. Unless you (or anyone reading) would like to PR!

(BTW - liquid-fire was one of the things that attracted me to Ember when I had the chance to use it a few years ago, that eventually led to JSON:API usage and then this project. Full circle! Cheers! ❤️ )

richmolj avatar Aug 04 '20 10:08 richmolj

Ran into this as well with the side-posting data format.

For posterity the correct structure for creating "user and posts" is:

{
  data: {
    type: "users",
    attributes: {
      name: "New User",
      email: "[email protected]",
      ...
    },
    relationships: {
      posts: {
        data: [
          { :'temp-id' => 'abc123', type: 'posts', method: 'create' }
        ]
      }
    }
  },
  included: [
    {
      :'temp-id' => "abc123",
      type: "posts",
      attributes: {
        title: 'Some Post',
        body: "Here is the post"
        ...
      }
    }
  ]
}

Note relationships is inside the data attribute, but included is a top-level element.

johnmosesman avatar Dec 06 '21 22:12 johnmosesman