【发布时间】:2021-02-16 13:20:12
【问题描述】:
我正在尝试使用 React 和 Hasura GraphQL 创建一个非常简单的 3 页日程管理应用程序。我将所有已安排的会议显示为一页,我将会议的标题、日期和参与者人数作为用户的输入,并将用户发送到下一页以输入会议的开始和结束时间。但是,当我尝试将输入的值写入数据库时,我收到一条错误消息:
variable startTime of type String! is used in position expecting time
和
variable date of type String! is used in position expecting Date
我不确定日期和时间是否是架构中的预期数据类型,我可以在编写查询时使用。我还附上了两个输入页面的代码,让您了解我现在做错了什么。
这是我的第一个输入页面:
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import Form from 'react-bootstrap/Form'
import Button from 'react-bootstrap/Button';
import { Link } from "react-router-dom";
import { withRouter } from 'react-router-dom'
class NewSchedulePageFirst extends React.Component {
constructor(props) {
super(props);
this.state= {
title: "",
date: "",
participants: ""
}
this.handleChange=this.handleChange.bind(this);
}
handleChange(event) {
const {name, value} = event.target
this.setState({
[name]: value
})
}
render() {
return (
<div className="form">
<div className="form-title">
<h2>Enter details for new entry </h2>
</div>
<div className="form-fields">
<Form>
<div className="form-title">
<Form.Control type="text" placeholder="Title" onChange={this.handleChange} value={this.state.title} name="title" />
</div>
<div className="form-date">
<Form.Control type="date" onChange={this.handleChange} value={this.state.date} name="date" />
</div>
<div className="form-participants">
<Form.Control type="number" placeholder="No. of participants" onChange={this.handleChange} value={this.state.participants} name="participants" />
</div>
</ Form>
</div>
<Link to={{
pathname: "/NewSchedulePageTwo",
state : {
title: this.state.title,
date: this.state.date,
participants: this.state.participants
}
}}>
<Button variant="outline-primary"> Next </Button>
</Link>
</div>
);
}
}
export default withRouter(NewSchedulePageFirst) ;
这是我尝试将数据写入数据库的第二页:
import React, { useState } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import Form from 'react-bootstrap/Form'
import Button from 'react-bootstrap/Button';
import { GET_MY_SCHEDULE } from './currentscheduledisplay'
import { Link , withRouter } from "react-router-dom";
import { gql } from "@apollo/client";
import { useMutation } from "@apollo/client";
const ADD_SCHEDULE = gql `
mutation($title: String!, $date: String!, $startTime: String!, $endTime: String!, $participants: Int!) {
insert_Schedule(objects: {title: $title, date: $date, startTime: $startTime, endTime: $endTime, participants: $participants }) {
affected_rows
returning {
id
title
date
startTime
endTime
participants
}
}
}
`;
function NewSchedulePageSecond(props) {
// console.log(props.location.state);
const { title, date, participants } = props.location.state;
const [ stitle, setStitle ] = useState(title);
const [ sdate, setSdate ] = useState(date);
const [ sparticipants, setSparticipants ] = useState(participants);
const [ startTime, setStartTime ] = useState('');
const [ endTime, setEndTime ] = useState('');
// handleChange(event) {
// const {name, value} = event.target
// this.setState({
// [name]: value
// })
// }
// scheduleInput(event) {
// const { value } = event.target;
// this.setState({schedule : value});
// }
// componentDidMount() {
// }
const resetInput = () => {
setStitle(title);
setSdate(date);
setSparticipants(participants);
setStartTime('');
setEndTime('');
}
const updateCache = (cache, {data}) => {
// Fetch the todos from the cache
const existingSchedule = cache.readQuery({
query: GET_MY_SCHEDULE
});
// Add the new todo to the cache
const newSchedule = data.insert_todos.returning[0];
cache.writeQuery({
query: GET_MY_SCHEDULE,
data: {schedule: [newSchedule, ...existingSchedule.schedule]}
});
};
const [addSchedule] = useMutation(ADD_SCHEDULE, { update: updateCache, onCompleted: resetInput });
return (
<div className="form">
<div className="form-title">
<h2>Enter details for new entry </h2>
</div>
<div className="form-fields">
<Form>
<div className="form-start-time">
<Form.Control type="time" onChange={(e) => setStartTime(e.target.value)} value={startTime} name="startTime" />
</div>
<div className="form-end-time">
<Form.Control type="time" onChange={(e) => setEndTime(e.target.value)} value={endTime} name="endTime" />
</div>
</ Form>
</div>
<Link to='/'>
<Button variant="outline-primary" onClick={() => {addSchedule(
{variables: {title: stitle,
date: sdate,
participants: sparticipants,
startTime: startTime,
endTime: endTime
}})}}>
Create
</Button>
</Link>
{/* <p>
Title: {title}
</p>
<p>
Date: {date}
</p> */}
</div>
);
}
export default withRouter(NewSchedulePageSecond);
我的关系的架构如下:
id - integer, primary key, unique, default: nextval('"Schedule_id_seq"'::regclass)
title - text, default: 'Meeting'::text
participants - integer, default: 2
startTime - time without time zone
endTime - time without time zone
date - text, default: 'No Date Entered'::text
我无法找到表示日期和时间的方法。我是第一次使用 GraphQl,所以如果你能解释为什么我也会收到这个错误,那就太好了。
编辑:我的突变如下:
mutation($title: String!, $date: String!, $startTime: String!, $endTime: String!, $participants: Int!) {
insert_Schedule(objects: {title: $title, date: $date, startTime: $startTime, endTime: $endTime, participants: $participants }) {
affected_rows
returning {
id
title
date
startTime
endTime
participants
}
}
}
【问题讨论】:
-
'insert_Schedule' 变异 server 签名?参数类型?
-
@xadm 这些是我的表 'Schedule' 中的属性:- id - 整数、主键、唯一,默认值:nextval('"Schedule_id_seq"'::regclass) title - 文本,默认值: 'Meeting'::text 参与者 - 整数,默认:2 startTime - 不带时区的时间 endTime - 不带时区的时间 date - 文本,默认:'No Date Entered'::text
-
我不关心 DB .... 在 graphiql/playground 中测试您的查询 ... docs explorer 再次显示类型 defs ...,这个突变 def 和参数类型的外观如何 - 自定义日期和时间标量...传递参数[格式]匹配这些标量要求
-
@xadm 这正是我正在努力解决的问题,如果您能更具体地说明我应该做什么来解决这个问题,那就太好了。我只是在学习 GraohQL,所以如果你能解释显而易见的事情,那就太好了。
-
检查 SERVER 突变规范,您的 CLIENT 突变必须与 SERVER defs/arg 类型匹配...您可以在 graphiql/playground 文档中检查...或自定义类型 defs 和突变定义输入和返回类型,显示这个,而不是你怎么称呼它,是早知道的
标签: reactjs api graphql webhooks hasura