Skip to content

Commit

Permalink
fix: ensure programatic access to client is seperated by operation type
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 14, 2019
1 parent 9b587f5 commit 2066776
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Mutation.ts
Original file line number Diff line number Diff line change
@@ -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 & {
Expand Down Expand Up @@ -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
});
Expand Down
4 changes: 2 additions & 2 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
15 changes: 13 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type Fetcher = typeof fetch;

type FetchOptions = Omit<RequestInit, 'body'>;

interface CachedOperation extends Operation {
cachePolicy?: CachePolicy;
}

interface GraphQLRequestContext {
fetchOptions?: FetchOptions;
}
Expand Down Expand Up @@ -76,7 +80,7 @@ export class VqlClient {
this.subscriptionForwarder = opts.subscriptionForwarder;
}

public async query(operation: Operation): Promise<OperationResult> {
public async executeQuery(operation: CachedOperation): Promise<OperationResult> {
const fetchOptions = this.context ? this.context().fetchOptions : {};
const opts = makeFetchOptions(operation, fetchOptions || {});
const policy = operation.cachePolicy || this.defaultCachePolicy;
Expand Down Expand Up @@ -105,7 +109,14 @@ export class VqlClient {
return lazyFetch();
}

public subscribe(operation: Operation) {
public async executeMutation(operation: Operation): Promise<OperationResult> {
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.');
}
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down

0 comments on commit 2066776

Please sign in to comment.