123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- const gql = url =>
- (query, variables = {}) =>
- fetch(url, {
- method: 'POST',
- headers: {
- "Content-Type": "application/json",
- "Accept": "application/json",
- },
- body: JSON.stringify({ query, variables })
- }).then(res => res.json()).then(data => {
- try {
- if (!data.data && data.errors) {
- throw new SyntaxError(JSON.stringify(Object.values(data.errors[0])[0]));
- } else {
- return Object.values(data.data)[0]
- }
- }
- catch (e) {
- console.log(e);
- }
- });
- const url = 'http://shop-roles.node.ed.asmer.org.ua/graphql'
- const gqlLogin = (login, password) =>
- gql(`query login($login:String, $password: String){
- login(login:$login, password: $password)
- }`, { login, password })
- const gqlRegister = (login, password) => {
- return gql(url, `mutation register($login: String, $password: String){
- UserUpsert(user: {login: $login, password: $password}){
- _id, login, createdAt
- }
- }`, { login, password })
- }
- const gqlCategories = () => {
- return gql(url,
- ` query categories{
- CategoryFind(query:"[{}]"){
- _id name goods {
- _id
- name
- price
- description
- }
- }
- }`
- );
- }
- const categoryIdImagesDescription = (id) => {
- let SearchId = `\\"_id\\":\\"${id}\\"`
- return gql(
- url,
- `
- query categoryIdImagesDescription{
- GoodFind(query:"[{${SearchId}}]"){
- name description
- images{
- _id
- url
- text
- }
- }
- }
- `
- )
- }
- console.log(gqlCategories());
- console.log(categoryIdImagesDescription(""));
- console.log(gqlRegister("noname ", "noname"));
- console.log(gqlLogin("noname ", "noname"));
|