GraphQL/中继模式”;无法查询字段&”;
GraphQL/Relay Schema "Cannot query field..."
我在查询graphql/rerelay中的一些字段时遇到问题。我试图在下面的模式中查询课程上的"courseName"字段。
如果我查询用户,例如:
{user(id:1){firstname,lastname}}
它工作并返回正确的响应。
这是我的模式:
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from 'graphql';
import {
connectionArgs,
connectionDefinitions,
connectionFromArray,
fromGlobalId,
globalIdField,
mutationWithClientMutationId,
nodeDefinitions,
} from 'graphql-relay';
import {
// Import methods that your schema can use to interact with your database
User,
Course,
getUser,
getCourses
} from './database';
/**
* We get the node interface and field from the Relay library.
*
* The first method defines the way we resolve an ID to its object.
* The second defines the way we resolve an object to its GraphQL type.
*/
var {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId);
if (type === 'User') {
return getUser(id);
} else if (type === 'Course') {
return getCourses(id);
} else {
return null;
}
},
(obj) => {
if (obj instanceof User) {
return userType;
} else if (obj instanceof Course) {
return courseType;
} else {
return null;
}
}
);
/**
* Define your own types here
*/
var userType = new GraphQLObjectType({
name: 'User',
description: 'A person who uses our app',
fields: () => ({
id: globalIdField('User'),
firstname : {
type : GraphQLString,
description : 'User First Name'
},
lastname :{
type : GraphQLString,
description : 'User Last Name'
},
email : {
type : GraphQLString,
description : 'User email'
},
company : {
type : GraphQLString,
description : 'User company'
},
courses: {
type: courseConnection,
description: 'A user''s collection of courses',
// args: {
// userId: {
// type: GraphQLInt
// }
// },
// resolve: (user, args) => connectionFromArray(getCourses(args.userId), args),
args: connectionArgs,
resolve: (user, args) => connectionFromArray(
getCourses(user.id),
args
)
},
}),
interfaces: [nodeInterface],
});
var courseType = new GraphQLObjectType({
name: 'Course',
description: 'A shiny course',
fields: () => ({
id: globalIdField('Course'),
courseName: {
type: GraphQLString,
description: 'The name of the Course',
}
}),
interfaces: [nodeInterface],
});
/**
* Define your own connection types here
*/
var {connectionType: courseConnection} =
connectionDefinitions({name: 'Course', nodeType: courseType});
/**
* This is the type that will be the root of our query,
* and the entry point into our schema.
*/
var queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
// Add your own root fields here
user: {
type: userType,
args: {
id: { type: GraphQLInt }
},
resolve: (_,{id}) => getUser(id)
}
})
});
/**
* This is the type that will be the root of our mutations,
* and the entry point into performing writes in our schema.
*/
var mutationType = new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
// Add your own mutations here
})
});
/**
* Finally, we construct our schema (whose starting query type is the query
* type we defined above) and export it.
*/
export var Schema = new GraphQLSchema({
query: queryType,
// Uncomment the following after adding some mutation fields:
// mutation: mutationType
});
这是我的数据库.js:
import Course from './models/course';
import User from './models/user';
module.exports = {
User : User,
// Export methods that your schema can use to interact with your database
getUser: (id) => {
return User.findOne( {_id: id}).exec();
},
getCourses: (userId) => {
return Course.find( { userId : userId }).exec();
},
Course : Course
// getCourse: (id) => widgets.find(w => w.id === id),
// User,
// Widget,
};
当我尝试查询:
{用户(id:1){课程{课程名称}}}}
它给了我一个错误:
"无法查询'CourseConnection'中的字段'courseName'"
似乎是连接有问题。我还在用cURL测试这个查询。因为我想在转到react/rerelay之前测试模式。
我错过了什么?非常感谢。
所以,我发现了为什么这不起作用。我不得不用RelayJS风格进行查询:
所以:
{ user(id:1){courses(first:2){edges{node{courseName}}}} }
相关文章:
- 如何从查询字符串中的输入字段发回文本
- 如何在 Meteor 中的 Mongo 查询中使用变量作为字段名称
- j查询检查复选框是否被选中,然后向输入字段添加值
- dojo.查询所需字段
- Mongoose,按填充字段对查询进行排序
- SQL查询-替换字段名称中的句点
- j查询验证单独的年、月、日的日期字段
- j查询各种字段添加,带有JSFiddle的可选复选框
- 对填充字段的 MongoDB 查询
- 隐藏字段值未显示在查询字符串中
- 查询MongoDB中不存在的字段
- 在控制台.log中显示查询的 [对象对象] 的字段
- j查询输入字段格式为 XX-XX-XX
- j查询添加必填项到输入字段
- 在MongoDB中动态查询字段
- GraphQL/中继模式”;无法查询字段&”;
- 如何在 where 子句中使用动态查询字段
- Mongodb查询字段的对象类型
- 传递嵌套文档查询字段
- 无法推断查询字段以在插入时设置错误