Angular上でGraphQL(Apollo)を利用してバックエンド側からJSON形式で任意の値を引っ張ってくる処理を実装しています。
その際、引っ張ってきたresult結果を任意の型に配列で格納したいのですがエラーとなり格納する事ができません。
プロパティが無効である意味はわかんですが、この場合どのようにすればエラー無く格納できますでしょうか?
■ デモを置いてます - Github
https://github.com/inukuji/ApolloDemo/
■ name.ts
export class Name { id: number; name: string; }
■ app.component.ts
import { Component, OnInit } from '@angular/core'; import {Apollo} from 'apollo-angular'; import gql from 'graphql-tag'; import { Name } from './name'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor(private apollo: Apollo) {} names: Name[]; ngOnInit() { this.apollo .watchQuery({ query: gql` { names { id name } } `, }) .valueChanges.subscribe(result => { this.names = result.data.names; }); } }
「this.names = result.data.names;」← VSCode上で「Property 'names' does not exist on type 'unknown'.ts(2339)」となる。
■ 返ってくるJSONデータ
{ "data": { "names": [ { "id": "1", "name": "令和 太郎" }, { "id": "2", "name": "令和 次郎" }, { "id": "3", "name": "令和 三郎" } ] } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/09 05:02