首页 > 解决方案 > 如何使用 ember-apollo-client?

问题描述

我正在尝试通过 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

我能做些什么?

标签: pythondjangoember.jsgraphqlapollo-client

解决方案


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

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

推荐阅读