Apollo-link-rest: 当服务器返回不符合graphql的结果时我该怎么办

创建于 2018-06-20  ·  6评论  ·  资料来源: apollographql/apollo-link-rest

我想要做的是通过其 http 休息接口向bcoin查询帐户信息。
为此,我必须

  1. 通过调用GET /wallet/:id/account询问所有现有accountName的名称
  2. 通过GET /wallet/:id/account/:accountName询问帐户详细信息

问题是第一次调用会返回类似["default", "mySecondAccountName"]
我不知道如何解析为 graphql 响应,也不知道如何直接传递给第二个GET

例如,当我尝试通过以下查询进行查询时,

query (id: "primary") @rest(type: AccountName, path: "/wallet/:id/account") {
  default

我会得到Error: Network error: Cannot create property 'default' on string 'default'

我在文档中找不到任何线索,但这似乎很常见,所以我一定遗漏了一些非常简单的东西。 如果有人能指出我应该寻找什么,我真的很感激

question❔

最有用的评论

@joemphilips我认为你应该使用typePatcher

我用它来处理不符合 graphql 的响应,例如

{
   "100": {
      "id": 100,
      "node_id": 105,
      "category_id": 8,
      "title": "Title 100",
      "highlight": true
   },
   "138": {
      "id": 138,
      "node_id": 106,
      "category_id": 7,
      "title": "Title 138",
      "highlight": false
   },
   "140": {
      "id": 140,
      "node_id": 101,
      "category_id": 9,
      "title": "Title 140",
      "highlight": false
   },
   "158": {
      "id": 158,
      "node_id": 146,
      "category_id": 4,
      "title": "Title 158",
      "highlight": false
   }
}

查询定义

import { gql } from 'apollo-boost';

export const GET_NEWS_QUERY = gql`
  query getNews {
    News @rest(type: "News", path: "news") {
      data
    }
  }
`;

RestLink 配置

const restLink = new RestLink({
  uri: API_URL,
  typePatcher: {
    News: (data, outerType) => {
      if (outerType === 'News') {
        data = Object.keys(data).map(id => {
          const result = data[id];

          if (result) {
            return {
              __typename: 'News',
              ...result
            };
          }
        });
      }

      return { data };
    }
  }
});

查询用法
```js
使成为() {
返回 (
{({ 加载,错误,数据 }) => {
如果(加载){
返回 (

正在加载...

);
}

      if (error) {
        return (
          <View>
            <Text>Error :(</Text>
          </View>
        );
      }

      const news = data && data.News && data.News.data;
      const filteredNews = news && news.filter(Boolean);

      return (
        <Styles.Container>
          <Carousel
            data={filteredNews}
            handleOnPress={this.navigateToScreen}
          />
        </Styles.Container>
      );
    }}
  </Query>
);

}
`
``

所有6条评论

我只是猜测,因为我不熟悉 bcoin,但这里有一个建议:

query Account(id: $id){
   account(id: $id) @rest(type: "[String]", path: "/wallet/:id/account")
}

然后您从数据中获取结果并将其传递给获取帐户详细信息的新查询。

感谢您的友好和快速响应。 @fbartho
也许我应该没有提到bcoin,我的问题更简单

在文档中的@export指令示例中,有调用 REST 端点两次的示例
我的问题是,当本示例中的第一个查询返回标量数组而不是对象数组时,我该怎么办?
由于它没有键,似乎没有办法指定应该导出哪个参数。

我也试过不使用@export而是像你提到的那样查询,但 fetch 的结果是{}
有以下警告

Missing field account in {}

顺便说一句,我认为在你的建议中

query Account(id: $id)

应该

query Account($id: id)

我发现当我运行你建议的命令时,即

query Account(id: $id){
   account(id: $id) @rest(type: "[String]", path: "/wallet/:id/account")
}

它实际上不会执行任何查询,它只会返回{}作为data
所以我必须在account行之后指定{ theFieldNameIDesire } account ,问题是我没有要指定的字段名称:(

@joemphilips我认为你应该使用typePatcher

我用它来处理不符合 graphql 的响应,例如

{
   "100": {
      "id": 100,
      "node_id": 105,
      "category_id": 8,
      "title": "Title 100",
      "highlight": true
   },
   "138": {
      "id": 138,
      "node_id": 106,
      "category_id": 7,
      "title": "Title 138",
      "highlight": false
   },
   "140": {
      "id": 140,
      "node_id": 101,
      "category_id": 9,
      "title": "Title 140",
      "highlight": false
   },
   "158": {
      "id": 158,
      "node_id": 146,
      "category_id": 4,
      "title": "Title 158",
      "highlight": false
   }
}

查询定义

import { gql } from 'apollo-boost';

export const GET_NEWS_QUERY = gql`
  query getNews {
    News @rest(type: "News", path: "news") {
      data
    }
  }
`;

RestLink 配置

const restLink = new RestLink({
  uri: API_URL,
  typePatcher: {
    News: (data, outerType) => {
      if (outerType === 'News') {
        data = Object.keys(data).map(id => {
          const result = data[id];

          if (result) {
            return {
              __typename: 'News',
              ...result
            };
          }
        });
      }

      return { data };
    }
  }
});

查询用法
```js
使成为() {
返回 (
{({ 加载,错误,数据 }) => {
如果(加载){
返回 (

正在加载...

);
}

      if (error) {
        return (
          <View>
            <Text>Error :(</Text>
          </View>
        );
      }

      const news = data && data.News && data.News.data;
      const filteredNews = news && news.filter(Boolean);

      return (
        <Styles.Container>
          <Carousel
            data={filteredNews}
            handleOnPress={this.navigateToScreen}
          />
        </Styles.Container>
      );
    }}
  </Query>
);

}
`
``

抱歉回复晚了。 您的解决方案似乎不适用于数组的字符串/标量。 但它似乎是固定的。 我会再试一次。 如果我一直遇到麻烦,可能会重新开放。 谢谢。

此页面是否有帮助?
0 / 5 - 0 等级