サーバー側の検証を可能にするリレー突然変異をコード化しようとしています。その名前のレコードはすでに存在します。サーバーサイド検証によるリレーの突然変異
以下は私のGraphQL変異です。
/* @flow weak */
import {mutationWithClientMutationId,offsetToCursor} from "graphql-relay";
import {GraphQLString, GraphQLList, GraphQLNonNull} from "graphql";
import NominationConnection from '../NominationsConnection';
import ViewerType from '../../../types/ViewerType';
import Helper from '../../../helper/helper';
import Nomination from '../../../model/Nomination';
const mongo = require('mongodb');
/**
* mutationWithClientMutationId()
*
* This helper function helps us create Relay-compliant GraphQL mutations it takes an object with the following params
* { name: "", inputField: { }, outputFields: {}, mutateAndGetPayload: function({}) }
*
*/
export default mutationWithClientMutationId({
// Name of the Mutation
name: "Nomination_Add",
// Describes the fields that should be used when invoking the mutation
inputFields: {
name: {
type: new GraphQLNonNull(GraphQLString)
},
description: {
type: new GraphQLNonNull(GraphQLString)
},
books: {
type: new GraphQLList(GraphQLString)
}
},
// response that will be sent back when the mutation is complete
outputFields: {
NominationsEdge: {
type: NominationConnection.edgeType,
resolve: ({local_id}, { ...args }, context, { rootValue: objectManager }) =>
{
//RT: local_id is the object that was inserted into DB.;
let nomination = local_id[0];
console.log("nomination: ", nomination);
if(nomination.Errors.length > 0){
return objectManager.getListBy('Nomination', nomination, {}, {})
.then((arr) => {
return ({
cursor: null,
node: nomination,
})
})
}else {
let an_Object;
return objectManager.getOneByParam('Nomination', nomination, {_id: nomination.id})
.then((retrieved_Object) => {
an_Object = retrieved_Object;
})
.then(() => objectManager.getListBy('Nomination', an_Object, {}, {}, objectManager.getViewerUserId()))
.then((arr) => {
return ({
cursor: Helper.cursorForObjectInConnection(arr, an_Object, "id"),
node: an_Object,
})
})
;
}
}
},
Viewer: {
type: ViewerType,
resolve: (parent, args, context, {rootValue: objectManager}) => objectManager.getOneById('User', objectManager.getViewerUserId())
}
},
mutateAndGetPayload: ({name, description, books}, context, {rootValue: objectManager}) => {
if(!books){
books = [];
}
return objectManager.add('Nomination', {
name,
description,
books
}, {name: name}).then((local_id) => ({local_id}));
}
});
以下は、私の中継突然変異である。
/* @flow weak */
import Relay from 'react-relay';
export default class Nomination_addMutation extends Relay.Mutation {
// static fragments = {
// Viewer:() => Relay.QL`
// fragment on Viewer {
// id,
// }
// `,
// };
getMutation() {
return Relay.QL`mutation{Nomination_Add}`;
}
getFatQuery() {
return Relay.QL`
fragment on Nomination_AddPayload {
NominationsEdge,
Viewer {
Nominations(first:500){
edges{
node{
id,
Name,
Description
}
}
}
}
}
`;
}
getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'Viewer',
parentID: this.props.Viewer.id,
connectionName: 'Nominations',
edgeName: 'NominationsEdge',
rangeBehaviors: {
// When the ships connection is not under the influence
// of any call, append the ship to the end of the connection
'': 'append',
// Prepend the ship, wherever the connection is sorted by age
// 'orderby(newest)': 'prepend',
},
}];
}
getVariables() {
return {
name: this.props.name,
description: this.props.description,
books: this.props.books,
};
}
getOptimisticResponse() {
return {
Nomination: {
name: this.props.name,
description: this.props.description,
books: this.props.books,
},
Viewer: {
id: this.props.Viewer.id,
},
};
}
}
ここで私はどのように呼びます。
Relay.Store.commitUpdate(
new Nomination_Add({name: fields.name , description: fields.desc ,Viewer:this.props.Viewer}),
{
onSuccess: response => {
console.log(response);
console.log(response.Nomination_Add);
},
}
);
私の応答オブジェクトには、Viewer.idとclientmuationIDのみがあります。私はgraphqlを使ってこの突然変異を呼び出すと、このように見えます。
mutation{
Nomination_Add(input:{name:"test", description:"test", clientMutationId:"2"}){
NominationsEdge{
node{
Name,
Description,
Errors {
Message
}
Books{
Title
}
}
}
}
}
この応答です。
{
"data": {
"Nomination_Add": {
"NominationsEdge": {
"node": {
"Name": "test",
"Description": "test",
"Errors": [
{
"Message": "Nomination Already Exists"
}
],
"Books": []
}
}
}
}
}
サーバーの検証エラーメッセージをリレー経由でクライアントに返信する方法はありますか?
{,…}
data
:
{Nomination_Add: {clientMutationId: "0", Viewer: {id: "00000000-0000-0000-0000-000000000000"}}}
Nomination_Add
:
{clientMutationId: "0", Viewer: {id: "00000000-0000-0000-0000-000000000000"}}
Viewer
:
{id: "00000000-0000-0000-0000-000000000000"}
id
:
"00000000-0000-0000-0000-000000000000"
clientMutationId
:
"0"
'ビューア:{ID: "00000000-0000-0000-0000-000000000000"}' < - あなたがあなたのビューアオブジェクト(GraphQL型ではなく、GraphQL型が関連付けられているオブジェクト)に 'id'を割り当てているかどうかチェックできますか? –
はい、現在、アプリケーションのビルド中に割り当てています。 – jackncoke
graphiqlインターフェイスで次のクエリを実行するとどうなりますか? 'クエリのxyz { ビューア{ ID、(最初:1) ノミネート{ エッジ{ ノード{ ID、 名、 説明 } } } } }' –