This repository has been archived by the owner on Jan 6, 2025. 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.
WIP: Add
t.effect
on field builder (#40)
* Add `t.effect` types * Add changeset * Add changeset * Update * Changeset
- Loading branch information
Showing
27 changed files
with
1,034 additions
and
698 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,5 @@ | ||
--- | ||
"pothos-plugin-effect": minor | ||
--- | ||
|
||
Add `t.effect` on field builder |
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,7 @@ | ||
--- | ||
"pothos-plugin-effect": minor | ||
"graphql-effect": minor | ||
"effect-utils": minor | ||
--- | ||
|
||
Support Stream |
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,3 +1,4 @@ | ||
export { executeEffect } from './executeEffect.js'; | ||
export { executeStream } from './executeStream.js'; | ||
export { isStream } from './isStream.js'; | ||
export type { InferValueType } from './types.js'; |
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,18 @@ | ||
import { expect, test } from 'vitest'; | ||
import { isStream } from './isStream.js'; | ||
import { Effect, pipe, Stream } from 'effect'; | ||
|
||
test('should returns false if value is Effect', () => { | ||
const result = isStream(Effect.succeed(1)); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test('should returns true if value is Stream', () => { | ||
const result = isStream(Stream.make(1)); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test('should returns false if value is not Effect or Stream ', () => { | ||
const result = isStream({ pipe: () => void 0 }); | ||
expect(result).toBe(false); | ||
}); |
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,12 @@ | ||
import { Effect, Stream } from 'effect'; | ||
|
||
// This is a unsafe way to check if a value is a Stream. | ||
// I hope that the Effect will expose the `Stream.isStream` function. | ||
export const isStream = <A, E, R>( | ||
value: unknown, | ||
): value is Stream.Stream<A, E, R> => { | ||
return ( | ||
Effect.isEffect(value) === false && | ||
Object.getPrototypeOf(value).constructor.name.startsWith('Stream') | ||
); | ||
}; |
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,43 @@ | ||
import { Effect, Stream } from 'effect'; | ||
import { GraphQLFieldResolver } from 'graphql'; | ||
import { expect, test } from 'vitest'; | ||
import { effectResolver } from './effectResolver.js'; | ||
|
||
const fromAsync = async <T>(iterator: AsyncIterable<T>) => { | ||
const result = []; | ||
|
||
for await (const value of iterator) { | ||
result.push(value); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
test('should resolve Effect as a resolver value', async () => { | ||
const resolve: GraphQLFieldResolver<unknown, unknown> = async ( | ||
_source, | ||
_args, | ||
_context, | ||
_info, | ||
) => Effect.succeed(1); | ||
|
||
const result = await effectResolver(resolve)({}, {}, {}, {} as never); | ||
|
||
expect(result).toBe(1); | ||
}); | ||
|
||
test('should resolve Stream as a resolver value', async () => { | ||
const resolve: GraphQLFieldResolver<unknown, unknown> = async ( | ||
_source, | ||
_args, | ||
_context, | ||
_info, | ||
) => Stream.range(1, 5); | ||
|
||
const result = await fromAsync( | ||
// @ts-ignore | ||
await effectResolver(resolve)({}, {}, {}, {} as never), | ||
); | ||
|
||
expect(result).toEqual([1, 2, 3, 4, 5]); | ||
}); |
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 { Effect, Runtime } from 'effect'; | ||
import { executeEffect, executeStream, isStream } from 'effect-utils'; | ||
import type { GraphQLFieldResolver } from 'graphql'; | ||
|
||
export const effectResolver = | ||
( | ||
resolve: GraphQLFieldResolver<any, any>, | ||
runtime = Runtime.defaultRuntime, | ||
): GraphQLFieldResolver<any, any> => | ||
async (source, args, context, info) => { | ||
const result = await resolve(source, args, context, info); | ||
|
||
if (Effect.isEffect(result)) { | ||
// @ts-ignore | ||
return executeEffect(result, runtime); | ||
} | ||
|
||
if (isStream(result)) { | ||
// @ts-ignore | ||
return executeStream(result, runtime); | ||
} | ||
|
||
return result; | ||
}; |
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 +1,2 @@ | ||
export { enableExecuteEffect } from './enableExecuteEffect.js'; | ||
export { effectResolver } from './effectResolver.js'; |
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 |
---|---|---|
|
@@ -50,7 +50,7 @@ | |
"better-sqlite3": "^9.4.0", | ||
"drizzle-kit": "^0.20.14", | ||
"drizzle-orm": "^0.29.3", | ||
"effect": "^3.0.0", | ||
"effect": "^3.1.5", | ||
"graphql": "*", | ||
"nanobundle": "*", | ||
"prisma": "5.9.1", | ||
|
@@ -63,8 +63,7 @@ | |
"graphql": "^16 || ^17" | ||
}, | ||
"dependencies": { | ||
"effect-utils": "workspace:^", | ||
"type-plus": "^7.6.0" | ||
"effect-utils": "workspace:^" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Oops, something went wrong.