From 20667764cb1bdd7e49c179924a9e5ca49252dd94 Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Sun, 14 Jul 2019 16:44:14 +0200 Subject: [PATCH] fix: ensure programatic access to client is seperated by operation type --- src/Mutation.ts | 4 ++-- src/Query.ts | 4 ++-- src/Subscription.ts | 2 +- src/client.ts | 15 +++++++++++++-- src/types.ts | 1 - 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Mutation.ts b/src/Mutation.ts index 3cc5145..c004fb5 100644 --- a/src/Mutation.ts +++ b/src/Mutation.ts @@ -1,6 +1,6 @@ import Vue, { VueConstructor } from 'vue'; import { VqlClient } from './client'; -import { normalizeQuery, normalizeChildren } from './utils'; +import { normalizeChildren } from './utils'; type withVqlClient = VueConstructor< Vue & { @@ -41,7 +41,7 @@ export const Mutation = (Vue as withVqlClient).extend({ this.errors = null; this.fetching = true; this.done = false; - const { data, errors } = await this.$vql.query({ + const { data, errors } = await this.$vql.executeMutation({ query: this.query, variables: vars || undefined }); diff --git a/src/Query.ts b/src/Query.ts index 04a3feb..872b4d2 100644 --- a/src/Query.ts +++ b/src/Query.ts @@ -2,7 +2,7 @@ import Vue, { VueConstructor } from 'vue'; import stringify from 'fast-json-stable-stringify'; import { CachePolicy } from './types'; import { VqlClient } from './client'; -import { normalizeVariables, normalizeQuery, normalizeChildren, hash } from './utils'; +import { normalizeVariables, normalizeChildren, hash } from './utils'; type withVqlClient = VueConstructor< Vue & { @@ -81,7 +81,7 @@ export const Query = (Vue as withVqlClient).extend({ try { this.fetching = true; - const { data, errors } = await this.$vql.query({ + const { data, errors } = await this.$vql.executeQuery({ query: this.query, variables: normalizeVariables(this.variables, vars || {}), cachePolicy: cachePolicy || (this.cachePolicy as CachePolicy) diff --git a/src/Subscription.ts b/src/Subscription.ts index f6eb405..f1346cf 100644 --- a/src/Subscription.ts +++ b/src/Subscription.ts @@ -47,7 +47,7 @@ export const Subscription = (Vue as withVqlClient).extend({ const self = this; this.$observer = this.$vql - .subscribe({ + .executeSubscription({ query: this.query, variables: this.variables }) diff --git a/src/client.ts b/src/client.ts index e3645c6..09737ab 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6,6 +6,10 @@ type Fetcher = typeof fetch; type FetchOptions = Omit; +interface CachedOperation extends Operation { + cachePolicy?: CachePolicy; +} + interface GraphQLRequestContext { fetchOptions?: FetchOptions; } @@ -76,7 +80,7 @@ export class VqlClient { this.subscriptionForwarder = opts.subscriptionForwarder; } - public async query(operation: Operation): Promise { + public async executeQuery(operation: CachedOperation): Promise { const fetchOptions = this.context ? this.context().fetchOptions : {}; const opts = makeFetchOptions(operation, fetchOptions || {}); const policy = operation.cachePolicy || this.defaultCachePolicy; @@ -105,7 +109,14 @@ export class VqlClient { return lazyFetch(); } - public subscribe(operation: Operation) { + public async executeMutation(operation: Operation): Promise { + const fetchOptions = this.context ? this.context().fetchOptions : {}; + const opts = makeFetchOptions(operation, fetchOptions || {}); + + return this.fetch(this.url, opts).then(response => response.json()); + } + + public executeSubscription(operation: Operation) { if (!this.subscriptionForwarder) { throw new Error('No subscription forwarder was set.'); } diff --git a/src/types.ts b/src/types.ts index 23187b4..219d6c2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,7 +10,6 @@ export type CachePolicy = 'cache-and-network' | 'network-only' | 'cache-first'; export interface Operation { query: string | DocumentNode; variables?: { [k: string]: any }; - cachePolicy?: CachePolicy; } export interface ObserverLike {