Single schema of truth: JSON Schema / Realm Schema / GraphQL Schema / TypeScript Types
I think the current recommended initial prototyping workflow for a Realm web SDK user is like this:
1) Add first data in collection (manually, on Compass etc.)
2) Generate JSON Schema (manually click a button on web UI)
3) Set up relationship (manually, on web UI)
4) Download GraphQL Schema (manually click a button on web UI)
5) Get TypeScript types and React hooks (automatically generated by graphql-codegen CLI)
6) If you use another Realm SDK somewhere else (ex. Node.js sdk for server side), write Realm Schema (manually)
This is not very developer friendly, and requires back-and-forth work between CLI, browser, VSCode, and Compass etc. It is not a one-time operation; you need to repeat it whenever you change your model structures, which frequently happens when you prototype.
What I think the ideal way is AWS Amplify's one. In Amplify, you just:
1) Write (a part of) GraphQL schema on editor
2) Run a CLI command to apply
Although I noticed pro/cons, it feels like Realm is going with data-first approach. If we can use schema-first approach too, it will be more wonderful. It will be great to have a single source of schema.
At this moment, it seems developers are required to manually check all the schema are aligned and the same, if I understand correctly.
PS. I also checked if I can use mongoose ORM. It seems it also requires another schema of its own.
Hi all,
We recently announced that we have decided to deprecate the Atlas GraphQL API, and current users should’ve received an email explaining the details of the deprecation and end-of-life plan. Consequently, we will not be adding new features going forward and are updating the status for all related feedback accordingly.
Please read our announcement in the MongoDB Developer Center for more details and guidance on the migration process and timeline. Should you have any questions or need further assistance, please reach out to our support team via the MongoDB Support Portal.
We truly prioritize your satisfaction and success, and we thank you for your patience and the feedback you have provided us over the years.
-
Austin commented
+1 to @Toshi example for AWS Amplify. Coming from that and using Realm it seems like a very fragmented experience.
In Amplify, its so nice because you can define all your models in GraphQL and then it autogenerates resolvers/etc for you.
```
type Hero
@model
@auth(
rules: [
{
allow: owner
provider: oidc
ownerField: "owner"
identityClaim: "uid"
operations: [read, create, update, delete]
}
{
allow: groups
provider: oidc
groupsField: "group"
groupClaim: "groups"
operations: [read, create, update, delete]
}
]
) {
id: ID!
name: String
owner: ID
group: ID
age: String
}
```On top of that you can define your permissions using GraphQL directives. The really sad thing about AWS Amplify is it uses DynamoDB which is awful to put it nicely.
-
Jorden commented
The places that you have to add schemas is out of hand. You have to add DIFFERENT schema to the database, then to MongoDB Realm, then you have to take that and give it to your clients. I get that you might want to lock things down in the Realm layer differently than the database, but it is SUPER confusing.
-
Toshi commented
Hi Sumedha, sorry for the delay and thanks for the reply.
> I was able to generate typescript types using the endpoint with GQL Codegen
I will check that again. Last time I tried, it generates a schema which has a lot of @deprecated directives. Is it expected?> The one thing I can think of is including the authorization header
Yes, that was a trap, but I think I solved: I got an access token running curl with API key, and put the token in codegen.yml like: (indent/space is broken here)schema:
- https://realm.mongodb.com/api/client/v2.0/app{my app id here}/graphql:
headers:
Authorization: Bearer {token here}This gave me a schema but with @deprecated directives.
> You should be able to do this on the 'SDKs' tab in the UI.
SDKs tab looks great! (I thought I once checked this tab, I maybe overlooked, sorry..)
One question; are these code snippets only accessible on this page? Could I get them through CLI like graphql-codegen? -
Sumedha Mehta commented
Thanks for clarifying - I've added a couple of answers to some of your questions and will take the feedback into consideration. Also happy to continue the conversation via email.
> The Realm GraphQL endpoint it didn't work with GraphQL codegen correctly, so I use the manually downloaded file instead.
I was able to generate typescript types using the endpoint with GQL Codegen, so I'm curious to know what was going wrong here. The one thing I can think of is including the authorization header in the codegen.yml file to generate the types.
> Is there any way to convert the json schema to realm object schema
You should be able to do this on the 'SDKs' tab in the UI. This would fit into the following flow you described:
adding in Data to Atlas -> generating Schema on Realm from the UI -> clicking on the SDKs tab to see what the JS Realm Objects would look like -> adding Realm Objects to Node SDK
-
Toshi commented
Hi Sumedha, thank you for response. I'm sorry I didn't notice your comment, and I even updated my post after you commented. So you might find some diffs between what you initially read and the current text above. I just didn't expect any response! Sorry and thanks.
So, I started a scratch web-app project. I used React(Next.js) and Apollo client for GraphQL requests. I read many doc pages, but basically followed this one:
https://docs.mongodb.com/realm/tutorial/web-graphql/I created Atlas cluster -> Put some data in collection -> Initialized Realm -> Clicked "Generate schema" button and got schemas by sampling -> Went to the GraphQL page and tried some queries-> Downloaded schema.graphql by clicking button there -> Setup graphql-codegen's yml file locally.
> Is the Web UI you're referring to here, the Atlas/Compass UI?
I referred to the web pages, whose domains are cloud.mongodb.com / realm.mongodb.com.As for your question about difference of #3 and #6:
In addition to a frontend app with React, I'm writing some serverless functions too. I used Next.js(Vercel) build-in functions feature here. On the server side, I wanted to try Realm node.js SDK instead of Apollo, because it looked simpler and attractive in how it defines and handles relationship.
So I read over Realm Node.js SDK docs, but I couldn't come up with good way to keep DRY to align schemas between frondend and function. I also tried realm-cli and got local json schema files (`rules/tracker.users.json` file etc). I thought the json schema files can be the source-of-truth. Is there any way to convert the json schema to realm object schema, or vice versa? Especially I'm lost in which to define relationships. I checked 3rd party tooling (for example quicktype.io), but I couldn't find a good way to go.
Also, I felt that these local json files are not expected to be edited repeatedly. For this need, I put up another idea (maybe I'm wrong):
https://feedback.mongodb.com/forums/930808-mongodb-cli/suggestions/41158219-push-local-settings-pull-remote-settingsOne more note; I think graphql-codegen is really well developed, so GraphQL->TypeScript conversion is ok. I should say JSON->GraphQL->TypeScript schema generation is greatly automated already. One minor problem is currently realm's `schema.graphql` file is only downloadable from the "Download Schema Button" if I'm right. It would be great to download it programatically. I usually use the remote graphql endpoint for the schema path on the codegen.yml file. However with Realm GraphQL endpoint it didn't work correctly, so I use the manually downloaded file instead.
Sorry for the long comment. If I'm behaving not as you recommended/expected (maybe so), please point out. For further communication, I'll be happy to continue with emails.
-
Sumedha Mehta commented
Hi Toshi,
We're planning on working on improving our Schema workflow and local development with Realm this half and I'm interested in some of the feedback you brought up.
Can you go into more detail about the difference between:
#3 "Generate JSON Schema (manually, on web UI)"
and
#6 'If you use Realm too, write Realm Schema (manually)'
Is the Web UI you're referring to here, the Atlas/Compass UI?
If you're available to chat a bit more about this topic, you can also shoot me an email at sumedha.mehta@mongodb.com