-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ApolloUserProfile.tsx
164 lines (152 loc) · 4.61 KB
/
ApolloUserProfile.tsx
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
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable*/
import React, {useState} from 'react';
import { gql, useQuery, useMutation } from '@apollo/client';
import PeriqlesForm from 'periqles';
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import jsx from 'react-syntax-highlighter/dist/esm/languages/prism/jsx';
import js from 'react-syntax-highlighter/dist/esm/languages/prism/graphql';
import {vscDarkPlus} from 'react-syntax-highlighter/dist/esm/styles/prism';
SyntaxHighlighter.registerLanguage('jsx', jsx);
SyntaxHighlighter.registerLanguage('js', js);
export const USER_DATA = gql`
fragment UserData on DemoUser {
username
password
email
gender
pizzaTopping
age
}
`;
export const GET_USER = gql`
query DemoUser {
demoUser {
...UserData
}
}
${USER_DATA}
`;
export const ADD_USER = gql`
mutation AddUser($input: AddUserInput!){
addUser(input: $input){
username
email
gender
pizzaTopping
age
}
}
`;
const ApolloUserProfile = () => {
const [updated, setUpdate] = useState(false);
const {
data,
loading,
error,
refetch
} = useQuery(GET_USER);
const [
addUser,
respObj
] = useMutation(
ADD_USER,
);
const specifications: PeriqlesSpecifications = {
header: 'Sign Up',
fields: {
gender: {
element: 'radio',
label: 'Gender',
options: [
{label: 'Non-binary', value: 'NON_BINARY'},
{label: 'Male', value: 'MALE'},
{label: 'Female', value: 'FEMALE'},
],
},
pizzaTopping: {
label: 'Favorite pizza topping:',
element: 'select',
options: [
{label: 'Buffalo chicken', value: 'BUFFALO_CHICKEN'},
{label: 'Pepperoni', value: 'PEPPERONI'},
{label: 'Meat lovers', value: 'MEATLOVERS'},
{label: 'Eggplant parmesan', value: 'EGGPLANT_PARM'},
{label: 'Olives', value: 'OLIVES'},
{label: 'Hawaiian', value: 'HAWAIIAN'},
],
},
},
};
const args = {clientMutationId: '0000'};
const onSuccess = (response) => {
refetch(GET_USER);
};
const onFailure = (error) => {
alert(`Problem submitting form: ${error.toString()}`);
};
const renderUser = (demoUser) => {
return (
<div>
<p><label>Username:</label> {demoUser.username}</p>
<p><label>Email:</label> {demoUser.email}</p>
<p><label>Gender:</label> {demoUser.gender}</p>
<p><label>Favorite Pizza Topping:</label> {demoUser.pizzaTopping}</p>
<p><label>Age:</label> {demoUser.age}</p>
</div>
)
}
return (
<div>
<section className="UserProfile">
<PeriqlesForm
mutationName={'AddUser'}
callbacks={{onSuccess, onFailure}}
specifications={specifications}
args={args}
useMutation={addUser}
/>
<main className="UserProfile-main">
<h2>Most Recently Added User</h2>
{loading ? <p>Loading data...</p> : null}
{error ? <p>ERROR: {JSON.stringify(error)}</p> : null}
{data && data.demoUser ? renderUser(data.demoUser): <p>Sign up...</p>}
</main>
</section>
<section className="CodeDemo">
<h1>Apollo Code Examples</h1>
<section className="Codeblocks">
<section className="SchemaCode">
<h3>Mutation Schema</h3>
<SyntaxHighlighter language="js" style={vscDarkPlus} showLineNumbers={true} codeTagProps={{style: {fontSize: "inherit"}}} customStyle={{fontSize: 18}}>
{"mutation AddUser($input: AddUserInput!){\n"+
" addUser(input: $input) {\n"+
" username\n"+
" password\n"+
" email\n"+
" gender\n"+
" pizzaTopping\n"+
" age\n"+
" }\n"+
"}"}
</SyntaxHighlighter>
</section>
<section className="PeriqlesCode">
<h3>PeriqlesForm Tag</h3>
<SyntaxHighlighter language="jsx" style={vscDarkPlus} showLineNumbers={true} codeTagProps={{style: {fontSize: "inherit"}}} customStyle={{fontSize: 18}}>
{"<PeriqlesForm\n"+
" mutationName={'AddUser'}\n"+
" callbacks={{onSuccess, onFailure}}\n"+
" specifications={specifications}\n"+
" args={args}\n"+
" useMutation={addUser}\n"+
"/>\n"+
"\n"+
"\n"}
</SyntaxHighlighter>
</section>
</section>
</section>
</div>
);
};
export default ApolloUserProfile;