Home > Back-end >  GraphQL mutation type thows error when try to set type
GraphQL mutation type thows error when try to set type

Time:07-16

I got this error: "Mutation fields must be an object with field names as keys or a function which returns such an object." I try to add user but I don't know how to make this work.

const mutation = new GraphQLObjectType({
    name: "Mutation",
    fileds: {
        addUser: {
            type: UserType,
            args: {
                firstName: { type: new GraphQLNonNull(GraphQLString) },
                age: { type: new GraphQLNonNull(GraphQLInt) },
                companyId: { type: GraphQLString }
            },
            resolve(parentValue, { firstName, age }) {
                return axios.post("http://localhost:3000/users", { firstName, age }).then(r => r.data);
            }
        }
    }
})

UserType is defined as follows:

const UserType = new GraphQLObjectType({
    name: "User",
    fields: {
        id: { type: GraphQLString },
        firstName: { type: GraphQLString },
        age: { type: GraphQLInt },
        company: {
            type: CompanyType,
            resolve(parentValue, args) {
                return axios.get("http://localhost:3000/companies/"   parentValue.companyId).then(res => res.data)
            }
        }
    }
})

CodePudding user response:

You have a typo: "fileds" should be "fields".

  • Related