gpt4 book ai didi

javascript - GraphQL/中继架构 "Cannot query field..."

转载 作者:行者123 更新时间:2023-11-29 18:02:23 35 4
gpt4 key购买 nike

我在查询 graphql/relay 中的某些字段时遇到问题。我正在尝试在下面的架构中查询类(class)中的“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
});

这是我的 database.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,
};

当我尝试查询时:

{ user (id:1){courses{courseName}} }

它给我错误:

"Cannot query field 'courseName' in 'CourseConnection'"

看来是连接的问题。我也在用 cURL 测试这个查询。因为我想在移动到 react /中继之前测试架构。

我错过了什么?谢谢。

最佳答案

所以,我发现了为什么这不起作用。我必须以 RelayJS 样式进行查询:

所以:

{ user(id:1){courses(first:2){edges{node{courseName}}}} }

关于javascript - GraphQL/中继架构 "Cannot query field...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994007/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com