cnpm install apollo-boost @apollo/react-hooks graphql --save
import React from 'react';
import ReactDOM from 'react-dom';
import ApolloClient, {gql} from 'apollo-boost'
const client=new ApolloClient ({
uri:'http://localhost:4000/graphql'
})
client.query({
query:gql`
query{
getCategories{
id,name
}
}
`
}).then(result=>console.log(result))
import React from 'react';
import ReactDOM from 'react-dom';
import ApolloClient, {gql} from 'apollo-boost'
import {ApolloProvider} from '@apollo/react-hooks'
import App from './App'
const client=new ApolloClient ({
uri:'http://localhost:4000/graphql'
})
ReactDOM.render(
<ApolloProvider client = {client}>
<App></App>
</ApolloProvider>,document.getElementById('root')
)
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import ApolloClient from 'apollo-boost'
import {ApolloProvider} from '@apollo/react-hooks'
import App from './App'
import 'antd/dist/antd.css';
const client=new ApolloClient ({
uri:'http://localhost:4000/graphql'
})
ReactDOM.render(
<ApolloProvider client = {client}>
<App></App>
</ApolloProvider>,document.getElementById('root')
)
App.js
import React from 'react';
import { Layout, Menu, Breadcrumb } from 'antd';
import MyContent from './mycontent'
const {Footer,Header,Content} = Layout
export default function (){
return (
<Layout className="layout">
<Header>
<div className="logo" />
<Menu
theme="dark"
mode="horizontal"
defaultSelectedKeys={['2']}
style={{ lineHeight: '64px' }}
>
<Menu.Item key="1">nav 1</Menu.Item>
<Menu.Item key="2">nav 2</Menu.Item>
<Menu.Item key="3">nav 3</Menu.Item>
</Menu>
</Header>
<Content style={{ padding: '0 50px' }}>
<Breadcrumb style={{ margin: '16px 0' }}>
<Breadcrumb.Item>Home</Breadcrumb.Item>
<Breadcrumb.Item>List</Breadcrumb.Item>
<Breadcrumb.Item>App</Breadcrumb.Item>
</Breadcrumb>
<div style={{ background: '#fff', padding: 24, minHeight: 280 }}> <MyContent></MyContent></div>
</Content>
<Footer style={{ textAlign: 'center' }}>Ant Design ©2018 Created by Ant UED</Footer>
</Layout>
)
}
query.js
import {gql} from 'apollo-boost'
export const CATEGORIES_PRODUCT=gql`
query{
getCategories {
id,
name,
products{
id,
name,
}
}
getProducts {
id
name,
category{
id,
name,
products{
id,
name,
}
}
}
}`
export const CATEGORIES = gql`
query{
getCategories {
id
name
}
}
`;
export const PRODUCTS = gql`
query{
getProducts {
id
name,
category{
id,
name
}
}
}`;
export const ADD_PRODUCT = gql`
mutation($name:String!,$category: String!){
addProduct(name: $name,category:$category) {
id,
name,
category{
id,
name
}
}
}`;
export const ADD_CATEGORY = gql`
mutation($name:String!){
addCategory(name:$name) {
id,
name
}
}`;
export const DEL_PRODUCT = gql`
mutation ($name:String!,$categoryId: String!){
delProductByName(name: $name, category: $categoryId) {
id
name
category {
id
name
products {
id
name
}
}
}
}
`
//使用语句修改
const [addcategory]=useMutation(ADD_CATEGORY)
addcategory({variables:values,refetchQueries:[{query:CATEGORIES_PRODUCT}]})
//使用语句查询
const {data}=useQuery(CATEGORIES_PRODUCT);
mycontent.js
import React from 'react'
import { useQuery } from '@apollo/react-hooks';
import {CATEGORIES_PRODUCT} from './query';
import ChangeProduct from './content/changeproduct'
import DisplayProduct from './content/displayproduct'
export default function(){
const {data}=useQuery(CATEGORIES_PRODUCT);
return (<div style={{display:"flex"}}>
<ChangeProduct ></ChangeProduct>
<DisplayProduct getProducts = {data}> </DisplayProduct>
</div>)
}
changeproduct.js
import React from 'react'
import Addcategory from './addcategory';
import Addproduct from './addproduct';
import Deleteproduct from './deleteproduct'
function ChangeProduct(props) {
return (
<div style={{flex:'1'}} >
<Addproduct></Addproduct>
<Addcategory></Addcategory>
<Deleteproduct></Deleteproduct>
</div>
)
}
export default ChangeProduct
addcategory.js
import React from 'react'
import { Form, Input, Button } from 'antd';
import { useMutation } from '@apollo/react-hooks';
import { CATEGORIES_PRODUCT,ADD_CATEGORY} from '../query';
function AddCategory(props){
const formItemLayout ={
labelCol: { span: 4 },
wrapperCol: { span: 14 },
}
const buttonItemLayout ={
wrapperCol: { span: 14, offset: 4 },
}
const { getFieldDecorator } = props.form;
const [addcategory]=useMutation(ADD_CATEGORY)
function categorySubmit(e){
e.preventDefault();
props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
addcategory({variables:values,refetchQueries:[{query:CATEGORIES_PRODUCT}]})
}
});
}
return (
<Form layout="horizontal" onSubmit={categorySubmit}>
<Form.Item label="ADD Category" {...formItemLayout}>
</Form.Item>
<Form.Item label="CATEGORY" {...formItemLayout}>
{getFieldDecorator('name', {
rules: [{ required: true, message: 'input category' }],
})(
<Input placeholder="input category"/>,
)}
</Form.Item>
<Form.Item {...buttonItemLayout}>
<Button type="primary" htmlType="submit" className="login-form-button">submit</Button>
</Form.Item>
</Form>
)
}
const WrappedForm = Form.create({})(AddCategory);
export default WrappedForm