Using GraphQL with Oracle's MERN Stack
This post will discuss using GraphQL with the Oracle's MERN Stack. This is Oracle's full stack for Web and Mobile development based on Parse Platform. It will discuss using the Parse Dashboard GraphQL Playground to prototype queries and run through some basic GraphQL concepts. The next post will discuss how to use GraphQL from the GameScore Mobile Application Demo which is written in Flutter
Why GraphQL
GraphQL offers several advantages that might make you want to use it in your web and mobile development projects:
Overall, GraphQL offers greater flexibility and efficiency compared to traditional REST APIs, making it a compelling choice for many modern web and mobile applications, particularly those with complex data requirements or evolving frontend needs.
GraphQL Playground
When installing the Mobile Application Development Stack to the Oracle Cloud, the Parse Dashboard is configured, booted and ready to use OOTB. The Parse Dashboard provides a slew of management and configuration capabilities for Parse Server. It also provides code Playgrounds, 3 specifically. They are REST, JavaScript and GraphQL.
The GraphQL Playground is divided into 3 columns
The query above gets all the GameScore documents from the GameScore collection. The query supports pagination and uses edges and nodes. In GraphQL, an "edge" is a concept used within connection-based queries to represent a single item in a list or collection of data. It is typically used in combination with "nodes" to structure the data in a standardized way, especially when retrieving paginated data. Here's how it works:
In this example, we're retrieving all the GameScore documents, and for each GameScore, we're getting the "id", "playerName", "score" and "cheatmode within the "node" object. The "cursor" within each "edge" helps with pagination, and "totalCount" in the "connection" object gives the total count of nodes in the collection.
The data returned has 3 nodes and edges with the edges containing the relative cursor position. There is pageInfo information prior to the edges. In GraphQL, "PageInfo" is an object type used to provide information about pagination in connection-based queries. Connection-based queries are often used to retrieve lists of items, and PageInfo helps in managing pagination by offering details about the current page of data and the ability to navigate through the dataset. PageInfo typically includes the following fields:
The PageInfo object is often included as part of a connection-based query response to provide clients with essential information for paginating through a dataset. This allows developers to implement efficient and user-friendly pagination in GraphQL APIs.
Creating a New Collection
The following mutation creates a new Course schema.
This mutation created a schema entry for the Course collection
But what is a mutation?
In GraphQL, a mutation is a type of operation that is used to modify or change data on the server. While queries in GraphQL are used to retrieve data, mutations are used when you want to create, update, or delete data. Mutations are similar in structure to queries but are designed to make modifications to the data rather than fetching it. Here are some key points about GraphQL mutations:
Dynamic Doc
Once the Course class is created, new queries and mutations are also created and immediately available in the documentation. But first, refetch the GraphQL schema from Parse Server by clicking the button on the lower left
Drill down in Query to view the new Course Query APIs
And drill down in Mutation to see the new Course Mutation APIs
Dynamic API Doc, pretty impressive imho
Create Course Document
Let's use one of these new mutations to create a Course document.
Successful creation of a Course document. You can see the full document that was created in the return pane. It has the clientMutationId for request correlation and the all the fields that make up a course document instance including unique ID, timestamps and Access Control Lists.
Summary
I hope this overview of GraphQL was helpful and encourages you to take a look at the new Mobile and Web Development Stack deployed to the Oracle cloud. Everything I demonstrated here can be done OOTB from an Oracle's MERN Stack. Look for the next post which will cover using GraphQL from within a Flutter Mobile Application.
Architect and Developer Advocate, Oracle. XR, Hologram, Immersive Tech Developer.
2ynice!