How create new record with graphql?

On the react project I installed the graphql-hooks library (Minimal hooks-first GraphQL client).

   const handleCreateTag = useCallback(() => {
    fetch('https://site-api.datocms.com/items',  {
      method: 'POST',
      headers: {
        Authorization: 'Bearer <token>',
        Accept: 'application/json',
        'X-Api-Version': '3',
        'Content-Type': 'application/vnd.api+json'
      },
      body: JSON.stringify({
        data: {
          type: 'item',
          attributes: {
            tag: '<tagname>',
          },
          relationships: {
            item_type: {
              data: {
                type: 'item_type',
                id: '<item_type_id>'
              }
            }
          }
        }
      }),
    })
    .then((res) => {
      return res.json();
    })
    .then((data) => {
      console.log(data);
    });
  }, []);

This snippet works but I would like to use GraphQL language (mutation).

  • UPDATE: The GraphQL API is readonly on Dato, to create, edit or delete records, you can use our REST API

    – 

Leave a Comment