python - 如何使用 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
我能做些什么?
解决方案
好的,有几件事可以帮助您:
- 如果您使用的是 Chrome 并转到开发工具,请转到网络 -> 预览。这应该显示一些标记,其中包含有关错误的更多信息。
- 我不确定您的 graphql 突变文件是什么样的,但您可能应该将其导入为
import mutation from ../gql/mutation
. 然后调用它this.get('apollo').mutate({mutation, credentials}, 'createUser')
- 确保你的 graphql 文件的相对路径是正确的
- 我建议您扩展您的路线
ComponentQueryManager
,然后删除注入的 Apollo 服务
推荐阅读
- javascript - jquery datatable 没有显示任何 json 数组数据的记录
- php - 更新 Session 对象项 Laravel
- vb.net - 我不知道为什么我的嵌套 for next 循环只经过一个循环
- mysql - 在 SQL 查询中设置日期格式
- jquery - 如何从控制器响应中填充 HTML 视图
- c# - ReaderWriterLockSlim.EnterReadLock 在 try 块之前,有什么原因吗?
- tomcat - 从 Tomcat 的日志中排除某些请求
- json - 如何使用 Kotlin 从 Retrofit 获取 Json 数据?(安卓工作室)
- c# - WPF CollectionView/DataGrid 获取第一个可见项的索引
- javascript - React-Native 上的 RTCPeerConnection 配置