如何使用Ember-Apollo连接到Graphql服务器?

iugsix8n  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(156)

我正在使用一个基于GraqlQL的全栈应用程序。服务器工作正常,现在我需要在客户端尝试第一个查询和突变。由于某种原因,“监视”路径和它后面的所有内容都没有显示。下面我将显示我编辑或创建的文件。
items.graphql:

query {
  items {
    _id
    name
  }
}

environment.js:

'use strict';

module.exports = function(environment) {
  let ENV = {
    apollo: {
      apiURL: 'http://localhost:5000/graphql'
    },
    modulePrefix: 'client',
    environment,
    rootURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        //
      },
      EXTEND_PROTOTYPES: {
        Date: false
      }
    },

    APP: {
      //
    }
  };

  if (environment === 'development') {
    //
  }

  if (environment === 'test') {
    ENV.locationType = 'none';

    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
    ENV.APP.autoboot = false;
  }

  if (environment === 'production') {
    //
  }

  return ENV;
};

monitoring.js(路由):

import Route from '@ember/routing/route';
import { queryManager } from 'ember-apollo-client';
import query from 'client/gql/items.graphql';

export default Route.extend({
  apollo: queryManager(),

  model() {
    return this.apollo.watchQuery({ query }, 'items');
  }
});

monitoring.hbs:

<h3>Monitoring</h3>

<div>
  {{#each model as |item|}}
    <h3>{{item.name}}</h3>
  {{/each}}
</div>

{{outlet}}

感谢您的关注!
我看到此错误:

Uncaught (in promise) Error: fetch is not defined - maybe your browser targets are not covering everything you need?
umuewwlo

umuewwlo1#

解决方案是修复两件事。首先是将其放在ember-cli-build.js中:

'ember-fetch': {
  preferNative: true
}

并修复路由文件:

import Route from '@ember/routing/route';
import { queryManager } from 'ember-apollo-client';
import query from 'client/gql/queries/items.graphql';

export default Route.extend({
  apollo: queryManager(),

  async model() {
    let queryResults = await this.apollo.watchQuery({ query }, 'items');
    return Object.values(queryResults);
  }
});

相关问题