I'm pretty new to typescript/graphql world and I am receiving a strange error when I am defining the type of one of my root nodes. The root node I am implementing simply returns a user by ID in the resolve function and therefore, I am populating the 'type' key with UserType I've created. However, I get an error from typescript indicating
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>;
~~~~~~
The expected type comes from property 'fields' which is declared here on type 'Readonly<GraphQLObjectTypeConfig<any, any>>'
rootQuery.ts file:
import mongoose from "mongoose";
import { GraphQLObjectType, GraphQLString } from "graphql";
const UserType = "./user_type.ts";
const User = mongoose.model("user");
const RootQueryType = new GraphQLObjectType({
name: "RootQueryType",
fields: () => ({
getUserById: {
type: UserType,
args: {
id: { type: GraphQLString },
},
async resolve(_, { id }) {
try {
return await User.findById(id);
} catch (err) {
console.error(err.message);
}
},
},
}),
});
module.exports = RootQueryType;
user_type.ts file:
import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql'
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
name: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
language: { type: GraphQLString },
topics: { type: new GraphQLList(GraphQLString)}
})
})
module.exports = UserType;
The error is pointing at the "fields" key in the RootQueryType. When I replace the value of the 'type' key in the 'getUserById' node with 'GraphQLString', the errors goes away. But clearly that's not what I want since the returning value will be referenced to the UserType.
Thanks for your time.
I found that it was the way I was importing/requiring the graphql package in my rootQuery.ts file. Instead of importing, I changed it to:
const graphql = require('graphql')
const { GraphQLObjectType, GraphQLString } = graphql;
But I am not understanding why. How does require compare to import so much that it causes errors?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.