Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-source-graphql): Convert ts to plain js until better times #22848

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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