aribouius / jsonapi-react

A minimal JSON:API client and React hooks for fetching, updating, and caching remote data.
MIT License
149 stars 28 forks source link

Mutation sends field in attributes, not relationships #21

Closed timothyjoh closed 3 years ago

timothyjoh commented 4 years ago

I might be doing something wrong here, but here is my schema:

export const schema = {
  articles: {
    type: 'articles',
    fields: {
      title: 'string',
    },
    relationships: {
      field_template: {
        type: 'field_templates',
      },
      user: {
        type: 'users',
      },
    },
  },
  field_templates: {
    type: 'field_templates',
    fields: {
      field_context: {
        type: 'rich_text',
      },
      id: 'string',
    },
  },
  users: {
    type: 'users',
    fields: {
      name: 'string',
    },
  },
  rich_text: {
    type: 'rich_text',
    fields: {
      value: 'string',
      processed: 'string',
      formatted: 'string',
    },
  },
}

And here is my mutation code:

  const [addArticle, { isLoading, data, error, errors }] = useMutation(
    'node/article'
  )
  const result = await addArticle({
    title,
    field_template: { id: '2398729384-324239823942' },
  })

It always ends up sticking the field_template in the data.attributes instead of in the data.relationships section of the request.

I am using Drupal as a backend, and wondering if I am missing something you can see.

jonahsol commented 3 years ago

Hi @timothyjoh ,

I think you'll need to call useMutation with a key in schema, eg. useMutation('articles'). This will make a request to /articles.

timothyjoh commented 3 years ago

But the proper URL for my API is "node/article" so do I need to reflect that in the schema that way?

aribouius commented 3 years ago

Hey @timothyjoh, so a cursory glance at your example leads be to believe there might be a couple things going on.

aribouius commented 3 years ago

Closing due to inactivity.

HassanAzzam commented 2 months ago

Just came across this and figured out that the schema key MUST have the same value as the resource type and the requested url.