{"size": 2141, "ext": "js", "max_stars_repo_path": "src/graphql/index.js", "max_stars_repo_name": "imsamtar/hasurafire", "max_stars_repo_stars_event_min_datetime": "2020-04-28T05:23:38.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-11T20:36:43.000Z", "max_issues_repo_path": "src/graphql/index.js", "max_issues_repo_name": "imsamtar/hasurafire", "max_issues_repo_issues_event_min_datetime": "2020-06-23T15:16:00.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-20T04:43:27.000Z", "max_forks_repo_path": "src/graphql/index.js", "max_forks_repo_name": "imsamtar/hasurafire", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 6, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 36.2881355932, "max_line_length": 84, "alphanum_fraction": 0.5903783279}
import { get } from "svelte/store";
import gql from "graphql-tag";
import client from "./client";
import { queries, global } from "../store.js";

function stringQuery(query, type) {
    if (typeof query == "string") {
        query &&
            query.indexOf(" ") === -1 &&
            query.indexOf("{") === -1 &&
            (query = get(queries)[query] || query);
        query = query.replace(/^\s*(query|mutation|subscription)?\s*/g, `${type} `);
    }
    return query;
}

function parseQuery(query, type) {
    if (typeof query == "string")
        return gql(stringQuery(query, type));
    return query;
}

function vars(query, vars) {
    let variables = {};
    if (typeof query === "string") {
        query = stringQuery(query);
        Array.from(query.matchAll(/\$(\w+)\s*:\s*\w+!?\s*[,\)]/g))
            .forEach(match => {
                if (typeof get(global)[match[1]] !== "undefined")
                    variables[match[1]] = get(global)[match[1]];
            });
    }
    return { ...variables, ...vars };
}

export const query = (query, variables = {}, options = {}) => {
    /* Fall back to global variable (if any),
     * if a variable not passed,
     * only works with string form of query (not with graphql-tag) */
    variables = vars(query, variables);
    /* execute this query */
    return client.query(parseQuery(query, "query"), variables, options)
};
export const mutate = (mutation, variables = {}, options = {}) => {
    /* Fall back to global variable (if any),
     * if a variable not passed,
     * only works with string form of mutation (not with graphql-tag) */
    variables = vars(mutation, variables);
    /* execute this mutation */
    return client.mutate(parseQuery(mutation, "mutation"), variables, options)
};
export const subscribe = (query, variables = {}, options = {}) => {
    /* Fall back to global variable (if any),
     * if a variable not passed,
     * only works with string form of query (not with graphql-tag) */
    variables = vars(query, variables);
    /* execute this query */
    return client.subscribe(parseQuery(query, "subscription"), variables, options)
};