Skip to content

Commit

Permalink
Add useGETForQueries option for URL Loader to fetch queries using HTT…
Browse files Browse the repository at this point in the history
…P GET
  • Loading branch information
ardatan committed May 27, 2020
1 parent 82c9ffc commit bd17df5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/loaders/url/src/index.ts
@@ -1,4 +1,4 @@
import { print, IntrospectionOptions, DocumentNode } from 'graphql';
import { print, IntrospectionOptions, DocumentNode, GraphQLResolveInfo } from 'graphql';
import {
SchemaPointerSingle,
Source,
Expand All @@ -23,6 +23,7 @@ export interface LoadFromUrlOptions extends SingleFileOptions, Partial<Introspec
method?: 'GET' | 'POST';
enableSubscriptions?: boolean;
webSocketImpl?: typeof w3cwebsocket | string;
useGETForQueries?: boolean;
}

export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
Expand All @@ -41,7 +42,7 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
async load(pointer: SchemaPointerSingle, options: LoadFromUrlOptions): Promise<Source> {
let headers = {};
let fetch = crossFetch;
let method: 'GET' | 'POST' = 'POST';
let defaultMethod: 'GET' | 'POST' = 'POST';
let webSocketImpl = w3cwebsocket;

if (options) {
Expand All @@ -68,7 +69,7 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {
}

if (options.method) {
method = options.method;
defaultMethod = options.method;
}
}

Expand All @@ -80,7 +81,19 @@ export class UrlLoader implements DocumentLoader<LoadFromUrlOptions> {

const HTTP_URL = pointer.replace('ws:', 'http:').replace('wss:', 'https:');

const executor = async ({ document, variables }: { document: DocumentNode; variables: any }) => {
const executor = async ({
document,
variables,
info,
}: {
document: DocumentNode;
variables: any;
info: GraphQLResolveInfo;
}) => {
let method = defaultMethod;
if (options.useGETForQueries && info.operation.operation === 'query') {
method = 'GET';
}
const fetchResult = await fetch(HTTP_URL, {
method,
...(method === 'POST'
Expand Down

0 comments on commit bd17df5

Please sign in to comment.