我正在阅读 udemy 的 GraphQL 教程,
https://www.udemy.com/introduction-to-graphql-and-apollo-building-modern-apis
我正在阅读操作 graphql 和 graphiql -> apollo -express - 服务器的指南。并得到了这个。视频中未定义此特定错误。这是一个免费的教程,第 9 课有这个。怎么办。我找不到解决办法。请帮忙。
TypeError: Cannot read property 'startsWith' of undefined
at Object.renderGraphiQL (/home/dell/Desktop/graphql-
tutorial/node_modules/apollo-server-module-
graphiql/src/renderGraphiQL.ts:48:17)
at Object. (/home/dell/Desktop/graphql-tutorial/node_modules/apollo-
server-module-graphiql/src/resolveGraphiQLString.ts:62:10)
at step (/home/dell/Desktop/graphql-tutorial/node_modules/apollo-
server-module-graphiql/dist/resolveGraphiQLString.js:32:23)
at Object.next (/home/dell/Desktop/graphql-
tutorial/node_modules/apollo-server-module-
graphiql/dist/resolveGraphiQLString.js:13:53)
at fulfilled (/home/dell/Desktop/graphql-tutorial/node_modules/apollo-
server-module-graphiql/dist/resolveGraphiQLString.js:4:58)
at
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
Run Code Online (Sandbox Code Playgroud)
renderGraphiQLString.js
这是它说有错误的行-->
export function renderGraphiQL(data: GraphiQLData): string {
const endpointURL = data.endpointURL;
const endpointWs =
endpointURL.startsWith('ws://') || endpointURL.startsWith('wss://');
const subscriptionsEndpoint = data.subscriptionsEndpoint;
const usingHttp = !endpointWs;
const usingWs = endpointWs …Run Code Online (Sandbox Code Playgroud) 在express-graphql应用程序中,我有一个userLogin解析器,如下所示:
const userLogin = async ({ id, password }), context, info) => {
if (!id) {
throw new Error('No id provided.')
}
if (!password) {
throw new Error('No password provided.')
}
// actual resolver logic here
// …
}
Run Code Online (Sandbox Code Playgroud)
如果用户不提供idAND AND password,则只会抛出一个错误。
{
"errors": [
{
"message": "No id provided.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"userLogin"
]
}
],
"data": {
"userLogin": null
}
}
Run Code Online (Sandbox Code Playgroud)
如何在errors响应数组中引发多个错误?
我一直在研究GraphQL并触发查询以将JSON响应放入react应用程序。
但是我需要触发一个查询,该查询将返回该列的最大值和最小值。
我遍历了GraphQL的文档,但无处找不到实现此目的的方法。
{
schneider(where: {_and: [{device_time: {_gte: "2018-09-04T20:11:42.097+05:30"}}, {device_time: {_lte: "2018-09-04T20:12:43.187+05:30"}}]}) {
active_energy_received
}
}
Run Code Online (Sandbox Code Playgroud)
上面的查询为我提供了两个提供的日期之间的值的列表。
{
"data": {
"schneider": [
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 0
},
{
"active_energy_received": 4.3699998856
},
{
"active_energy_received": 0.82099998
},
{
"active_energy_received": 0.82099998
},
{
"active_energy_received": 4.3699998856
},
{
"active_energy_received": …Run Code Online (Sandbox Code Playgroud) 给定此架构:
interface INode {
id: ID
}
type Todo implements INode {
id: ID
title: String!
}
type Query {
node(id: ID!): INode
}
Run Code Online (Sandbox Code Playgroud)
给定此类:
export default class Todo {
constructor (public id: string, public title: string) { }
isTypeOf(value: any): Boolean {
return value instanceof Todo;
}
}
Run Code Online (Sandbox Code Playgroud)
使用此解析器:
type NodeArgs = {
id: string
}
export const resolver = {
node: ({ id }: NodeArgs) => {
return new Todo('1', 'Todo 1');
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用查询时:
query {
node(id: …Run Code Online (Sandbox Code Playgroud) 鉴于我有一个具有类型的模式:用户、评论、帖子、图像;给定查询和架构,是否可以确定查询中使用的 GraphQL 类型?例如,如果客户有疑问
{
user(userName: "username") {
email
comments
}
}
Run Code Online (Sandbox Code Playgroud)
在本例中,查询具有 User 和 Comment 类型。是否可以使用 node.js 的 graphql-js 或 graphql 包以编程方式确定?
我正在按照教程进行操作,并使用express-graphql. 我有一个突变来创建一个Friend对象和一个查询来返回相同的对象。自动完成和模式检测在 localhost:8080/graphql 上运行的 GraphiQL 上运行良好。我尝试在 SO 上搜索类似的错误,但这些错误要么有复杂的示例,要么正在使用 Apollo。
这是我尝试执行的 GraphQL 突变:
mutation {
createFriend(input: {
firstName:"FName1",
lastName:"LName1",
email:"test@test.com"
}) {
id
}
}
Run Code Online (Sandbox Code Playgroud)
和我看到的错误(仅显示错误的相关部分):
{
"errors": [
{
"message": "Cannot read property 'input' of undefined",
// Erased irrelevant portion of error
}
Run Code Online (Sandbox Code Playgroud)
这是我的 schema.js 文件,它导入resolvers并resolvers.js使用makeExecutableSchema:
import { resolvers } from './resolvers';
import { makeExecutableSchema } from '@graphql-tools/schema';
const typeDefs = `
type Friend {
id: ID
firstName: String
lastName: …Run Code Online (Sandbox Code Playgroud) 因此,我尝试在 MongoDB 中创建一个 User 集合,并使用 GraphQL 和 mongoose 对其进行查询。
我已经在路径“pathToServer\server\models\user.js”中创建了用户架构,它看起来像这样:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {
firstName: String,
lastName: String,
},
email: String,
password: String,
})
module.exports = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
我创建了一个 GraphQL 类型,目前我将它放在路径“pathToServer\server\schema\types\user.js”中,它看起来像这样:
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLList, GraphQLInt, GraphQLID, GraphQLString, GraphQLSchema, GraphQLNonNull} = graphql;
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: new GraphQLObjectType({
firstName: {type: GraphQLString},
lastName: …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) 我正在实施 graphql 登录突变来验证用户登录凭据。Mutation 使用 bcrypt 验证密码,然后将 cookie 发送到客户端,客户端将根据 cookie 是买家还是所有者用户来呈现用户配置文件。
GraphQL 登录突变代码:
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
loginUser: {
type: UserType,
args: {
email: { type: GraphQLString },
password: { type: GraphQLString }
},
resolve: function (parent, args, { req, res }) {
User.findOne({ email: args.email }, (err, user) => {
if (user) {
bcrypt.compare(args.password, user.password).then(isMatch => {
if (isMatch) {
if (!user.owner) {
res.cookie('cookie', "buyer", { maxAge: 900000, httpOnly: false, path: '/' });
} else …Run Code Online (Sandbox Code Playgroud) 在我将资源部分添加到代码中之前,这段代码一直有效。它与其他两个相似,所以我不确定为什么不起作用。
提前致谢
更新:-使用调试器后,我开始了解到问题出在RootQueryType.js和与之相关的文件中,因为当我尝试导出架构时,错误地弹出,并且恰好在query:RootQueryType位置,但仍然可以不能指出错误。
更新:-我也将schema.js文件放在最后
resourceType.js
const graphql = require("graphql");
const UserType = require("./userType");
const User = require("../models/User");
const Project = require("../models/Project");
const Resource = require("../models/Resource");
const ProjectType = require("./projectType");
const {
GraphQLObjectType,
GraphQLString,
GraphQLList,
} = graphql;
const ResourceType = new GraphQLObjectType({
name: "ResourceType",
fields: () => ({
id: { type: GraphQLString },
title: { type: GraphQLString },
url: { type: GraphQLString },
project:{
type:ProjectType,
resolve(parentValues,args){
return Project.findById(parentValues.id).populate("resources")
}
}
})
});
module.exports=ResourceType;
Run Code Online (Sandbox Code Playgroud)
RootQueryType.js
const mongoose = require('mongoose');
const graphql …Run Code Online (Sandbox Code Playgroud) express-graphql ×10
graphql ×9
graphql-js ×3
javascript ×3
node.js ×3
express ×1
graphiql ×1
hasura ×1
mongoose ×1
reactjs ×1
typescript ×1