-
Notifications
You must be signed in to change notification settings - Fork 18
/
blogPart2.txt
151 lines (134 loc) · 6.41 KB
/
blogPart2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
This is a second part of reactjs apollo-client front end in which as backend we have used golang gqlgen graphql server
Here also I am assuming readers know reactjs little bit.
For this demo again preferred path is
~/go/src/golang-gqlgen-reactjs-subscription-demo/reactjs
Step 1 :
you can start react project with default create-react-app
First of all we need to connect our front-end with back end
So we have used ApolloClient for connection to golang backend
const wsLink = new WebSocketLink({
uri: environment.WSHost,
options: {
reconnect: true
}
});
const apolloClient = new ApolloClient({
link: wsLink,
cache: new InMemoryCache(),
});
function render(component) {
ReactDOM.render(<ApolloProvider client={apolloClient}>
{component}
</ApolloProvider>, document.getElementById('root'));
}
render(<App />);
Step 2:
Create component for CURD operation of channel
for that we will create file reactjs/src/components/ChannelList.jsx
Okay so we will try to understand devide this file in few section to understand easily
1) Import section we need to insert react-apollo and graphql-tag for call graphql Query and Mutation
import React, { Component } from 'react';
import { graphql, Mutation, compose } from 'react-apollo';
import gql from 'graphql-tag';
2) Render of react component
class ChannelsList extends Component {
componentWillMount() {
// Add subscribeToMore method for watch subscription of add channel
this.props.data.subscribeToMore({
document: addChannelSubscription, // Use the subscription
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) {
return prev;
}
const newChannel = subscriptionData.data.subscriptionChannelAdded;
// Add check to prevent double adding of channels.
if (!prev.channels.find((channel) => channel.id === newChannel.id)) {
let updatedChannels = Object.assign({}, prev, { channels: [...prev.channels, newChannel] });
return updatedChannels;
} else {
return prev;
}
}
});
}
render() {
// we get data of channels on page render from props
const { data: { loading, error, channels } } = this.props;
if (loading) {
return <p>Loading ...</p>;
}
if (error) {
return <p>{error.message}</p>;
}
return (
<div className="container">
<div className="row justify-content-md-center">
<div className="col-md-8">
<center><h3 className="main-title">Channel List</h3></center>
<hr />
<div className="input-group mb-3">
<input
type="text"
className="form-control"
placeholder="Enter Channel"
onKeyUp={this.handleKeyUp}
value={this.state.nameValue}
onChange={this.onChangeFunc} />
</div>
<ul className="list-group">
{channels.map(ch =>
<li key={"div_" + ch.id} className="list-group-item">
<label htmlFor="checkbox5">
{ch.id} : {ch.name}
</label>
<div className="pull-right action-buttons">
<a onClick={() => this.onEditClick(ch)} href="javascript:void(0)"><span className="fa fa-pencil"></span></a>
<!-- Use Mutation for direct run deleteChannel mutation-->
<Mutation mutation={deleteChannel} >
{(deleteChannelMutation, { data }) => (
<a className="trash" href="javascript:void(0)" onClick={() => { deleteChannelMutation({ variables: { id: ch.id } }); }}><span className="fa fa-trash"></span></a>
)}
</Mutation>
</div>
</li>
)}
</ul>
</div>
</div>
</div>
)
}
}
3) Now Write Query ,Mutation and Subscription for CRUD channel
// query for get all channel list
export const channelsListQuery = gql`
query ChannelsListQuery {
channels {
id
name
}
}
`;
//Mutation for add channel
const CreateChannelMutation = gql`
mutation addChannel($name: String!) {
addChannel(name: $name) {
id
name
}
}
`;
//Subscription for newly added channel
const addChannelSubscription = gql`
subscription Channels {
subscriptionChannelAdded {
id
name
}
}
`
//export class with graphql query and mutation is very import for get data on component load
const multipleMutation = compose(
graphql(CreateChannelMutation, { name: 'createChannelMutation' })
)
export default compose(multipleMutation, graphql(channelsListQuery))(ChannelsList);