【问题标题】:How to use ember-apollo-client?如何使用 ember-apollo-client?
【发布时间】:2019-10-26 05:35:43
【问题描述】:

我正在尝试通过 apollo-client 使用 graphql。前端有 Ember,后端有 django 和 graphql 服务器,在本教程中实现 (https://www.howtographql.com/graphql-python/0-introduction/)。 Ember 应用在​​ 4200 端口,graphql 服务器在 8000/graphql 上代理(ember s --proxy http://localhost:8000

我尝试遵循本指南 (https://www.howtographql.com/ember-apollo/1-getting-started/),但没有使用 graphcool,因为我们有后端。

在控制器 signup.js 我有

import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import Mutation from '../gql/mutation';

我是这样注入阿波罗的

export default Controller.extend({
    apollo: Ember.inject.service(),

突变是这样的

actions: {
    signUp() {
        let username = this.get('username');
        let password = this.get('password');
        let email = this.get('email');
        const credentials = { username, password, email };
        let mutation = Mutation.CREATE_USER;

        return this.get('apollo').mutate({mutation, credentials}, 'createUser').then(() => {
        this.set('username', '');
        this.set('email', '');
        this.set('password', '');
        console.log('user created successfully!');
    }).catch(error => console.log('ERROR!!!!111:', error));

点击表单中的提交按钮后,我收到了这条消息

POST http://localhost:4200/graphql 500 (Internal Server Error)
ServerParseError: Unexpected token < in JSON at position 0

我能做什么?

【问题讨论】:

  • 您确定这不是 API 问题吗?如果您可以包含请求有效负载,将会很有帮助。请注意,如果使用最新版本的 Ember,您可以使用 async / await。使代码更具 IMO 可读性。

标签: python django ember.js graphql apollo-client


【解决方案1】:

好的,有几件事可以帮助您:

  • 如果您使用的是 Chrome 并转至开发工具,请转至网络 -> 预览。这应该会显示一些标记,其中包含有关错误的更多信息。
  • 我不确定您的 graphql 变异文件是什么样的,但您可能应该像 import mutation from ../gql/mutation 一样导入它。然后用this.get('apollo').mutate({mutation, credentials}, 'createUser') 调用它
  • 确保您的 graphql 文件的相对路径正确
  • 我建议让您的路线扩展 ComponentQueryManager,然后删除注入的 Apollo 服务

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 2020-10-24
    • 2020-01-19
    • 2020-12-06
    • 2021-01-15
    • 2021-01-11
    • 2019-12-16
    • 2018-05-15
    相关资源
    最近更新 更多