JMJ CLOUD
  • Home
  • Projects
  • Blog
  • About Us
  • Contact Us

Our Blog

Comparing ORDS and REST Enabled SQL to GraphQL

1/10/2019

3 Comments

 

Introduction

ORDS and GraphQL
​REST enabled SQL was first introduced in ORDS 17.4. It is an important feature in the continued evolution of ORDS as a product. REST enabled SQL fits very well into Oracle’s Cloud mantra, in that it enables functionality much akin to a database link but without incurring the resource overhead and the security shortfalls of database links. It essentially provides developers to run Queries, DML, DDL and PL/SQL  statements against an Oracle database via a REST interface.

GraphQL is a relatively new open-source technology, introduced by Facebook. GraphQL is a data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data.

​While the relationship between GraphQL and REST enabled SQL may not be immediately obvious, I believe there are many similarities and plan to describe them in this post.

​What is GraphQL?

From Wikipedia: “GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015. On 7 November 2018, the GraphQL project was moved from Facebook to the newly-established GraphQL foundation, hosted by the non-profit Linux Foundation”.
 
Essentially, GraphQL allows a front-end developer to generate a dynamic data query without needing to know the underlying database technology. Much like REST services therefore, GraphQL abstracts the developer from the underlying database. Because of its dynamic nature, however, GraphQL removes the need to build a REST end-point to cover every resource and data element. Instead, you build one back end end-point to handle all requests. You then dynamically build your query on the front end (using a special JSON format) and submit your query to the server for execution. This avoids the overhead of calling multiple REST resources and then consolidating the responses.
 
GraphQL is not a database, it is a query language that allows you to inquire on data. It requires a backend (think middleware server) that handles the query and dispatches the query to the appropriate database(s) to get the actual data. These databases could be MySQL, Oracle, Postgress etc. An example of a GraphQL API server is an open source product called Prisma.
Prisma Image
​GraphQL allows the client to decide exactly what data it wants. If you are just looking for the customer name and number, you can limit your GraphQL query to just those fields. Most REST services would force you to retrieve all attributes of a customer this bloating the payload.
 
GraphQL is strongly typed and each type describes a set of available fields. This not only facilitates descriptive error messages but also allows you to inquire on the types that are supported by the database.
 
So, what does a GraphQL query look like?
​GraphQL Query

    
Response

    
​You can see that the query itself defines the shape of the data that should be returned.
 
This all sounds great but the GraphQL server does not come for free. It obviously needs to understand everything about the underlying databases. Forgetting the infrastructure requirements, the work of mapping all of that data can be a big hurdle.
 
So how can Oracle RAD stack developers achieve some of the benefits of GraphQL using the software they already own? ORDS and REST enabled SQL to the rescue.

​What is REST Enabled SQL?

REST enabled SQL allows a client to send any SQL statement to an Oracle database and have the database execute that statement.
These statements can include:
  • SELECT, UPDATE, DELETE
  • PL/SQL Blocks
  • SQL*Plus and SQLCL Commands
ORDS and REST Enabled SQL
​Attributes of REST Enabled SQL that are similar to Graph include:
  • The client can inquire on what tables and views are available in the database by inquiring on the data dictionary.
  • The client can inquire on the structure of specific tables and views and receive detailed information on each column (i.e. columnName, jsonColumnName, type, precision, scale and isNullable)
  • The client can request exactly what data it wants by defining it in the SQL statement.
  • REST enabled SQL uses a single end-point “https://<HOST>/ords/<SchemaAlias>/_/sql“ for all inquiries (not multiple separate REST services).
  • Because it is using the Oracle database, the data is strongly typed.
  • You can join multiple tables (an or use views) to consolidate data into one request that would otherwise have required you to call multiple REST resources and then merge the data together.
 
​An interesting point to note here is that much like with GraphQL, a ‘smart’ client can navigate an inquire on a database with no pre-programmed knowledge of the structure of the database.
 
Other attributes of REST Enabled SQL:
  • Queries can be expressed as a simple select statement ‘SELECT SYSDATE FROM DUAL’ or by passing a structured JSON document.
  • REST enabled SQL can be secured by:
    • Schema Authentication (the database schema user name and password).
    • OAuth 2 Client Credentials (client gets a token and then uses that token for subsequent calls)
    • First Party Authentication (Basic Authentication). Utilize a user and password created in Oracle REST Data Services with the SQL Developer role.
 
Probably the biggest mismatch between GraphQL and REST Enabled SQL is that REST enabled SQL assumes the client/developer knows the Oracle database. While this may sound like heresy to the open source world, in the RAD stack world this is not a bad thing. In fact, embracing the power of the Oracle database is something every RAD stack developer should be looking to do.

​Configuring REST Enabled SQL

​To enable REST enabled SQL for your instance, you need to add the following entry to the ORDS parameter file on your server (typically defaults.xml):
<entry key="restEnabledSql.active">true</entry>
 
In order to use REST enabled SQL for a specific schema, you also need to REST enable the schema, just like you would to allow creation of any ORDS services in a schema.
  • Connect to the Schema
  • Run the command: exec ords.enable_schema;
 
Important Note: As mentioned in the documentation “Enabling the REST- Enabled SQL service enables authentication against the Oracle REST Data Service enabled database schemas. This makes the database schemas accessible over HTTPS, using the database password. Oracle highly recommends that you provide strong secure database passwords”.

​Securing REST Enabled SQL

In view of this warning described above, I advise that you create an entirely separate schema for use with REST enabled SQL (and make the schema password very complex). This at least limits your exposure to that one schema. Added to that, you should also avoid using Schema Authentication (i.e. use schema name and password) whenever possible and instead use OAUTH2 Client Credentials Authentication.
 
If a third party needs to access your database using REST enabled SQL, setup OAUTH2 Client Credentials Authentication by performing the following steps:
  • Create a new schema e.g. SYSTEMX
  • Login as SYSTEMX and run ords.enable_schema to enable ORDS in the schema
  • Create the appropriate grants to the schema SYSTEMX for the tables and views you want to provide access to.
  • Create an OAUTH client and assign the role ‘SQL Developer’
  • Provide the client with the client_id and client_secret values from the table ORDS_METADATA.OAUTH_CLIENTS.
  • The client would then use the client id and secret to get a token and use that and would never know the schema password.
Script

    
​Unfortunately, even when you configure OAUTH2 Client Credentials Authentication you can still use Schema Authentication. I would like to see a way of turning Schema Authentication off for a schema that has OAUTH2 Client Credentials Authentication.

​A REST Enabled SQL Example

In this example, I am going to be using the ‘application/sql’ Content-Type. This indicates to ORDS that it should expect a SQL statement in the payload. You can also use ‘application/json’ and pass a structured JSON document. You may notice that I am using OAUTH2 Client Credentials security and passing a token with each call.
 
I have created a table called atx_ev_charging_stations which contains a list of the electric vehicle charging stations in Austin. The first think I need to do is get a definition of the table. REST enabled SQL will always return a metadata array in the response, this describes the columns in the SELECT statement. All I need to do is pass a SQL statement that I know will return no rows and I can get the metadata e.g. 'select * from  atx_ev_charging_stations where 1=2'

    
Result set (truncated for brevity):

    
​I have just shown the first column in the metadata array, but you can see from this the metadata information that is returned about each column.
 
Now I understand the table structure I can form a query to get the charging station name and first line of the address for charging stations at apartments in the zip code 78704.
SQL Statement

    
​Notice I am only asking for two columns from the table. An equivalent REST resource would need to contain all of the columns in order to fulfill the requirements of all inquiries on this resource:
CURL Command

    
Result Set

    

Conclusion

​In this post, I provided some insight into what GraphQL is and how it can be compared to ORDS and REST enabled SQL. I also provided a high-level overview of REST enabled SQL along with a basic example. From this, I hope to convey what a powerful feature REST enabled SQL is and how you may be able to use it in your applications.

Author

Jon Dixon, Co-Founder JMJ Cloud

3 Comments
Ashwin Pingali
1/10/2019 07:50:10 pm

Hi Jon,
Nice article and I think Oracle is moving in the right direction. Can REST enabled SQL be used for getting data from multiple databases, from a single end point.
Currently we are working on creating a single graphql end point that is aggregating data from multiple sources (SQL, noSQL and rest end points). Is this possible at least for a SQL use case where data could be aggregated from multiple data base instances but have a single end point which in turn figures out how to get the data.

Reply
Jon Dixon
2/13/2019 08:47:59 pm

Hi Ashwin,

You can configure a single ORDS server to handle requests for any number of Oracle databases. So, you could have example.com/ords/dev route to the dev database and example.com/ords/prod route the request to the prod database.

You can't consolidate data from multiple databases in a single request but you could orchestrate this from the client end.

Reply
Kevin Zhang
1/14/2019 08:36:30 am

Thanks for this detailed comparison. Security is always the # 1 challenge for enabling RESTful web service. I haven't done any of this yet in my env (APEX).

I will for sure reading this thread again when I do the RESTFUL web service.

Thank you,

Kevin

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    RSS Feed

    Popular Posts

    - APEX Dog Food
    - Cloud ERP & APEX Mashup
    - Modernizing EBS with APEX
    - Easy APEX_WEB_SERVICE
    - Running APEX in RDS
    - ORDS What & Why?

    Categories

    All
    APEX
    AWS
    Fusion Cloud ERP
    INTEGRATION
    MS GRAPH
    OCI
    ORDS
    PaaS
    RAD
    REST
    SOAP

    Archives

    October 2021
    February 2021
    January 2021
    October 2020
    September 2020
    June 2020
    May 2020
    April 2020
    February 2020
    January 2020
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    September 2017
    August 2017
    July 2017
    June 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016

Company

About
Contact
Blog
  • Home
  • Projects
  • Blog
  • About Us
  • Contact Us