Skip to content

(DOCSP-27000): Call a Function for React Native #2535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2023

Conversation

mongodben
Copy link
Collaborator

Pull Request Info

Call a Function for React Native w @realm/react

Jira

Staged Changes

Reminder Checklist

If your PR modifies the docs, you might need to also update some corresponding
pages. Check if completed or N/A.

  • Create Jira ticket for corresponding docs-app-services update(s), if any
  • Checked/updated Admin API
  • Checked/updated CLI reference

Review Guidelines

REVIEWING.md

@mongodben mongodben changed the base branch from master to realm-react-guidance January 30, 2023 22:11
@mongodben mongodben requested a review from takameyer January 30, 2023 22:17
@github-actions
Copy link

github-actions bot commented Jan 30, 2023

Readability for Commit Hash: 5d0632f

You can see any previous Readability scores (if they exist) by looking
at the comment's history.

Flesch Reading Ease scores for changed documents:

  • source/sdk/react-native/app-services/call-a-function: 69.68

The following table can be helpful in assessing the readability score of a document.

Score Difficulty
90-100 Very Easy
80-89 Easy
70-79 Fairly Easy
60-69 Medium
50-59 Fairly Hard
30-49 Hard
0-29 Very Hard

Copy link
Collaborator

@takameyer takameyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly good. I think we do need to show how to add types to the useUser hook in order to define custom function types, data types and profile types.

// ...
return loggedIn ? (
<View>
<UserProvider>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also provide a fallback component to the UserProvider. Then your wrapper above would look like so:

<AppProvider appId={}>
  <UserProvider fallback={<AnonLogin/>}>
      <MyApp/>
  </UserProvider>
</AppProvider>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UserProvider will not render its children until there is a current user set on the app object.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UserProvider will not render its children until there is a current user set on the app object.

yeah it took me an hour+ of debugging to figure that out 😅

since this code isn't in the example i don't think we need to include that here.

tho when we @realm/reactify the auth pages, this pattern will be important to explain (cc @krollins-mdb)

// Call Atlas Function

// Method 1: call with User.callFunction()
const sumMethod1 = await user?.callFunction('sum', numA, numB);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const sumMethod1 = await user?.callFunction('sum', numA, numB);
const sumMethod1 = await user.callFunction('sum', numA, numB);

I need to get a fix out for this immediately. The user from useUser should always be set and never undefined or null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'd rather keep this example as is til that's fixed since the code will throw a TS error if missing ?

Comment on lines +14 to +15
// Method 2: Call with User.function.<Function name>()
const sumMethod2 = await user?.functions.sum(numA, numB);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this would be the preferred syntax. But we need to show an example of how to type this. This can be done on the hook itself.

const user = userUser<FunctionTypes, CustomDataType, UserProfileDataType>;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm confused by the code example and how it relates to calling a function. could you explain a bit more?

Copy link
Collaborator

@takameyer takameyer Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you declare the FunctionTypes as so:

type FunctionTypes = {
  sum: (number, number) => number;
};

const Component = () => {
  const user = userUser<FunctionTypes>();
  
  const sum = user.functions.sum(12, 14); // <- this is now typed
  const sum = user.functions.foo(12, 14); // <- this will now give a type error
  const sum = user.functions.sum("foo", "bar"); // <- this will also now give a type error
}

Then sum will be typed for user.function.sum with typed arguments and a return value.

CustomDataType and UserProfileDataType can be used to add any extra user data and have that typed as well.

The thing is, it is currently not possible to infer what a user has configured in App Services, so they would have to define the types themselves in order to have proper typing in their application. Ideally we should have a way to generate this for our users, but for now, this is the way.

Copy link
Collaborator

@takameyer takameyer Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very well documented. There is an example of this in realm-web. The type is set on the creation of Realm.App and in turn is applied to any user object returned from that app instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks for clarifying. this is quite useful. will include in the docs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, after spending a bit of time with this, it seems that the API is slightly different from what's in the above example.

these differences are:

  1. i need to provide all 3 types to useUser<FunctionTypes, CustomDataType, Realm.DefaultUserProfileData> to not cause a TS error.
  2. FunctionTypes must be a union with Realm.DefaultFunctionsFactory to work. for example:
type FunctionTypes = {
  sum: (a: number, b: number) => number;
} & Realm.DefaultFunctionsFactory;

for these reasons, i'd rather not include this in the docs yet, though i think longer-term it absolutely makes sense to include in the docs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can see the working version with the types in this commit checkpoint: RN types on useUser.

going to revert those changes in the HEAD of the branch.

@mongodben
Copy link
Collaborator Author

Mostly good. I think we do need to show how to add types to the useUser hook in order to define custom function types, data types and profile types.

could you provide an example of how to do this? didn't see anything in the @realm/react unit test suite or elsewhere.

@mongodben mongodben requested a review from takameyer February 1, 2023 22:18
@mongodben mongodben merged commit 0cbfadc into mongodb:realm-react-guidance Feb 6, 2023
mongodben added a commit that referenced this pull request Feb 10, 2023
## Pull Request Info

Call a Function for React Native w @realm/react 

### Jira

- https://jira.mongodb.org/browse/DOCSP-27000

### Staged Changes

- [Call a Function
(RN)](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-27000/sdk/react-native/app-services/call-a-function/)

### Reminder Checklist

If your PR modifies the docs, you might need to also update some
corresponding
pages. Check if completed or N/A.

- [x] Create Jira ticket for corresponding docs-app-services update(s),
if any
- [x] Checked/updated Admin API
- [x] Checked/updated CLI reference

### Review Guidelines


[REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)

---------

Co-authored-by: Chris Bush <[email protected]>
mongodben added a commit that referenced this pull request Mar 21, 2023
## Pull Request Info

Call a Function for React Native w @realm/react 

### Jira

- https://jira.mongodb.org/browse/DOCSP-27000

### Staged Changes

- [Call a Function
(RN)](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-27000/sdk/react-native/app-services/call-a-function/)

### Reminder Checklist

If your PR modifies the docs, you might need to also update some
corresponding
pages. Check if completed or N/A.

- [x] Create Jira ticket for corresponding docs-app-services update(s),
if any
- [x] Checked/updated Admin API
- [x] Checked/updated CLI reference

### Review Guidelines


[REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)

---------

Co-authored-by: Chris Bush <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants