graphql-svelte
graphql fetcher alternate apollo client
Last updated 2 months ago by timeshift92 .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install graphql-svelte 
SYNC missed versions from official npm registry.

graphql-svelte

Alternate apollo-graphql for Svelte and other native js

setup

$ npm i -d graphql-svelte <br>
$ yarn add --dev graphql-svelte

how to use for vanilla js

import { GraphQL } from "graphql-svelte";
import { graphqlFetchOptions, hashObject, SubscribeQL } from "graphql-svelte";
const graphql = new GraphQL();

const fetchOptionsOverride = options => {
(options.url = "http://localhost:4000/graphql"),
(options.headers = {"content-type": "application/json"});
};

//if you need subscribtion

const sub = new SubscribeQL('wss://locahost:4000/v1/graphql', {
  reconnect: true,
  lazy: true,
  connectionParams: async () => {
    return {
      headers: {
        "x-hasura-admin-secret": "secret"
      }
    };
  }
})

let subscribe = (query) => {
  return sub.request(query);
}

//if you nedd query and mutate

let get = async (query, variables) => {
  const fetchOptions = graphqlFetchOptions({
    query,
    variables
  });
  fetchOptionsOverride(fetchOptions)
  const has = hashObject(fetchOptions);
  if (graphql.cache[has])
    return new Promise((resolve, reject) => {
      resolve(graphql.cache[has]);
    });
  const pending = graphql.operate({
    fetchOptionsOverride,
    operation: {
      query,
      variables
    }
  });
  return pending.cacheValuePromise
}


export { get, subscribe };

how to use for svelte

--- graphql.js



import { GraphQLProvider, reportCacheErrors } from "graphql-svelte";

const client = GraphQLProvider({
    url: 'http://localhost:8082/v1/graphql',
    headers: () => ({
        "content-type": "application/json",
        Accept: 'application/json',
        "x-hasura-admin-secret": "secret"
    }),
    ws: {
        url: 'ws://localhost:8082/v1/graphql'
    }
})
client.graphql.on('cache', reportCacheErrors)



export default client

--- api.js


import { queries } from "../queries"
import client from './graphql'

export async function get(query, variables = null) {
  return await client.get({ query: queries[query], variables })
}

export function query(query, variables = null) {
  return client.query({ query: queries[query], variables })
}

export async function mutate(mutation, variables = null) {
  return await client.mutate( { query: queries[mutation], variables })
}

export function subscription(query,  variables = null ) {
  return client.subscribe( { query: queries[query], variables })
}

export function restore(query, data) {

 return client.restore(query, data)
}

Current Tags

  • 1.1.9                                ...           latest (2 months ago)

35 Versions

  • 1.1.9                                ...           2 months ago
  • 1.1.8                                ...           2 months ago
  • 1.1.7                                ...           2 months ago
  • 1.1.6                                ...           2 months ago
  • 1.1.5                                ...           2 months ago
  • 1.1.4                                ...           2 months ago
  • 1.1.3                                ...           2 months ago
  • 1.1.1                                ...           2 months ago
  • 1.1.0                                ...           2 months ago
  • 1.0.25                                ...           2 months ago
  • 1.0.24                                ...           3 months ago
  • 1.0.23                                ...           3 months ago
  • 1.0.22                                ...           3 months ago
  • 1.0.21                                ...           3 months ago
  • 1.0.20                                ...           3 months ago
  • 1.0.19                                ...           3 months ago
  • 1.0.18                                ...           4 months ago
  • 1.0.17                                ...           4 months ago
  • 1.0.16                                ...           4 months ago
  • 1.0.15                                ...           4 months ago
  • 1.0.14                                ...           4 months ago
  • 1.0.13                                ...           4 months ago
  • 1.0.12                                ...           4 months ago
  • 1.0.11                                ...           4 months ago
  • 1.0.10                                ...           4 months ago
  • 1.0.9                                ...           4 months ago
  • 1.0.8                                ...           4 months ago
  • 1.0.7                                ...           4 months ago
  • 1.0.6                                ...           4 months ago
  • 1.0.5                                ...           4 months ago
  • 1.0.4                                ...           4 months ago
  • 1.0.3                                ...           4 months ago
  • 1.0.2                                ...           4 months ago
  • 1.0.1                                ...           4 months ago
  • 1.0.0                                ...           4 months ago
Maintainers (1)
Downloads
Today 0
This Week 35
This Month 35
Last Day 0
Last Week 0
Last Month 7
Dependencies (11)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |