Skip to content

Commit

Permalink
fix(gatsby-source-graphql): Convert ts to plain js until better times (
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar authored Apr 6, 2020
1 parent 22b3033 commit ad945ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { parse } from "graphql"
import { execute } from "apollo-link"
import { createDataloaderLink } from "../dataloader-link"
const { parse } = require(`graphql`)
const { execute } = require(`apollo-link`)
const { createDataloaderLink } = require(`../dataloader-link`)

const sampleQuery = parse(`{ foo }`)
const expectedSampleQueryResult = { data: { foo: `bar` } }

// eslint-disable-next-line @typescript-eslint/camelcase
const fetchResult = { data: { gatsby0_foo: `bar` } }

const makeFetch = (expectedResult: any = fetchResult): jest.Mock<any> =>
const makeFetch = (expectedResult = fetchResult) =>
jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve(expectedResult),
Expand All @@ -23,7 +22,7 @@ describe(`createDataloaderLink`, () => {
})
const observable = execute(link, { query: sampleQuery })
observable.subscribe({
next: (result: any) => {
next: result => {
expect(result).toEqual(expectedSampleQueryResult)
done()
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { print, parse } from "graphql"
import { IQuery, merge, resolveResult } from "../merge-queries"
const { print, parse } = require(`graphql`)
const { merge, resolveResult } = require(`../merge-queries`)

describe(`Query merging`, () => {
it(`merges simple queries`, () => {
Expand Down Expand Up @@ -213,7 +213,7 @@ describe(`Resolving merged query results`, () => {
})

it(`throws on unexpected results`, () => {
const shouldThrow = (): void => {
const shouldThrow = () => {
resolveResult({
data: {
gatsby0_foo: `foo`,
Expand All @@ -225,10 +225,8 @@ describe(`Resolving merged query results`, () => {
})
})

type QueryFixture = [string, object]

function fromFixtures(fixtures: QueryFixture[]): IQuery[] {
return fixtures.map(([query, variables]: QueryFixture) => {
function fromFixtures(fixtures) {
return fixtures.map(([query, variables]) => {
return {
query: parse(query),
variables,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import DataLoader from "dataloader"
import { ApolloLink, Observable, Operation, FetchResult } from "apollo-link"
import { print } from "graphql"
import { IQuery, IQueryResult, merge, resolveResult } from "./merge-queries"
const DataLoader = require(`dataloader`)
const { ApolloLink, Observable } = require(`apollo-link`)
const { print } = require(`graphql`)
const { merge, resolveResult } = require(`./merge-queries`)

interface IOptions {
uri: string
fetch: Function
fetchOptions?: object
dataLoaderOptions?: object
headers?: object
}

export function createDataloaderLink(options: IOptions): ApolloLink {
const load = async (keys: ReadonlyArray<IQuery>): Promise<IQueryResult[]> => {
export function createDataloaderLink(options) {
const load = async keys => {
const query = merge(keys)
const result: object = await request(query, options)
const result = await request(query, options)
if (!isValidGraphQLResult(result)) {
const error: any = new Error(
const error = new Error(
`Failed to load query batch:\n${formatErrors(result)}`
)
error.name = `GraphQLError`
Expand All @@ -34,12 +26,12 @@ export function createDataloaderLink(options: IOptions): ApolloLink {
const dataloader = new DataLoader(load, {
cache: false,
maxBatchSize,
batchScheduleFn: (callback): any => setTimeout(callback, 50),
batchScheduleFn: callback => setTimeout(callback, 50),
...options.dataLoaderOptions,
})

return new ApolloLink(
(operation: Operation): Observable<FetchResult> =>
operation =>
new Observable(observer => {
const { query, variables } = operation

Expand All @@ -61,7 +53,7 @@ export function createDataloaderLink(options: IOptions): ApolloLink {
)
}

function formatErrors(result: any): string {
function formatErrors(result) {
if (result?.errors?.length > 0) {
return result.errors
.map(error => {
Expand All @@ -75,15 +67,15 @@ function formatErrors(result: any): string {
return `Unexpected GraphQL result`
}

function isValidGraphQLResult(response): response is IQueryResult {
function isValidGraphQLResult(response) {
return (
response &&
response.data &&
(!response.errors || response.errors.length === 0)
)
}

async function request(query: IQuery, options: IOptions): Promise<object> {
async function request(query, options) {
const { uri, headers = {}, fetch, fetchOptions } = options

const body = JSON.stringify({
Expand Down
Loading

0 comments on commit ad945ec

Please sign in to comment.