中继风味的graphql变异验证错误

时间:2016-12-29 15:52:48

标签: graphql relay graphql-js

我已经认识了一个简单的接力调味突变,效果很好。我有一个简单的客户端组件,它使用所需的数据提交变异。

我的问题是如何向客户端发回多个错误?目前我可以简单地在mutateAndGetPayload中抛出错误(或拒绝承诺),我将在客户端收到错误,但这目前仅适用于字符串消息。我应该简单地使用错误数组的JSON字符串拒绝承诺吗?或者有更好的方法吗?

const createCashAccountMutation = mutationWithClientMutationId({
    name: 'CreateCashAccount',
    inputFields: {
        name: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'Cash account name'
        },
        code: {
            type: GraphQLString,
            description: 'Optional code'
        },
        businessId: {
            type: new GraphQLNonNull(GraphQLString),
                description: 'Business ID'
        },
        currencyId: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'Currency ID'
        },
        isActive: {
            type: new GraphQLNonNull(GraphQLInt)
        }
    },
    outputFields: {
        name: {
            type: GraphQLString,
            resolve: (payload) => payload.name
        },
        code: {
            type: GraphQLString,
            resolve: (payload) => payload.code
        },
        businessId: {
            type: GraphQLString,
            resolve: (payload) => payload.businessId
        },
        currencyId: {
            type: GraphQLString,
            resolve: (payload) => payload.currencyId
        },
        isActive: {
            type: GraphQLString,
            resolve: (payload) => payload.isActive
        }
    },
    mutateAndGetPayload: async (options) => {
        throw 'wtf';
        return options;
    }
});

更新1。

我提出了以下示例:

const graphQLCashAccount = new GraphQLObjectType({
    name: 'cashAccount',
    fields: {
        name: {
            type: GraphQLString,
            resolve: (payload) => payload.name
        },
        code: {
            type: GraphQLString,
            resolve: (payload) => payload.code
        },
        businessId: {
            type: GraphQLString,
            resolve: (payload) => payload.businessId
        },
        currencyId: {
            type: GraphQLString,
            resolve: (payload) => payload.currencyId
        },
        isActive: {
            type: GraphQLString,
            resolve: (payload) => payload.isActive
        }
    }
});

const graphQLErrors = new GraphQLList(new GraphQLObjectType({
    name: 'errors',
    fields: {
        key: {
            type: GraphQLString,
            resolve: (payload) => payload.key
        },
        message: {
            type: GraphQLString,
            resolve: (payload) => payload.message
        }
    }
}));

const graphQlInput = new GraphQLInputObjectType({
    name: 'data',
    fields: {
        name: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'Cash account name'
        },
        code: {
            type: GraphQLString,
            description: 'Optional code'
        },
        businessId: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'Business ID'
        },
        currencyId: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'Currency ID'
        },
        isActive: {
            type: new GraphQLNonNull(GraphQLInt)
        }
    }
});

const createCashAccountMutation = mutationWithClientMutationId({
    name: 'CreateCashAccount',
    inputFields: {
        data: {
            type: graphQlInput
        }
    },
    outputFields: {
        data: {
            type: graphQLCashAccount,
            resolve: (payload) => payload.data
        },
        errors: {
            type: graphQLErrors,
            resolve: (payload) => payload.errors
        }
    },
    mutateAndGetPayload: async (options) => {
        const payload = {
            errors: [{ key: 'asd', message: 'asd failed' }],
            data: options
        };
        return payload;
    }
});

这实际上将解决事务,只返回2个字段,一个数据字段和一个错误字段。其中一个将填充。

这是一种更好的方法吗?我很难知道如何在Relays fatquery中应用更新。

更新2。

Relay Mutation客户端示例。

export class NewCashAccountMutation extends Relay.Mutation {

    getMutation () {
        return Relay.QL`mutation { 
            createCashAccount
        }`;
    }

    getVariables() {
        return { data: this.props.cashAccount };
    }

    getFatQuery() {
        return Relay.QL`
            fragment on CreateCashAccountPayload {
                data, errors
            }
        `;
    }
    getConfigs() {
        return [{
            type: 'FIELDS_CHANGE',
            fieldIDs: {
                data: this.props.cashAccount.id,
            },
        }];
    }
}

0 个答案:

没有答案