This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Selector and selector APIs
- Loading branch information
1 parent
fbb6313
commit d5fb12f
Showing
4 changed files
with
195 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ISelector } from "./Selector"; | ||
|
||
// export const operation = <S extends ISelector<any>>( | ||
// type: "query" | "mutation" | "subscription", | ||
// selector: S | ||
// ) => <T extends Array<Selection>>( | ||
// name: string, | ||
// select: (t: S) => T | ||
// ): Operation<SelectionSet<T>> => | ||
// new Operation(name, type, new SelectionSet(select(selector))); | ||
|
||
// ``` | ||
// // or new Query({ ... }) | ||
// const operation = query({ | ||
// name: 'Example', | ||
// variables: { | ||
// id: t.string, | ||
// }, | ||
// selection: (t, v) => [ | ||
// t.user({ id: v.id }, t => [ | ||
// t.id, | ||
// t.name, | ||
// t.friends(t => [ | ||
// t.id, | ||
// t.firstName, | ||
// ]) | ||
// ]) | ||
// ] | ||
// extensions: [] | ||
// }) | ||
// ``` | ||
|
||
export interface QueryConfig<TQuery> { | ||
name?: string; | ||
variables?: Record<string, unknown /* @todo */>; | ||
selection: ISelector<TQuery>; | ||
// extensions?: Array<Extension> | ||
} | ||
|
||
export class Query<Q> { | ||
// ast?: DocumentNode or OperationDefinitionNode | ||
|
||
constructor(config: QueryConfig<Q>) {} | ||
|
||
// toString() | ||
} |
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,61 @@ | ||
import { | ||
Field, | ||
SelectionSet, | ||
Selection, | ||
Argument, | ||
Primitive, | ||
} from "./Operation"; | ||
|
||
export type ISelector<T> = { | ||
// @todo use Paramaters<T> to support variables | ||
[F in keyof T]: T[F] extends Primitive | ||
? (variables?: Record<string, any>) => Field<F extends string ? F : never> | ||
: <S extends Array<Selection>>( | ||
arg0?: ((selector: ISelector<T[F]>) => S) | Record<string, any> | never, | ||
arg1?: ((selector: ISelector<T[F]>) => S) | never | ||
) => Field<F extends string ? F : never, never, SelectionSet<S>>; | ||
}; | ||
|
||
export class Selector<T> { | ||
constructor(public readonly select: (t: ISelector<T>) => Array<Selection>) {} | ||
|
||
toSelectionSet() { | ||
return new SelectionSet(this.select(selector())); | ||
} | ||
} | ||
|
||
export const selector = <T>(): ISelector<T> => | ||
new Proxy(Object.create(null), { | ||
get(target, field) /*: FieldFn*/ { | ||
return function fieldFn(...args: any[]) { | ||
if (args.length === 0) { | ||
return new Field<any, any, any>(field); | ||
} else if (typeof args[0] === "function") { | ||
return new Field<any, any, any>( | ||
field, | ||
undefined, | ||
new SelectionSet(args[0](selector())) | ||
); | ||
} else if (typeof args[0] === "object") { | ||
if (typeof args[1] === "function") { | ||
return new Field<any, any, any>( | ||
field, | ||
Object.entries(args[0]).map( | ||
([name, value]) => new Argument(name, value) | ||
), | ||
new SelectionSet(args[1](selector())) | ||
); | ||
} else { | ||
return new Field<any, any, any>( | ||
field, | ||
Object.entries(args[0]).map( | ||
([name, value]) => new Argument(name, value) | ||
) | ||
); | ||
} | ||
} | ||
|
||
throw new Error(`Unable to derive field function from arguments.`); | ||
}; | ||
}, | ||
}); |
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,86 @@ | ||
import { print } from "graphql"; | ||
|
||
import { selector, Selector } from "../Selector"; | ||
import { SelectionSet } from "../Operation"; | ||
import { Result } from "../Result"; | ||
|
||
describe("Selector", () => { | ||
describe("type-saftey", () => { | ||
it("supports selecting against an interface", () => { | ||
interface Query { | ||
foo: string; | ||
bar: number; | ||
baz: { | ||
id: string; | ||
}; | ||
} | ||
|
||
const { foo, bar, baz } = selector<Query>(); | ||
|
||
const selection = [foo(), bar(), baz((t) => [t.id()])]; | ||
type ExampleResult = Result<Query, SelectionSet<typeof selection>>; | ||
|
||
const query = print(new SelectionSet(selection).ast); | ||
|
||
expect(query).toMatchInlineSnapshot(` | ||
"{ | ||
foo | ||
bar | ||
baz { | ||
id | ||
} | ||
}" | ||
`); | ||
}); | ||
}); | ||
|
||
describe("class API", () => { | ||
it("supports arbitrarily deep seletions", () => { | ||
const selector = new Selector<any>((t) => [ | ||
t.user({ id: "foo" }, (t) => [t.id()]), | ||
]); | ||
|
||
const query = print(selector.toSelectionSet().ast); | ||
|
||
expect(query).toMatchInlineSnapshot(` | ||
"{ | ||
user(id: \\"foo\\") { | ||
id | ||
} | ||
}" | ||
`); | ||
}); | ||
}); | ||
|
||
describe("function API", () => { | ||
it("supports arbitrarily deep selections", () => { | ||
const { authors, user } = selector<any>(); | ||
|
||
const selection = [ | ||
authors((t) => [t.name(), t.address((t) => [t.country()])]), | ||
|
||
user({ id: "foo" }, (t) => [t.foo((t) => [t.bar((t) => [t.baz()])])]), | ||
]; | ||
|
||
const query = print(new SelectionSet(selection).ast); | ||
|
||
expect(query).toMatchInlineSnapshot(` | ||
"{ | ||
authors { | ||
name | ||
address { | ||
country | ||
} | ||
} | ||
user(id: \\"foo\\") { | ||
foo { | ||
bar { | ||
baz | ||
} | ||
} | ||
} | ||
}" | ||
`); | ||
}); | ||
}); | ||
}); |
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