试图创建我的第一个graphQL服务器,这是我到目前为止所写的内容.
https://gist.github.com/tharakabimal/7f2947e805e69f67af2b633268db0406
当我尝试按用户名过滤用户时,GraphQL上会弹出以下错误.
UserQueriesQL.js中的users字段中发生错误.
我在解析函数上传递参数的方式有什么不对吗?
user: {
type: UserType,
args: {
username: {
name: 'username',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: function(parentValue, args) {
return User.find( args ).exec();
}
Run Code Online (Sandbox Code Playgroud) 我试图返回一个身份验证令牌,以响应使用用户名和密码作为参数的graphql查询,但不知何故graphql总是返回null.我可以在返回之前打印令牌.
var {
GraphQLObjectType,
GraphQLInt,
GraphQLList,
GraphQLString,
GraphQLSchema
} = require('graphql');
let MyUser = require('./modules/user/user');
const Query = new GraphQLObjectType({
name: 'Query',
description : 'UserQuery',
fields: function(){
return {
Token :{
type: GraphQLString,
description : "Authentication token",
args: {
user_name: { type: GraphQLString },
password: { type: GraphQLString },
},
resolve(root, args){
let user = new MyUser();
user.authenticateUser(args.user_name, args.password, function(result){
console.log("token :"+result);
return result;
})
}
}
}
}
});
const Schema = new GraphQLSchema({
query: Query
}); …Run Code Online (Sandbox Code Playgroud) 我有一个 GraphQL 驱动的应用程序。查询和变异部分运行良好。我尝试添加 GraphQL 订阅。
服务器 GraphQL 订阅部分代码的灵感来自apollographql/subscriptions-transport-ws的自述文件中的演示。
另请检查代码中的注释以获取更多详细信息。
import Koa from 'koa';
import Router from 'koa-router';
import graphqlHTTP from 'koa-graphql';
import asyncify from 'callback-to-async-iterator';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import firebase from 'firebase-admin';
import { execute, subscribe } from 'graphql';
import { GraphQLObjectType, GraphQLString } from 'graphql';
const MeType = new GraphQLObjectType({
name: 'Me',
fields: () => ({
name: { type: GraphQLString },
// ...
}),
});
const listenMe = async (callback) => {
// Below …Run Code Online (Sandbox Code Playgroud) 我在服务器端尝试(Apollo)GraphQL并且一直有一个可能很愚蠢的问题.我正在尝试注册用户,但不断收到下面链接图片中显示的错误.问题是什么?忽略非常简单的auth流程,因为我只是测试GraphQl
以下是相关的代码段:
架构
export default `
type User {
id: ID!
name: String!
email: String!
}
type Query {
allUsers: [User]
currentUser: User
}
type Mutation {
createAccount(name: String!, email: String!, password: String!): User
loginUser(email: String!, password: String!): User
updatePassword(email: String!, password: String!, newPassword: String!): User
deleteAccount(email: String!, password: String!): User
}
`
Run Code Online (Sandbox Code Playgroud)
解析器
createAccount: async (
parent,
{ name, email, password },
{ User },
info
) => {
try {
// Check for invalid (undefined) credentials
if (!name …Run Code Online (Sandbox Code Playgroud) 我正在使用 GraphQL 连接到多个 RESTful 端点。我正在编写一个更改来更新用户详细信息,但是 GraphiQL 显示以下响应为 null 而不是更新的用户 ID 和名称...
{
"data": {
"updateUser": null
}
}
Run Code Online (Sandbox Code Playgroud)
updateUser: (parent, args) => {
const { id, name } = args;
return fetch(`${url}/users/${id}`, {
method: 'PUT',
body: JSON.stringify({ id, name }),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));
},
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我有这个 schema.graphql
### This file was generated by Nexus Schema
### Do not make changes to this file directly
type AuthPayload {
token: String!
users: users!
}
scalar DateTime
type Mutation {
login(email: String, password: String): AuthPayload!
signup(CREATED_BY: String, EMAIL: String, FIRST_NAME: String, IS_ACTIVE: Boolean, PASSWORD: String, USERNAME: String): AuthPayload!
}
type Query {
me: users
}
type users {
CREATED_BY: String!
CREATED_ON: DateTime
EMAIL: String!
FIRST_NAME: String!
id: Int!
IS_ACTIVE: Boolean!
LAST_NAME: String
MODIFIED_BY: String
MODIFIED_ON: DateTime
ORGANIZATION_ID: String …Run Code Online (Sandbox Code Playgroud) 我有一个待办事项列表,我已经成功地添加了新项目并更新了缓存,现在正在努力添加删除突变。我的最终目标是从删除突变中返回一个 id,该 id 可用于更新客户端的整个待办事项列表。
我尝试将项目的 id 传递给突变,但它返回 null。
图式
type Todo {
_id: ID!
todo: String!
}
type RootMutation {
deleteTodo(_id: ID!): Todo
}
Run Code Online (Sandbox Code Playgroud)
解析器
deleteTodo: async function({ _id }, req) {
return await Todo.deleteOne({ _id });
}
Run Code Online (Sandbox Code Playgroud)
使用以下代码在http://localhost:3000/graphql 的graphql 接口中测试了此突变:
mutation {
deleteTodo (_id: "example0101010101001010101aasdsadasd"){
_id
}
}
Run Code Online (Sandbox Code Playgroud)
出现此错误
{
"errors": [
{
"message": "Cannot return null for non-nullable field Todo._id.",
"locations": [
{
"line": 3,
"column": 3
}
],
"path": [
"deleteTodo",
"_id"
]
}
],
"data": …Run Code Online (Sandbox Code Playgroud) 我试图解决研究代码中的一个错误,但失败了。然后我只是尝试启动此代码...
https://github.com/nestjs/nest/tree/master/sample/23-type-graphql
和同样的情况...
错误看起来像
{
"errors": [
{
"message": "Cannot return null for non-nullable field Recipe.id.",
"locations": [
{
"line": 3,
"column": 5
}
],
"path": [
"recipe",
"id"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Cannot return null for non-nullable field Recipe.id.",
" at completeValue (/home/innistry/Downloads/nest-master/sample/23-type-graphql/node_modules/graphql/execution/execute.js:560:13)",
" at /home/innistry/Downloads/nest-master/sample/23-type-graphql/node_modules/graphql/execution/execute.js:492:16",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
}
}
],
"data": null
}
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?
graphql ×8
javascript ×6
node.js ×5
express ×2
graphql-js ×2
apollo ×1
koa ×1
nestjs ×1
prisma ×1
rest ×1
typescript ×1