Demo - How to Make Relationship Queries with DreamFactory’s REST API
by Terence Bennett
• July 6, 2018
Developers often ask how to query related data with DreamFactory’s REST API. For example, how do you fetch a record and its related children as JSON in a single API call? In this post, we’ll demonstrate related data queries with a real example.
1. To follow along, you’ll first need a DreamFactory account. You can sign up for an account here.
2. Log in to your account, go to your DSP admin console, and click on the Applications tab on the left hand side.
3. Click 'Import New App' in the upper right corner and choose the Address Book sample app. This contains related schema for both parent-child and related records via junction table relationships.
Once you’ve imported the application, you can preview it in your DSP.
Use your DSP admin user name and password to log in to the Address Book app and see the app.
4. Go to the 'Manage Schema' and 'Manage Data' tabs to peruse the table layouts and sample data if you wish.
The Contacts table stores basic information about each contact. ContactInfo stores contact information like phone, email, and address. A Contact can have many child ContactInfo records.
The ContactGroups table stores information about Groups. A Contact can belong to any number of Groups. ContactRelationships is a junction table that stores the associations between Contacts and Groups.
5. Click 'API Documentation' to load Swagger REST API interface.
Select 'schema' -> GET /schema/{table_name} to expand.
In the 'table_name' field type 'Contacts' and click the 'Try it out’ button.
In the Response Body, you’ll see the field section and the related objects. Note the relationship names in the JSON (highlighted in red below) and the key value pairs that describe each relationship.
6. Back in the main Swagger API Documentation, Select 'db' -> GET /db/{table_name}.
In the 'table_name' field type 'Contacts'. In the 'ids' field enter a couple of ids from the sample data e.g. 1,3). In the 'related' field type 'ContactInfos_by_contactId,ContactGroups_by_ContactRelationships'. This will pull both the child info records and the groups related by junction. Click 'Try it out' and note the URL call and results.
The Request URLlooks like this, where ‘foo’ is the name of your own DSP.
Take a look at the Response Body and notice the JSON structure for child data, in this example multiple ways to contact a person. Also notice how the JSON encodes the associations between a Contact and zero or more Contact Groups to which the Contact belongs.
We hope this overview has shown you how to make sophisticated queries with DreamFactory’s REST API! Please comment if you have any feedback or questions.
Terence Bennett, CEO of DreamFactory, has a wealth of experience in government IT systems and Google Cloud. His impressive background includes being a former U.S. Navy Intelligence Officer and a former member of Google's Red Team. Prior to becoming CEO, he served as COO at DreamFactory Software.