1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /*Запрос на список корневых категорий
- Используем CategoryFind, однако в параметре query используем поиск по полю parent, которое должно быть равно null. (у корневых категорий нет родителя)*/
- {
- let findCategory = `query baseCategory($searchNullparent: String){
- CategoryFind(query: $searchNullparent){
- _id name parent {
- _id
- name
- }
- }
- }`
- let variables = {
- searchNullparent: JSON.stringify([{ parent: null }])
- }
- const gql = getGql('http://shop-roles.node.ed.asmer.org.ua/graphql')
- gql(findCategory, variables).then(console.log)
- }
- // getGql - полностью готова
- {
- function getGql(endpoint) {
- let headers = {
- 'Content-Type': 'application/json;charset=utf-8',
- 'Accept': 'application/json',
- }
- if ('authToken' in localStorage) {
- headers.Authorization = 'Bearer ' + localStorage.authToken
- }
- return async function gql(query, variables = {}) {
- let result = await fetch(endpoint, {
- method: 'POST',
- headers,
- body: JSON.stringify({
- query,
- variables
- })
- }).then(res => res.json())
- if (('errors' in result) && !('data' in result)) {
- throw new Error(JSON.stringify(result.errors))
- }
- result = Object.values(result.data)[0]
- return result
- }
- }
- }
- // localStoredReducer
- {
- function localStoredReducer(originalReducer, localStorageKey) {
- function wrapper(state, action) {
- if (!state) {
- try {
- return JSON.parse(localStorage[localStorageKey])
- }
- catch (error) {
- }
- }
- const newState = originalReducer(state, action)
- localStorage[localStoredReducer] = JSON.stringify(newState)
- return newState
- }
- return wrapper
- }
- }
|