Graphql 初体验 第十六章 | #18 Bookings

对应内容 #18 Bookings | Build a Complete App with GraphQL, Node.js, MongoDB and React.js

主要内容:

  • 在events列表中完成订阅功能
  • 完成booking.js 渲染订阅的活动的列表
  • 学习了如何在后端给预检请求设置安全时间(如何不要每次都发预检请求?)

1 在events列表中完成订阅功能

bookEventHandler = () => {
	if (!this.context.token) {
	    this.setState({selectedEvent: null});
	    return;
	}
	const queryBody = {
	    query: `
	        mutation {
	            bookEvent(eventId: "${this.state.selectedEvent._id}") {
	                _id
	                createdAt
	                updatedAt
	            }
	        }
	    `
	}
	fetch("http://localhost:3030/graphql", {
	    method: "POST",
	    headers: {
	        "Content-Type": "application/json",
	        "Authorization": "Bearer " + this.context.token
	    },
	    body: JSON.stringify(queryBody),
	})
	.then(response => {
	    if (response.status !== 200 && response.status !== 201) {
	        throw new Error("request failed!")
	    }
	    this.setState({
	        isModalShow: false
	    })
	    return response.json();
	})
	.then(data => {
	    console.log(data.data);
	    this.setState({selectedEvent: null});
	})
	.catch(err => {
	    throw err;
	});
};

2 完成booking.js 渲染订阅的活动的列表

import React, { Component } from 'react';
import AuthContext from '../context/auth-context';
import Snipper from '../components/Snipper/Snipper';

class BookingsPage extends Component {
    state = {
        isLoading: false,
        bookings: []
    }

    static contextType = AuthContext;

    componentDidMount () {
        this.fetchBookings();
    }

    fetchBookings = () => {
        if (!this.context.token) {
            return;
        }
        this.setState({
            isLoading: true
        });
        const queryBody = {
            query: `
                query {
                    bookings {
                        _id
                        event {
                            _id
                            title
                        }
                        user {
                            _id
                        }
                        createdAt
                        updatedAt
                    }
                }
            `
        }
        fetch("http://localhost:3030/graphql", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + this.context.token
            },
            body: JSON.stringify(queryBody),
        })
        .then((response) => {
            if (response.status !== 200 && response.status !== 201) {
                throw new Error("request failed!");
            }
            return response.json();
        })
        .then((data) => {
            this.setState({
                bookings: data.data.bookings
            })
        })
        .catch((err) => {
            throw err;
        })
        .then(() => {
            this.setState({
                isLoading: false
            });
        }, err => {
            this.setState({
                isLoading: false
            });
            throw err;
        });
    };

    render() {
        return (
            <React.Fragment>
                <h1>The booking Page</h1>
                {
                    this.state.isLoading ? <Snipper /> :
                    <ul className="eventList">
                    {
                        this.state.bookings.map(booking => {
                            return <li className="eventlist__item" key={booking._id}>{booking.event.title} - {booking.createdAt}</li>
                        })
                    }
                    </ul>
                }
            </React.Fragment>
        );
    }
}

export default BookingsPage;

3 如何不要每次都发预检请求?

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值