Search code examples
arangodbjson-apifoxx

Foxx/ArangoDB: Can you create a response that adhere to JSON API specification?


I am currently writing some micro services with Foxx to be consumed by Ember.js. Ember data plays very nicely with JSON API (http://jsonapi.org) responses. So I tried to serialize the Foxx responses with the json-api-serializer (https://www.npmjs.com/package/json-api-serializer) - but with no luck. I only found the forClient method, but this only allows me to operate on the JSON representation of single objects, not the whole response. So my question: Is it possible to implement JSON API with Foxx/ArangoDB?


Solution

  • You can return arbitrary responses from Foxx routes, so it's entirely possible to generate JSON responses that conform to JSON API.

    However there's no built-in way to do this automatically.

    I don't see anything in json-api-serializer that shouldn't work in Foxx, so I'm not sure what problems you are encountering. You should be able to simply return the output object with res.json(outputFromSerializer) and set the content type with res.set('content-type', 'application/vnd.api+json').

    If everything else fails you can just write your own helper functions to generate the boilerplate and metadata JSON API expects.