<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dipali Goel</title>
    <description>The latest articles on Forem by Dipali Goel (@dipali3dips).</description>
    <link>https://forem.com/dipali3dips</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F446544%2Ff2baeb49-1e81-47ec-a319-15cadc1a75b0.jpg</url>
      <title>Forem: Dipali Goel</title>
      <link>https://forem.com/dipali3dips</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dipali3dips"/>
    <language>en</language>
    <item>
      <title>Decoupled DRUPAL 8 With JSON API</title>
      <dc:creator>Dipali Goel</dc:creator>
      <pubDate>Wed, 09 Sep 2020 14:42:11 +0000</pubDate>
      <link>https://forem.com/itsopensource/decoupled-drupal-8-with-json-api-59ma</link>
      <guid>https://forem.com/itsopensource/decoupled-drupal-8-with-json-api-59ma</guid>
      <description>&lt;h3&gt;
  
  
  Benefits of decoupled architecture
&lt;/h3&gt;

&lt;p&gt;A decoupled architecture does great work in reducing or completely eliminating problems that are a part of the monolithic content management systems.&lt;br&gt;
CMS data is exposed to and consumed by a front-end JavaScript framework. The framework can be React.js, Angular.js, and many more.&lt;/p&gt;
&lt;h3&gt;
  
  
  Json API
&lt;/h3&gt;

&lt;p&gt;In JSON API, the consumer can get all the data with a single query, tracing relationships between objects and returning the desired output in a single shot.&lt;br&gt;
A single request from a consumer can return multiple entities. For the same API, different consumers may use different embedding patterns, depending on the requirement.&lt;br&gt;
All API can be customized according to the requirement which we can achieve by downloading and enabling JSONAPI Extras module.&lt;/p&gt;
&lt;h3&gt;
  
  
  Enable json API
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Using Drush
&lt;span class="nv"&gt;$ &lt;/span&gt;drush en serialization &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;drush en jsonapi &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;drush dl jsonapi_extras &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; drush en jsonapi_extras &lt;span class="nt"&gt;-y&lt;/span&gt;

Using Drupal Console
&lt;span class="nv"&gt;$ &lt;/span&gt;drupal module:install serialization
&lt;span class="nv"&gt;$ &lt;/span&gt;drupal module:install jsonapi
&lt;span class="nv"&gt;$ &lt;/span&gt;drupal module:download jsonapi_extras &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; drupal module:install jsonapi_extras
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;

&lt;p&gt;JSONAPI is a zero-configuration module that gives access to all Drupal entities for reading out of the box.&lt;br&gt;
You need to go to the module settings page &lt;em&gt;&lt;code&gt;(/admin/config/services/jsonapi)&lt;/code&gt;&lt;/em&gt; and make the changes according to the requirement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R-Lqp6ca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e3z4tdbcu5m4ot6pya00.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R-Lqp6ca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e3z4tdbcu5m4ot6pya00.png" alt="Json API"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Retrieving resources with JSON API
&lt;/h3&gt;

&lt;p&gt;JSON API specification recommends that all requests include an Accept header with the correct MIME type for JSON API, &lt;br&gt;
the JSON API module accepts requests without any request headers present.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieving resource collections&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Key motivations for using JSON API instead of REST is the possibility of retrieving multiple resources using JSON API collections. &lt;br&gt;
There is only Views rest export to get the collection entities can be retrieved. In JSON API, simply issue a GET request against the following URL to retrieve a collection of entities eg for articles:&lt;br&gt;
&lt;code&gt;/jsonapi/node/article&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using JSON API specification, you can use certain query parameters to operate on the collections retrieved through the API. &lt;br&gt;
Eg  &lt;code&gt;page[limlit],sort&lt;/code&gt;&lt;br&gt;
&lt;code&gt;/jsonapi/node/article?sort=nid&lt;/code&gt;&lt;br&gt;
&lt;code&gt;/jsonapi/node/article?page[limit]=30&amp;amp;page[offset]=25&lt;/code&gt;&lt;br&gt;
&lt;code&gt;/jsonapi/node/article?page[limit]=30&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieving limited subsets of fields&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
To capture only the title, created and modified timestamps, and body of the entity, you can provide fields query parameter.&lt;br&gt;
Note that with the parameter, the type and bundle must be included as well.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/jsonapi/node/article?fields[node--article]=title,created&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating resources with JSON API
&lt;/h3&gt;

&lt;p&gt;The JSON API specification allows for individual resources to be created through an API.&lt;br&gt;
The following request headers are obligatory on all POST requests in order to generate a standard response:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Accept: application/vnd.api+json&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Content-Type: application/vnd.api+json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To POST request that creates an article, use the same URL that we used to retrieve collections of articles:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/jsonapi/node/article&lt;/code&gt;&lt;br&gt;
For creating an article, you can include a relationship to a user to indicate that they were the one to create the article.&lt;br&gt;
The following payload reflects a relationship that connects the entity to an existing user &lt;code&gt;({{user_uuid}})&lt;/code&gt; &lt;br&gt;
whose authorship is assigned in this POST request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node--article"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"attributes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"My snazzy new article"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hello world! Lorem ipsum dolor sit amet consectetur adipiscing elit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"plain_text"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"relationships"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"uid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user--user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"{{user_uuid}}"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This request will result in a 201 Created response code with the JSON API response of the created entity.&lt;br&gt;
Sample response from postman&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fp-PIgMT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wc69vi6ygwzc95fw31m9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fp-PIgMT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wc69vi6ygwzc95fw31m9.png" alt="postman image 1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eAYjrW2y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zagvn8ua4fitbdd6sfcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eAYjrW2y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zagvn8ua4fitbdd6sfcf.png" alt="postman image 2"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Updating resources with JSON API
&lt;/h3&gt;

&lt;p&gt;PATCH requests also require the following headers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Accept: application/vnd.api+json&lt;br&gt;
Content-Type: application/vnd.api+json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;PATCH&lt;/code&gt; to modifies an article, find the UUID of the article in question and append it to the end of the &lt;br&gt;
URL, as follows:&lt;br&gt;
&lt;code&gt;/jsonapi/node/article/{{node_uuid}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And include a request payload that contains only the fields that you intend to modify and the UUID of the entity in question:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node--article"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"{{node_uuid}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"attributes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Change in article"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T6nYpZ_l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4smtjmsmgiqd0z15dfgn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T6nYpZ_l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4smtjmsmgiqd0z15dfgn.png" alt="Patch 1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yhzbL5l2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zh10aacu5yf7nbubgygx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yhzbL5l2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zh10aacu5yf7nbubgygx.png" alt="patch 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting resources with JSON API
&lt;/h3&gt;

&lt;p&gt;We can delete the artcile which we have created using &lt;code&gt;DELETE&lt;/code&gt; method&lt;br&gt;
Headers :&lt;br&gt;
&lt;code&gt;Content-Type: application/vnd.api+json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With DELETE requests no payload is required. We need to add the correct resource using its UUID and use the &lt;code&gt;Delete&lt;/code&gt; method for that URL.&lt;br&gt;
&lt;code&gt;/jsonapi/node/article/{{node_uuid}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will result in a 204 No Content response code .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zAg1_YR_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/djx9hit3txgb3gkj6hd9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zAg1_YR_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/djx9hit3txgb3gkj6hd9.png" alt="Delete Content"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;With the evolution of web-based APIs, this json format is a better approach than XML. &lt;br&gt;
JSONAPI has a great experience by making the Drupal entities (users, comments and more) available via a web service API. &lt;br&gt;
URLs can be manipulated as per the requirement to access the entity types and bundles using HTTP methods.&lt;/p&gt;

</description>
      <category>php</category>
      <category>drupal</category>
      <category>jsonapi</category>
    </item>
  </channel>
</rss>
