-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graffle): extension system (#871)
- Loading branch information
1 parent
9d81d14
commit 6eebe6f
Showing
31 changed files
with
1,821 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +0,0 @@ | ||
import type { ExecutionResult, GraphQLSchema } from 'graphql' | ||
import { execute } from './execute.js' | ||
import type { URLInput } from './request.js' | ||
import { request } from './request.js' | ||
import type { BaseInput } from './types.js' | ||
|
||
export type SchemaInput = URLInput | GraphQLSchema | ||
|
||
export interface Input extends BaseInput { | ||
schema: SchemaInput | ||
} | ||
|
||
export const requestOrExecute = async ( | ||
input: Input, | ||
): Promise<ExecutionResult> => { | ||
const { schema, ...baseInput } = input | ||
|
||
if (schema instanceof URL || typeof schema === `string`) { | ||
return await request({ url: schema, ...baseInput }) | ||
} | ||
|
||
return await execute({ schema, ...baseInput }) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { db } from '../../../tests/_/db.js' | ||
import { Graffle } from '../../../tests/_/schema/generated/__.js' | ||
import * as Schema from '../../../tests/_/schema/schema.js' | ||
|
||
const graffle = Graffle.create({ schema: Schema.schema }) | ||
|
||
// dprint-ignore | ||
describe(`query`, () => { | ||
test(`success`, async () => { | ||
await expect(graffle.query.$batch({ id: true })).resolves.toMatchObject({ id:db.id }) | ||
}) | ||
test(`error`, async () => { | ||
await expect(graffle.query.$batch({ error: true })).rejects.toMatchObject(db.errorAggregate) | ||
}) | ||
describe(`orThrow`, () => { | ||
test(`success`, async () => { | ||
await expect(graffle.query.$batchOrThrow({ id: true })).resolves.toMatchObject({ id:db.id }) | ||
}) | ||
test(`error`, async () => { | ||
await expect(graffle.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* eslint-disable */ | ||
import { ExecutionResult } from 'graphql' | ||
import { describe, expect } from 'vitest' | ||
import { db } from '../../../tests/_/db.js' | ||
import { createResponse, test } from '../../../tests/_/helpers.js' | ||
import { Graffle } from '../../../tests/_/schema/generated/__.js' | ||
import { GraphQLExecutionResult } from '../../legacy/lib/graphql.js' | ||
|
||
const client = Graffle.create({ schema: 'https://foo', returnMode: 'dataAndErrors' }) | ||
const headers = { 'x-foo': 'bar' } | ||
|
||
// todo each extension added should copy, not mutate the client | ||
|
||
describe(`entrypoint request`, () => { | ||
test(`can add header to request`, async ({ fetch }) => { | ||
fetch.mockImplementationOnce(async (input: Request) => { | ||
expect(input.headers.get('x-foo')).toEqual(headers['x-foo']) | ||
return createResponse({ data: { id: db.id } }) | ||
}) | ||
const client2 = client.extend(async ({ pack }) => { | ||
// todo should be raw input types but rather resolved | ||
// todo should be URL instance? | ||
// todo these input type tests should be moved down to Anyware | ||
// expectTypeOf(exchange).toEqualTypeOf<NetworkRequestHook>() | ||
// expect(exchange.input).toEqual({ url: 'https://foo', document: `query { id \n }` }) | ||
return await pack({ ...pack.input, headers }) | ||
}) | ||
expect(await client2.query.id()).toEqual(db.id) | ||
}) | ||
test('can chain into exchange', async ({ fetch }) => { | ||
fetch.mockImplementationOnce(async () => { | ||
return createResponse({ data: { id: db.id } }) | ||
}) | ||
const client2 = client.extend(async ({ pack }) => { | ||
const { exchange } = await pack({ ...pack.input, headers }) | ||
return await exchange(exchange.input) | ||
}) | ||
expect(await client2.query.id()).toEqual(db.id) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.