|
@@ -11,39 +11,131 @@ const history = createBrowserHistory()
|
|
|
//console.log(createQueryPub)
|
|
|
//
|
|
|
|
|
|
-const {queryPub, createQuery} = createQueryPub({
|
|
|
- queryFunc(route, id=''){
|
|
|
- return fetch(`https://swapi.dev/api/${route}/${id}`).then(res => res.json())
|
|
|
- },
|
|
|
- cacheTimeout: 5000,
|
|
|
+
|
|
|
+//
|
|
|
+//
|
|
|
+const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2NjdmZjBkYTIyMzIxMjBjN2M4NzFmN2MiLCJsb2dpbiI6InRzdDE5IiwiYWNsIjpbIjY2N2ZmMGRhMjIzMjEyMGM3Yzg3MWY3YyIsInVzZXIiXX0sImlhdCI6MTcyMDI1OTEwN30.eDcInxQlskt32LjaCe5sdCcJ3B97BKYZtgUARxThYQM'
|
|
|
+
|
|
|
+console.log(JSON.parse(atob(token.split('.')[1])).sub.id)
|
|
|
+
|
|
|
+const gql = (query, variables={}) =>
|
|
|
+ fetch('http://shop-roles.node.ed.asmer.org.ua/graphql',{
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ Accept: 'application/json',
|
|
|
+ Authorization: 'Bearer ' + token
|
|
|
+ },
|
|
|
+ body: JSON.stringify({query, variables})
|
|
|
+ }).then(res => res.json())
|
|
|
+
|
|
|
+const {queryPub, createQuery, withQueryFunc} = createQueryPub({
|
|
|
+ queryFunc: gql,
|
|
|
+ cacheTimeout: 500000,
|
|
|
//hidePendingOnRefresh: false,
|
|
|
})
|
|
|
|
|
|
-const {usePeopleQuery} = createQuery({promiseName: 'people',
|
|
|
- cacheTagFunc: (payload) => payload.results.map(man => ({type: 'people', id: +man.url.split('/')[5]}))})
|
|
|
-
|
|
|
-//queryPub.subscribe(() => console.log(JSON.stringify(queryPub, null, 4)))
|
|
|
|
|
|
+//queryPub.subscribe(() => console.log(JSON.stringify(queryPub.queries.user, null, 4)))
|
|
|
+
|
|
|
+const {useGoodsQuery} = createQuery({promiseName: 'goods',
|
|
|
+ cacheTagFunc: (payload) => payload.data.GoodFind.map(good => ({type: 'Good', id: good._id}))})
|
|
|
+
|
|
|
+//const {useUserQuery} = createQuery({
|
|
|
+ //promiseName: 'user',
|
|
|
+ //args: [`
|
|
|
+ //query user($query: String) {
|
|
|
+ //UserFindOne(query: $query){
|
|
|
+ //_id login nick
|
|
|
+ //}
|
|
|
+ //}
|
|
|
+ //`],
|
|
|
+ //cacheTagFunc: ({data: {UserFindOne: {_id}}}) => ({_id, type: 'User'})
|
|
|
+//})
|
|
|
+//const userState = useUserQuery({query: JSON.stringify([{_id: '667ff0da2232120c7c871f7c'}])})
|
|
|
+
|
|
|
+//const {useUserQuery} = withQueryFunc(_id => gql(
|
|
|
+ //`query user($query: String) {
|
|
|
+ //UserFindOne(query: $query){
|
|
|
+ //_id login nick
|
|
|
+ //}
|
|
|
+ //}`,
|
|
|
+ //{query: JSON.stringify([{_id}])}
|
|
|
+//))({
|
|
|
+ //promiseName: 'user',
|
|
|
+ //cacheTagFunc: ({data: {UserFindOne: {_id}}}) => ({_id, type: 'User'})
|
|
|
+//})
|
|
|
+
|
|
|
+
|
|
|
+const {useUserQuery} = createQuery({
|
|
|
+ promiseName: 'user',
|
|
|
+ args: (_id) => [`
|
|
|
+ query user($query: String) {
|
|
|
+ UserFindOne(query: $query){
|
|
|
+ _id login nick
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `, {query: JSON.stringify([{_id}])}],
|
|
|
+ cacheTagFunc: ({data: {UserFindOne: {_id}}}) => [{_id, type: 'User'}]
|
|
|
+})
|
|
|
|
|
|
+const {useUserMutation} = createQuery({
|
|
|
+ promiseName: 'user',
|
|
|
+ isMutation: true,
|
|
|
+ args: (_id, nick) => [
|
|
|
+ `mutation changeNick($_id:String, $nick:String){
|
|
|
+ UserUpsert(user:{_id:$_id, nick:$nick}){
|
|
|
+ _id nick
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ {_id, nick}],
|
|
|
+ cacheTagFunc: ({data: {UserUpsert: {_id}}}) => [{_id, type: 'User'}]
|
|
|
+})
|
|
|
|
|
|
+const EditNick = () => {
|
|
|
+ const [nick, setNick] = useState('')
|
|
|
+ const [update, {status, payload, error}] = useUserMutation()
|
|
|
+ console.log(status, payload, error)
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <input type='text' value={nick} onChange={e => setNick(e.target.value)} />
|
|
|
+ <button onClick={() => update('667ff0da2232120c7c871f7c', nick)}>Save</button>
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+}
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
- const queryState = usePeopleQuery('people')
|
|
|
+ //const queryState = useGoodsQuery(`query {
|
|
|
+ //GoodFind(query: "[{}]"){
|
|
|
+ //name price _id
|
|
|
+ //}
|
|
|
+ //}`)
|
|
|
+
|
|
|
+
|
|
|
+ const userState = useUserQuery('667ff0da2232120c7c871f7c')
|
|
|
+ console.log(userState)
|
|
|
+
|
|
|
const [,setRandomState] = useState<number>()
|
|
|
|
|
|
useEffect(() => {
|
|
|
//setInterval(() => setRandomState(Math.random()), 1000)
|
|
|
},[])
|
|
|
|
|
|
- console.log(queryState)
|
|
|
+ //console.log(queryState)
|
|
|
|
|
|
- const {status, payload,cacheTags} = queryState
|
|
|
+ //const {status, payload,cacheTags} = queryState
|
|
|
//console.log(JSON.stringify(cacheTags))
|
|
|
return (
|
|
|
<Router history={history}>
|
|
|
- {status === 'PENDING' && <h1>Loading</h1>}
|
|
|
- {status === 'FULFILLED' && payload.results.map(man => <h2 key={man.url}>{man.name}</h2>)}
|
|
|
+ <EditNick />
|
|
|
+ {userState.status === 'PENDING' && <h1>Loading</h1>}
|
|
|
+ {userState.status === 'FULFILLED' && userState.payload.data.UserFindOne.nick}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ {/*status === 'PENDING' && <h1>Loading</h1>}
|
|
|
+ {status === 'FULFILLED' && payload.data.GoodFind.map(good => <h2 key={good._id}>{good.name}</h2>) */}
|
|
|
|
|
|
|
|
|
</Router>
|