如何创建从Apollo GraphQL Server获取数据的Ember Service

thtygnil  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(222)

我使用ember-apollo-client成功地从Amber路由中的Apollo GraphQL Server获取数据。我尝试了同样的方法让一个服务获取数据,但是我从app/services/nav-categories.js获取了Uncaught TypeError: this.apollo.query is not a function

最小工作示例

启动新应用程序,使用

  1. $ ember new super-rentals --lang en
  2. $ ember generate service nav-categories

config/environment.js中配置Apollo端点:

  1. module.exports = function (environment) {
  2. const ENV = {
  3. ...
  4. apollo: {
  5. apiURL: 'http://localhost:4000',
  6. }
  7. };

app/gql/queries/allCategories.graphql

  1. query AllCategories {
  2. categories {
  3. name
  4. }
  5. }

app/services/nav-categories.js

  1. import Service from '@ember/service';
  2. import { queryManager } from "ember-apollo-client";
  3. import allCategories from "super-rentals/gql/queries/allCategories.graphql";
  4. export default class NavCategoriesService extends Service {
  5. constructor() {
  6. super();
  7. this.apollo = queryManager();
  8. this.categories = this.apollo.query({ query: allCategories });
  9. }
  10. }

app/components/my-header.js

  1. import Component from '@glimmer/component';
  2. import { service } from '@ember/service';
  3. export default class CartContentsComponent extends Component {
  4. // Will load the service defined in: app/services/nav-categories.js
  5. @service navCategories;
  6. }

app/components/my-header.hbs

  1. <ul>
  2. {{#each this.navCategories.categories as |category|}}
  3. <li>{{category.name}}</li>
  4. {{/each}}
  5. </ul>

app/templates/application.hbs

  1. <MyHeader></MyHeader>
  2. {{outlet}}
nkcskrwz

nkcskrwz1#

根据文档,query不返回数组(根据使用情况,它看起来像你所期望的那样)。
query返回promise。
它返回一个promise,该promise使用查询返回的原始POJO数据进行解析。
因此,这意味着您不能将categories字段同步设置到categories列表中。

在.then后设置属性

请记住,您需要自己处理早期破坏保护,以便测试稳定

  1. import { isDestroying, isDestroyed } from '@ember/destroyable';
  2. export default class NavCategoriesService extends Service {
  3. @tracked categories = [];
  4. constructor() {
  5. super();
  6. this.apollo = queryManager();
  7. this.apollo.query({ query: allCategories })
  8. .then(pojoData => {
  9. // we can't set properties on `this` if `this` is destroyed
  10. if (isDestroying(this) || isDestroyed(this)) return;
  11. this.categories = pojoData.whatever.results;
  12. });
  13. }
  14. }

使用资源实用程序帮助管理promise涉及的状态

Promises有错误、加载等状态,所以正确处理所有这些是很好的。有公用事业

  1. import { trackedFunction } from 'ember-resources/util/function';
  2. import { use, resourceFactory } from 'ember-resources';
  3. function Categories() {
  4. return trackedFunction(async () => {
  5. let apollo = queryManager();
  6. return apollo.query({ query: allCategories });
  7. });
  8. })
  9. export default class NavCategoriesService extends Service {
  10. @use categoriesRequest = Categories();
  11. get isLoading() {
  12. return this.categoriesRequest.isLoading;
  13. }
  14. get categories() {
  15. return this.categoriesRequset.value ?? [];
  16. }
  17. }

更多信息:https://github.com/NullVoxPopuli/ember-resources/

展开查看全部

相关问题