Skip to content

Commit

Permalink
feat: allow document nodes to be used programatically
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 14, 2019
1 parent d088907 commit f01f08f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ export const Mutation = (Vue as withVqlClient).extend({
throw new Error('Could not find the VQL client, did you install the plugin correctly?');
}

const query = normalizeQuery(this.query);
if (!query) {
throw new Error('A query must be provided.');
}

try {
this.data = null;
this.errors = null;
this.fetching = true;
this.done = false;
const { data, errors } = await this.$vql.query({
query,
query: this.query,
variables: vars || undefined
});

Expand Down
7 changes: 1 addition & 6 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,10 @@ export const Query = (Vue as withVqlClient).extend({
throw new Error('Could not detect Client Provider');
}

const query = normalizeQuery(this.query);
if (!query) {
throw new Error('A query must be provided.');
}

try {
this.fetching = true;
const { data, errors } = await this.$vql.query({
query,
query: this.query,
variables: normalizeVariables(this.variables, vars || {}),
cachePolicy: cachePolicy || (this.cachePolicy as CachePolicy)
});
Expand Down
8 changes: 7 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { makeCache } from './cache';
import { OperationResult, CachePolicy, Operation, ObservableLike } from './types';
import { normalizeQuery } from './utils';

type Fetcher = typeof fetch;

Expand Down Expand Up @@ -34,9 +35,14 @@ function resolveGlobalFetch(): Fetcher | undefined {
}

function makeFetchOptions({ query, variables }: Operation, opts: FetchOptions) {
const normalizedQuery = normalizeQuery(query);
if (!normalizedQuery) {
throw new Error('A query must be provided.');
}

return {
method: 'POST',
body: JSON.stringify({ query, variables }),
body: JSON.stringify({ query: normalizedQuery, variables }),
...opts,
headers: {
'content-type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DocumentNode } from 'graphql';

export interface OperationResult {
data: any;
errors: any;
Expand All @@ -6,7 +8,7 @@ export interface OperationResult {
export type CachePolicy = 'cache-and-network' | 'network-only' | 'cache-first';

export interface Operation {
query: string;
query: string | DocumentNode;
variables?: { [k: string]: any };
cachePolicy?: CachePolicy;
}
Expand Down

0 comments on commit f01f08f

Please sign in to comment.