Skip to content

Commit ff12f5c

Browse files
cwomackabdallahshaban557david-mcafeewrpecktannerabread
authored
fix(JS): Update Angular Getting Started tutorial with proper schema and file references (#5433)
* update docs to include switch for CLI and existing resources when adding authentication * fix flutter page with proper "existing resources" code sample * fix to file type and minor visual improvement of code sample * add preamble after "Set Up Backend Resources" * add back in the 'Identify User' title/reference in directory.js (out of scope for this PR, saving for Analytics updates overall). * Updates to remove config/dependencies within Blockswitcher, additional PR feedback implemented. * space added for spell check error * New block add for 3 options (2 CLI and 1 Manual config) for Auth backend resources. * Further refinement of blocks and verbiage. Add 3rd Manual Config block for Flutter & mobile. * fixes to create new fragments for Flutter/iOS/Android pages * add change to js preamble * fixes to links that navigating to wrong platform, general feedback from PR Reviews, and added of existing AWS resource links. * Revert callouts * remove a comma that leads to invalid JSON string * revert "jsx" to "dart" for code block * fixes to block switcher links, updates to text, fragments for JS and RN * fixes that should mimic closed PR #5307, but not include unecessary files that were already merged in #5219 * Update src/fragments/lib/auth/js/getting-started-steps-basic-auth-react-native.mdx Co-authored-by: David McAfee <[email protected]> * remove duplicated "amazon-cognito-identity-js" * Add Studio option for block switcher within Auth > Getting Started pages * minor changes to block order and updates to Studio block text * Update src/fragments/lib/auth/android/getting_started/50_configureBackend.mdx add link per suggestion from @wrpeck Co-authored-by: Wesley P <[email protected]> * Update src/fragments/lib/auth/js/getting-started-set-up-backend-resources-react-native.mdx update to new formatting Co-authored-by: Bannon Tanner <[email protected]> * Update src/fragments/lib/auth/js/getting_started/10_setUpBackendResources.mdx Update to new formatting guidelines Co-authored-by: Bannon Tanner <[email protected]> * updates to Auth block names, fix to React Native docs for Social Auth on Android * add update to Android * Updates to preamble wording to include additional options. * going with option #3 * update file reference for Angular tutorial, add what the final code block should look like for restaurants.component.ts * remove yarn.lock * update GraphQL Schema example to eliminate tutorial errors --------- Co-authored-by: Abdallah Shaban <[email protected]> Co-authored-by: David McAfee <[email protected]> Co-authored-by: Wesley P <[email protected]> Co-authored-by: Bannon Tanner <[email protected]> Co-authored-by: Katie Goines <[email protected]>
1 parent 69f91a9 commit ff12f5c

File tree

1 file changed

+71
-8
lines changed

1 file changed

+71
-8
lines changed

src/fragments/start/getting-started/angular/data-model.mdx

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ or continue
2424
The generated schema is for a Todo app. Replace the GraphQL schema at `amplify/backend/api/RestaurantAPI/schema.graphql` with the following:
2525

2626
```graphql
27-
type Restaurant @model {
27+
type Restaurant @model @auth(rules: [{ allow: public }]){
2828
id: ID!
2929
name: String!
3030
description: String!
3131
city: String!
32-
}
32+
}
3333
```
3434

35-
You'll notice a directive on the `Restaurant` type of `@model`. This directive is part of Amplify's [GraphQL transformer](/cli/graphql/data-modeling) functionality.
36-
37-
The GraphQL Transform Library provides custom directives you can use in your schema that allow you to do things like define data models, set up authentication and authorization rules, configure serverless functions as resolvers, and more.
35+
The GraphQL Transform Library provides custom directives you can use in your schema that allow you to do things like define data models, set up authentication and authorization rules, configure serverless functions as resolvers, and more. In the example schema above, the `@model` directive is part of Amplify's [GraphQL transformer](/cli/graphql/data-modeling) functionality and the `@auth` directive determines what [authorization rules](/cli/graphql/authorization-rules/) to apply.
3836

3937
A type decorated with the `@model` directive will scaffold out the database table for the type (Restaurant table), the schema for CRUD (create, read, update, delete) and list operations, and the GraphQL resolvers needed to make everything work together.
4038

@@ -220,7 +218,7 @@ Next, add a form that will be used for creating restaurants. Add the following t
220218
</div>
221219
```
222220

223-
Next, update your `RestaurantsComponent` class so that it will list all restaurants in the database when the app starts. To do so, implement [OnInit](https://angular.io/api/core/OnInit) add a `ListRestaurants` query in `src/app/restaurants/restaurants.component.ts`. Store the query results in an array.
221+
Next, update your `RestaurantsComponent` class so that it will list all restaurants in the database when the app starts. To do so, implement [OnInit](https://angular.io/api/core/OnInit) and add a `ListRestaurants` query in `src/app/restaurants/restaurants.component.ts`. Store the query results in an array.
224222

225223
```typescript
226224
import { Component, OnInit } from '@angular/core';
@@ -277,7 +275,7 @@ Add the following to your `src/app/restaurants/restaurants.component.html` to di
277275
</div>
278276
```
279277

280-
To subscribe to realtime data, declare a subscription class variable and update `ngOnInit` in `src/app/app.component.ts`. When the app starts, this code will set up a subscription. The subscription will update the `restaurants` array when new events are received (when a new restaurant is created):
278+
To subscribe to realtime data, declare a subscription class variable and update `ngOnInit` in `src/app/restaurants.component.ts`. When the app starts, this code will set up a subscription. The subscription will update the `restaurants` array when new events are received (when a new restaurant is created):
281279

282280
```typescript
283281
/** Subscription type will be inferred from this library */
@@ -303,7 +301,7 @@ async ngOnInit() {
303301
}
304302
```
305303

306-
Finally, unsubscribe from the subscription when the component is destroyed. Import and add `OnDestroy` in `src/app/app.component.ts`:
304+
Finally, unsubscribe from the subscription when the component is destroyed. Import and add `OnDestroy` in `src/app/restaurants.component.ts`:
307305

308306
```typescript
309307
import { Component, OnDestroy, OnInit } from '@angular/core';
@@ -317,6 +315,71 @@ export class AppComponent implements OnInit, OnDestroy {
317315
this.subscription = null;
318316
}
319317
```
318+
The final `restaurants.component.ts` file should look like the following:
319+
320+
```typescript
321+
import { Component, OnInit, OnDestroy } from '@angular/core';
322+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
323+
import { APIService, Restaurant } from '../API.service';
324+
325+
/** Subscription type will be inferred from this library */
326+
import { ZenObservable } from 'zen-observable-ts';
327+
328+
@Component({
329+
selector: 'app-restaurants',
330+
templateUrl: './restaurants.component.html',
331+
styleUrls: ['./restaurants.component.css']
332+
})
333+
export class RestaurantsComponent implements OnInit, OnDestroy {
334+
public createForm: FormGroup;
335+
336+
/* declare restaurants variable */
337+
public restaurants: Array<Restaurant> = [];
338+
/** Declare subscription variable */
339+
private subscription: ZenObservable.Subscription | null = null;
340+
341+
constructor(private api: APIService, private fb: FormBuilder) {
342+
this.createForm = this.fb.group({
343+
name: ['', Validators.required],
344+
description: ['', Validators.required],
345+
city: ['', Validators.required]
346+
});
347+
}
348+
349+
async ngOnInit() {
350+
this.api.ListRestaurants().then(event => {
351+
this.restaurants = event.items as Restaurant[];
352+
});
353+
354+
/* subscribe to new restaurants being created */
355+
this.subscription = this.api.OnCreateRestaurantListener().subscribe(
356+
(event: any) => {
357+
const newRestaurant = event.value.data.onCreateRestaurant;
358+
this.restaurants = [newRestaurant, ...this.restaurants];
359+
}
360+
);
361+
}
362+
363+
ngOnDestroy() {
364+
if (this.subscription) {
365+
this.subscription.unsubscribe();
366+
}
367+
this.subscription = null;
368+
}
369+
370+
public onCreate(restaurant: Restaurant) {
371+
this.api
372+
.CreateRestaurant(restaurant)
373+
.then((event) => {
374+
console.log('item created!');
375+
this.createForm.reset();
376+
})
377+
.catch((e) => {
378+
console.log('error creating restaurant...', e);
379+
});
380+
}
381+
}
382+
```
320383
321384
Next, run the app:
322385

0 commit comments

Comments
 (0)