我正在尝试创建新公告的表单,但出现错误:
Invariant Violation: Argument of undefined passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document
这是我第一次使用GraphQL和Apollo,所以我不知道该怎么做。
创建公告组件:
import { compose, graphql } from 'react-apollo';
import { addAnnouncement as addAnnouncementMutation } from '../../../mutations/Announcements.gql';
import { announcement as announcementQuery } from '../../../queries/Announcements.gql';
class CreateAnnouncement extends React.Component {
...
}
export default compose(
graphql(announcementQuery, {
options: ({ match }) => ({
variables: {
_id: match.params._id,
},
}),
}),
***THE ERROR POINTS NEXT LINE***
graphql(addAnnouncementMutation, {
name: 'addAnnouncement',
}),
)(CreateAnnouncement);
'../../../ mutations / Announcements.gql'
#import "../fragments/Announcements.gql"
mutation addAnnouncement($title: String!, $description: String, $date: String!) {
addAnnouncement(title: $title, description: $description, date: $date) {
...AnnouncementAttributes
}
}
“ ../ fragments / Announcements.gql”
fragment AnnouncementAttributes on Announcement {
_id
title
description
date
createdAt
updatedAt
}
答案 0 :(得分:0)
我解决了这个问题。原因是文件中只有一个“添加”突变。
我添加了一个新的突变,并且有效。
#import "../fragments/Announcements.gql"
mutation addAnnouncement($title: String!, $description: String, $date: String!) {
addAnnouncement(title: $title, description: $description, date: $date) {
...AnnouncementAttributes
}
}
mutation updateAnnouncement($_id: String!, $title: String, $description: String, $date: String) {
updateAnnouncement(_id: $_id, title: $title, description: $description, date: $date) {
...AnnouncementAttributes
}
}
您还需要在解析器和模式中定义此突变。