storybook/docs/snippets/angular/document-screen-with-graphql.ts.mdx
2021-11-09 01:41:54 +00:00

69 lines
1.6 KiB
Plaintext

```ts
// YourPage.component.ts
import { Component, OnInit } from '@angular/core';
import { Apollo } from 'apollo-angular';
import gql from 'graphql-tag';
@Component({
selector: 'document-screen',
template: `
<div *ngIf="loading">Loading...</div>
<div *ngIf="error">There was an error fetching the data!</div>
<div *ngIf="!loading && subdocuments.length > 0">
<page-layout [user]="user">
<document-header [document]="document"></document-header>
<document-list [documents]="subdocuments"></document-list>
</page-layout>
</div>
`,
})
export class SampleGraphqlComponent implements OnInit {
user: any = { id: 0, name: 'Some User' };
document: any = { id: 0, title: 'Some Title' };
subdocuments: any = [];
error = '';
loading = true;
constructor(private apollo: Apollo) {}
ngOnInit() {
this.apollo
.watchQuery({
query: gql`
query AllInfoQuery {
user {
userID
name
}
document {
id
userID
title
brief
status
}
subdocuments {
id
userID
title
content
status
}
}
`,
})
.valueChanges.subscribe((result: any) => {
this.user = result?.data?.user;
this.document = result?.data?.document;
this.subdocuments = result?.data?.subdocuments;
this.loading = result.loading;
// Errors is an array and we're getting the first item only
this.error = result.errors[0].message;
});
}
}
```