簡體   English   中英

嘗試設置類型時,GraphQL 突變類型出現錯誤

[英]GraphQL mutation type thows error when try to set type

我收到了這個錯誤:“變異字段必須是一個以字段名作為鍵的對象,或者是一個返回此類對象的函數。” 我嘗試添加用戶,但我不知道如何使這項工作。

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 定義如下:

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)
            }
        }
    }
})

你有一個錯字:“fileds”應該是“fields”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM