The PoP API now supports both REST and GraphQL!

We are very proud to announce that PoP can now return the response of its API using both the REST and GraphQL formats. This way, a PoP API can be used as a drop-in replacement for both REST and GraphQL, providing the benefits of both these APIs, at the same time! Check out these benefits:

  • No over/under-fetching data (as in GraphQL)
  • Shape of the response mirrors mirrors the query (as in GraphQL)
  • Passing parameters to the query nodes, at any depth, for filtering/pagination/formatting/etc (as in GraphQL)
  • Server-side caching (as in REST)
  • Secure: Not chance of Denial of Service attacks (as in REST)
  • Provide default data when no query is provided (as in REST)

In a nutshell: the PoP API is REST endpoints with GraphQL queries.

Demonstration

The API is accessed through the following endpoints (click on them to see an example):

REST-compatible:

Add parameters action=api&datastructure=rest to the endpoint URL:

GraphQL-compatible:

Add parameters action=api&datastructure=graphql to the endpoint URL, and parameter fields with the data fields to retrieve (using a custom dot notation). Examples:

PoP native:

Add parameters action=api to the endpoint URL, and parameter fields similar to the examples above for GraphQL.

How does it work?

Through a parameter datastructure in the URL we can select if the response must be REST-compatible or GraphQL-compatible. To fetch the data fields, for REST it supports default fields (as in typical REST behaviour), or explicitly querying for the fields, like in GraphQL. For this, the GraphQL query is converted to dot notation and passed in the URL through parameter fields. For instance, the following query:

query {
  id
  title
  url
  content
  comments {
    id
    content
    date
    author {
      id
      name
      url
      posts {
        id
        title
        url
      }
    }
  }
}

Is converted to dot notation like this:

id|title|url|content,comments.id|content|date,comments.author.id|name|url,comments.author.posts.id|title|url

Enjoy!


Sign up to our newsletter:

Welcome to the PoP framework!
Break the information monopoly

the PoP framework is open source software which aims to decentralize the content flow and break the information monopoly from large internet corporations. Read more.