Skip to content

Instantly share code, notes, and snippets.

@dgeb
Last active December 25, 2015 00:32
Show Gist options
  • Save dgeb/3dc80490208ef8e50586 to your computer and use it in GitHub Desktop.
Save dgeb/3dc80490208ef8e50586 to your computer and use it in GitHub Desktop.

This is a little experiment modeling JSON Patch operations as JSONAPI resources. The idea is to consider how bulk operations of multiple types could be modeled within the constraints of the base spec.

The only pre-requisite is support for bulk operations, as described in the experimental bulk extension. It's quite likely that bulk operation support will simply become an optional part of the base spec, rather than a separate extension.

Once bulk support is in place, an array of "operation" resources could be POSTed to an endpoint. This could be a single /operations endpoint or multiple endpoints that accept operations - that would be up to the implementation.

The only other pre-requisite here would be the use of client-generated IDs to link together newly created resources. An alternative approach could identify resources via JSON Pointers.


Here's an example request that creates a new article and comment:

POST /operations
{
  data: [
    {
      type: "operation",
      attributes: {
        op: "add",
        data: {
          type: "article",
          id: "article-1",
          attributes: {
            title: "Faux JSON Patch is nice!"
          },
          relationships: {
            author: {
              data: {
                type: "person",
                id: "1"
              }
            }
          }
        }
      }
    }, {
      type: "operation",
      attributes: {
        op: "add",
        data: {
          type: "comment",
          attributes: {
            body: "Interesting!"
          },
          relationships: {
            author: {
              data: {
                type: "person",
                id: "2"
              }
            },
            article: {
              data: {
                type: "article",
                id: "article-1"
              }
            }
          }
        }
      }
    }
  ]
}

Response:

{
  data: [
    {
      type: "operation",
      id: "op1",
      attributes: {
        op: "add"
      },
      relationships: {
        resource: {
          data: {
            type: "article",
            id: "article-1"
          }
        }
      }
    }, {
      type: "operation",
      id: "op2",
      attributes: {
        op: "add"
      }
      relationships: {
        resource: {
          data: {
            type: "comment",
            id: "comment-133"
          }
        }
      }
    }
  ],
  included: [
    {
      type: "article",
      id: "article-1",
      attributes: {
        title: "Faux JSON Patch is nice!"
      },
      relationships: {
        author: {
          data: {
            type: "person",
            id: "1"
          }
        }
      }
    }, {
      type: "comment",
      id: "comment-133",
      attributes: {
        body: "Interesting!"
      },
      relationships: {
        author: {
          data: {
            type: "person",
            id: "2"
          }
        },
        article: {
          data: {
            type: "article",
            id: "article-1"
          }
        }
      }
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment