Advancing Drupal Web Services
This article outlines some potential routes the Drupal platform could take to improve its services and become more efficient in the process.
Join the DZone community and get the full member experience.Join For Free
in an earlier blog post , i looked at the web services solutions available in drupal 8 and compared their strengths and weaknesses. that blog post was intended to help developers choose between different solutions when building drupal 8 sites. in this blog post, i want to talk about how to advance drupal's web services beyond drupal 8.1 for the benefit of drupal core contributors, module creators, and technical decision-makers.
moreover, newer headless content-as-a-service solutions (e.g. contentful, prismic.io, backand, and cloudcms) have entered the market and represent a widening interest in content repositories enabling more flexible content delivery. they provide content modeling tools, easy-to-use tools to construct rest apis, and sdks for different programming languages and client-side frameworks.
in my view, we need to do the following, which i summarize in each of the following sections:
facilitate a single robust rest module in core.
add functionality to help web services modules more easily query and manipulate drupal's entity graph.
incorporate graphql and json api out of the box.
add sdks enabling easy integration with drupal.
though i shared some of this in my drupalcon new orleans keynote , i wanted to provide more details in this blog post. i'm hoping to discuss this and revise it based on feedback from you.
one great rest module in core
while core rest can be enabled with only a few configuration changes, the full extent of possibilities in drupal is only unlocked either when leveraging modules, which add to or work alongside core rest's functionality, such as services or relaxed , or when augmenting core rest's capabilities with additional resources to interact with (by providing corresponding plugins ) or using other custom code.
having such disparate rest modules complicates the experience. these rest modules have overlapping or conflicting feature sets, which are shown in the following table.
|feature||core rest||relaxed||services||ideal core rest|
|content entity crud||yes||yes||yes||yes|
|configuration entity crud||create resource plugin ( issue )||create resource plugin||yes||yes|
|custom resources||create resource plugin||create resource plugin||create services plugin||possible without code|
|custom routes||create resource plugin or views rest export (get)||create resource plugin||configurable route prefixes||possible without code|
|translations||not yet ( issue )||yes||create services plugin||yes|
|revisions||create resource plugin||yes||create services plugin||yes|
|file attachments||create resource plugin||yes||create services plugin||yes|
|authenticated user resources (log in/out, password reset)||not yet ( issue )||no||user login and logout||yes|
i would like to see a convergence where all of these can be achieved in drupal core with minimal configuration and minimal code.
working with drupal's entity graph
recently, a discussion at drupalcon new orleans with key contributors to the core rest modules, maintainers of important contributed web services modules, and external observers led to a proposed path forward for all of drupal's web services.
buried inside drupal is an "entity graph" over which different api approaches like traditional rest, json api, and graphql can be layered. these varied approaches all traverse and manipulate drupal's entity graph, with differences solely in the syntax and features made possible by that syntax. unlike core's rest api which only returns a single level (single entity or lists of entities), graphql and json api can return multiple levels of nested entities as the result of a single query. to better understand what this means, have a look at the graphql demo video i shared in my drupalcon barcelona keynote .
what we concluded at drupalcon new orleans is that drupal's graphql and json api implementations require a substantial amount of custom code to traverse and manipulate drupal's entity graph, that there was a lot of duplication in that code, and that there is an opportunity to provide more flexibility and simplicity. therefore, it was agreed that we should first focus on building an "entity graph iterator" that can be reused by json api, graphql, and other modules.
this entity graph iterator would also enable manipulation of the graph, e.g. for aliasing fields in the graph or simplifying the structure. for example, the difference between drupal's "base fields" and "configured fields" is irrelevant to an application developer using drupal's web services api, but drupal's responses leak this internal distinction by prefixing configured fields with
(see the left column in the table below). by the same token, all fields, even if they carry single values, expose the verbosity of drupal's typed data system by being presented as arrays (see the left column in the table below). while there are both advantages and disadvantages to exposing single-value fields as arrays, many developers prefer more control over the output or the ability to opt into simpler outputs.
a good drupal entity graph iterator would simplify the development of drupal web service apis, provide more flexibility over naming and structure, and eliminate duplicate code.
|current core rest (shortened response)||ideal core rest (shortened response)|
graphql and json api in core
while both json api and graphql are preferred over traditional rest due to their ability to provide nested entity relationships, graphql goes a step further than json api by facilitating explicitly client-driven queries, in which the client dictates its data requirements.
sdks to consume web services
while a unified rest api and support for graphql and json api would dramatically improve drupal as a web service back end, we need to be attentive to the needs of consumers of those web services as well by providing sdks and helper libraries for developers new to drupal.
an sdk could make it easy to retrieve an article node, modify a field, and send it back without having to learn the details of drupal's particular rest api implementation or the structure of drupal's underlying data storage. for example, this would allow front-end developers to not have to deal with the details of single- versus multi-value fields, optional vs required fields, validation errors, and so on. as an additional example, incorporating user account creation and password change requests into decoupled applications would empower front-end developers building these forms on a decoupled front end such that they would not need to know anything about how drupal performs user authentication.
i believe that it is important to have first-class web services in drupal out of the box in order to enable top-notch apis and continue our evolution to become api-first.
in parallel with our ongoing work on shoring up our rest module in core, we should provide the underpinnings for even richer web services solutions in the future. with reusable helper functionality that operates on drupal's entity graph available in core, we open the door to graphql, json api, and even our current core rest implementation eventually relying on the same robust foundation. both graphql and json api could also be promising modules in core. last but not least, sdks like hydrant that empower developers to work with drupal without learning its complexities will further advance our web services.
collectively, these tracks of work will make drupal uniquely compelling for application developers within our own community and well beyond.
special thanks to preston so for contributions to this blog post and to moshe weitzman , kyle browning , kris vanderwater , wim leers , sebastian siemssen , tim millwood , ted bowman , and mateu aguiló bosch for their feedback during its writing.
Published at DZone with permission of Dries Buytaert, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.