Learn and fun with CPL (Crypto Premier League): Built with Grafbase
A Fantasy Trading platform on Crypto market to enhance your financial learning skills. After all practice makes us perfect.
Inspiration
While many fantasy gaming and trading applications exist in the market none are open source. CPL is an initiative to provide you with a prototype towards such an application which is linked with the crypto market(but not limited to).
After all, money that lies in saving will lose its value over time
Why Fantasy trading application with Grafbase? Real-time feed, Learning, complex calculations and too many moving parts at the same time. It's a good opportunity to test the time required for development. Plus, I have already an application around it developed a few days ago in AWS AppSync using AWS Amplify ๐ฌ.
Having already built a similar application using AWS Amplify. The goal was to migrate to Grafbase. In the process learn, explore and identify the benefits and challenges of each.
What is CPL?
CPL is a fantasy trading platform allowing one to practice investment in markets without investing or losing real money. You get 1M points at the start of the match, your goal is to choose from a single stock to invest as long as you think or withdraw/change your stock to maximise profit. One can enter the match and chose not to invest at all (this is called testing patience and waiting for the right time to enter). At the end of the contest, it's all about how well you are up to date with the market. If you think the stock will fall then make a sell trade (shorting) or go long with buy. Typically called Bulls vs Bears. On a side note, even I wasn't affluent with market terminologies until I started building an application on fantasy trading.
CPL Gamification overview
To add gamification to the project,
We have rates refreshing every 5-second interval (backoff strategy to avoid API usage limits)
Leaderboards refresh whenever someone joins/invests or withdraws
Plus all logged-in users have a chance to chat with each other, share their strategies or tips or jokes
You don't need to logged-in all the time. As they say, money should work for you while you sleep ๐
CPL Introduction
Technical Architecture of CPL with Grafbase
Demo and Source Code
Source Code: Github
Live demo: grafbase-godwinpinto.vercel.app (best suited on operating system dark mode and desktop)
What can one learn from this project (for developers)?
Migration Guide / How to from AWS AppSync to Grafbase
VueJS integration with Grafbase
Grafbase basic authentication(unexplored areas include owner, groups, etc) and authorization with Auth0 (using social provider i.e. Google sign-in)
Pusher (Real-time messages / Pub-sub) in the absence of subscription-based / live queries on Grafbase
Hosting and serverless API development, with Vercel
Realtime crypto feed from cryptocompare.com
Grafbase
With Grafbase you can instantly have APIs ready to be consumed by your web and mobile applications. No need to write server-side code to just fetch data from the database. Plus Grafbase is Serverless meaning you are charged on a nominal basis i.e. per API calls. Which may be a lower TCO (total cost of ownership) compared to managing your own Graph QL instance 24X7, and highly available. Grafbase does all this for you.
Why would one migrate from AWS AppSync to Grafbase?
Check this out: https://grafbase.com/alternatives/appsync
From my initial experiences, below were some highlights,
Grafbase is more natural for Graph QL developers
Local GraphQL development instance with Grafbase CLI "npx grafbase dev" is ๐. And instantly you have a graphql instance to experiment with. With AppSync, your billing meter starts because your APIs are responding from AWS Servers and not from a local instance. And now imagine you have a heavy data-driven application development (which means you need to clear data from your servers regularly) and many application developers working on it. And someone just wrote a buggy code to insert lots of data during development.
Github Branches to create separate instances/versions of your database
Simple Pricing model
Let the migration begin!
Typically you start development with App Sync with AWS Amplify for the eco-system it provides including code-gen and simpler integrations with Auth, hosting, storage and other products.
A short note before my audience reviews the source code, I am not an expert on frontend stack or GraphQL. My exploration of AppSync and Grafbase is just in total 15 days of overall effort).
All comparisons are based on my experience with Grafbase and AWS AppSync's built-in integration with Amplify working on the same project and may be limited by my knowledge and time.
Schema views:
In AppSync, you additionally write @hasMany and @belongsTo to indicate the dependency graph in structure development
With Grafbase @search annotation makes any fields searchable in your queries, while AppSync by default create a "list" query type which is searchable by any field
Amplify uses a strict AWSDate while Grafbase uses Date (the more familiar syntax of widely used Graph QL.
Backend Authorization to your objects:
With AWS:
@aws_api_key
- To specify the field isAPI_KEY
authorized.@aws_iam
- To specify that the field isAWS_IAM
authorized.@aws_oidc
- To specify that the field isOPENID_CONNECT
authorized.@aws_cognito_user_pools
- To specify that the field isAMAZON_COGNITO_USER_POOLS
authorized.@aws_lambda
- To specify that the field isAWS_LAMBDA
authorized.
With Grafbase
OIDC, JWT, JWKS and Custom
And the syntax is simply @auth
Now I leave it up to you to decide, if you prefer simplicity or more complex controls.
Viewing data
With Appsync you can view your data with explorer style in App Sync
or from the AWS Management Console -> dynamo DB without writing any query
-
With Grafbase you get a Pathfinder web application with "npx grafbase dev" command
One advantage of having the Pathfinder is that you can test your queries before putting them in your code. what you might miss is the ease of viewing and operating data in a tabular and button-click format. Although behind the scene it is dynamo db
Also, you need to ask if you are ok to handle the complexities (if your application is secure or open to threat because of some networking config going wrong arising out of some settings of which you are not aware) of having an AWS account. If you want extreme simplicity then Grafbase.
Mutation in Grafbase and AppSync
and the code
Not much difference in code. However, you get mutations and queries by default with Amplify and AppSync
Linking child-parent relation in Grafbase and AppSync
Grafbase uses a link while AppSync provides a parent[child]Id to link its parent with the child when using mutations
Queries in Grafbase and AppSync
and the code
Real-time subscriptions
With AppSync you can subscribe to create, delete, and update events separately. While in Grafbase you simply add @live to listen to all three events in one go and filter out based on the "op" (operation type) node in response.
@live queries are being sunsetted this September 2023, so you have alternatives to fallback to pub subsystems like a Pusher service provider or poll at intervals until we have some other alternative
VueJS and integrations
One can use the source code hosted on Github as a reference point to gain various concepts of integration with VueJS's latest build as of August 2023 with Vite along with Typescript.
Grafbase Auth integration with Auth0 and Google Sign-in(src/stores/userStore.ts)
Apollo client to make Grafbase queries and mutations (src/utils/apolloLink.ts)
VueJS integration with Pusher on the client side (src/components/ChatWidget.vue)
Servless API and Hosting with Vercel
Install "vercel" CLI and simply type vessel and follow the instructions. Refer to "/api" directory to know how to build a serverless function on Vercel
What I would love to see in the Future with Grafbase
Live Queries / Subscriptions: For applications needing real-time updates, this feature was a blessing. But I also understand other complexities of the ecosystem.
Auto-types generation: While with this project I have generated my typescript types using schema definition from Grafbase Pathfinder and then pasting on https://the-guild.dev/graphql/codegen. It would be good to have a type gen CLI command directly integrated
Auto Queries and Mutation Generation: The reference queries and mutations were generated using https://www.npmjs.com/package/gqlg. Hoping to have ready queries and mutations CLI generator command directly integrated.
Custom Resolver: Provision to write our custom resolvers similarly to writing serverless functions would be a great add-on with custom packages. In my case, an API developed on Vercel to publish on Pusher could then be eliminated.
Pre-configured GraphQL client: A preconfigured graphQL client not binding to any framework like React / Vue, etc, would be a great addon to develop things faster.
Grafbase is promising and looking forward to upcoming releases and features.
Points 2,3,5 are not show stoppers but just make adoption easy for beginners.
CPL is not limited to Crypto
While the feed from cryptocompare.com currently is showing only for Crypto markets, the possibility of displaying US or Indian markets is easy. However, I am not sure when the judges or audience timings would be when visiting the demo and in that case, the feel of near real-time rates would lose the excitement of leaderboard with rates refresh.
What more can be built on CPL?
Last matches winners
Weekly Leaderboard
Monthly Leaderboards
Mobile responsive
Difference contest segments (Crypto / US / Indian) with different stocks
A proper administrator panel
Moving from a Vuejs-dependent stock feed creator to backend based feed
Timeline taken for this migration
Exploring Grafbase documentation & Videos - 3/4 hours
Understanding and shortlisting a Graphql client (Apollo) - 4 hours
Integrating Auth0 into Grafbase with authentication and authorization- 8 hours (now after doing it once, it's just an hour job). Lack of starter documentation/blogs/articles.
Actual migration of code (i.e. writing queries and mutations and modifying code appropriately, 16 in total) - 12 hours
This section is to give you an idea when adopting/migrating to Grafbase.
Concluding thoughts
While AWS App Sync is eased at its best in combination with the AWS Amplify ecosystem, if you want to keep it simple and avoid managing complexities Grafbase is the way to go. However, if you want more deeper controls then AppSync with Amplify makes sense. However, some can even look for a third alternative. i.e. AWS Amplify with Grafbase too.
Lastly, thanking Grafbase and Hashnode for giving me this opportunity to explore a promising offering with Grafbase.
Thank you, everyone, for reading this article.