I'm encountering an issue when trying to query specific fields in graphql/relay. My goal is to retrieve the "courseName" field for Courses from the schema provided below.
For instance, if I query the User with:
{ user(id:1){firstname,lastname}}
The response is accurate and successful.
Below is my schema setup:
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';
/**
* The node interface and field are obtained from the Relay library.
*
* The first method resolves an ID into its object form.
* The second method defines how to transform an object into 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;
}
}
);
// Other parts of the schema definition...
export var Schema = new GraphQLSchema({
query: queryType,
});
This is a snippet from my 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
};
However, when attempting to execute this query:
{ user (id:1){courses{courseName}} }
An error occurs:
"Cannot query field 'courseName' in 'CourseConnection'"
This seems to be related to the connection aspect of the schema. Additionally, I am testing these queries via cURL to validate the schema prior to integrating with react/relay.
What could possibly be causing this issue? Thank you.