-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
287 additions
and
28 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 was deleted.
Oops, something went wrong.
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,3 @@ | ||
WIP | ||
# Example: Basic | ||
|
||
This example demonstrates basic usage of this plugin. |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 was deleted.
Oops, something went wrong.
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,3 @@ | ||
# Example: With Errors Plugin | ||
|
||
This example demonstrates how to use [Errors Plugin](https://pothos-graphql.dev/docs/plugins/errors) and get some leverage in catching errors. |
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,17 @@ | ||
{ | ||
"name": "examples-02-with-errors-plugin", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "tsx watch src/index.ts" | ||
}, | ||
"dependencies": { | ||
"@pothos/core": "^3.30.0", | ||
"@pothos/plugin-errors": "^3.11.1", | ||
"graphql": "^16.6.0", | ||
"graphql-yoga": "^4.0.1", | ||
"pothos-plugin-effect": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"tsx": "^3.12.7" | ||
} | ||
} |
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,33 @@ | ||
import * as Context from '@effect/data/Context'; | ||
import { pipe } from '@effect/data/Function'; | ||
import * as Effect from '@effect/io/Effect'; | ||
import * as Layer from '@effect/io/layer'; | ||
|
||
export class RequestError { | ||
readonly _tag = 'RequestError'; | ||
constructor(readonly response: Response | null) {} | ||
} | ||
|
||
export interface Fetch { | ||
get(input: RequestInfo | URL, init?: RequestInit | undefined): Effect.Effect<never, RequestError, Response>; | ||
} | ||
|
||
export const Fetch = Context.Tag<Fetch>(); | ||
|
||
export const FetchLive = Layer.succeed( | ||
Fetch, | ||
Fetch.of({ | ||
get: (input, init) => | ||
pipe( | ||
Effect.tryCatchPromise( | ||
() => fetch(input, init), | ||
() => new RequestError(null), | ||
), | ||
Effect.flatMap(response => | ||
response.ok | ||
? Effect.succeed(response) | ||
: Effect.fail(new RequestError(response)) | ||
), | ||
), | ||
}), | ||
); |
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,66 @@ | ||
import * as Context from '@effect/data/Context'; | ||
import * as Effect from '@effect/io/Effect'; | ||
import * as Layer from '@effect/io/layer'; | ||
import { pipe } from 'graphql-yoga'; | ||
|
||
import { Fetch } from './Fetch.js'; | ||
|
||
export class NotFound extends Error { | ||
readonly _tag = 'NotFound'; | ||
|
||
constructor(username: string) { | ||
super(`User ${username} not found`); | ||
|
||
this.name = 'NotFound'; | ||
} | ||
} | ||
|
||
export class ForbiddenUser extends Error { | ||
readonly _tag = 'ForbiddenUser'; | ||
|
||
constructor(username: string) { | ||
super(`User ${username} is forbidden`); | ||
|
||
this.name = 'ForbiddenUser'; | ||
} | ||
} | ||
|
||
export interface GitHubUser { | ||
followers: number; | ||
name: string; | ||
} | ||
|
||
export interface GitHub { | ||
getUser(username: string): Effect.Effect<never, ForbiddenUser | NotFound, GitHubUser>; | ||
} | ||
|
||
export const GitHub = Context.Tag<GitHub>(); | ||
|
||
export const GitHubLive = Layer.effect( | ||
GitHub, | ||
pipe( | ||
Fetch, | ||
Effect.map(fetch => | ||
GitHub.of({ | ||
getUser: username => | ||
pipe( | ||
Effect.if( | ||
username === 'admin', | ||
/* onTrue */ Effect.fail(new ForbiddenUser(username)), | ||
/* onFalse */ Effect.succeed(username), | ||
), | ||
Effect.flatMap(username => fetch.get(`https://api.github.com/users/${username}`)), | ||
Effect.flatMap(response => Effect.promise(() => response.json() as Promise<GitHubUser>)), | ||
Effect.catchTag('RequestError', () => Effect.fail(new NotFound(username))), | ||
), | ||
}) | ||
), | ||
), | ||
); | ||
|
||
export const GitHubStub = Layer.succeed( | ||
GitHub, | ||
GitHub.of({ | ||
getUser: username => Effect.succeed({ followers: 10_000_000, name: username }), | ||
}), | ||
); |
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,68 @@ | ||
import { pipe } from '@effect/data/Function'; | ||
import * as Effect from '@effect/io/Effect'; | ||
import SchemaBuilder from '@pothos/core'; | ||
import ErrorsPlugin from '@pothos/plugin-errors'; | ||
import { createYoga } from 'graphql-yoga'; | ||
import { createServer } from 'node:http'; | ||
import EffectPlugin from 'pothos-plugin-effect'; | ||
|
||
import { FetchLive } from './Fetch.js'; | ||
import { ForbiddenUser, GitHub, GitHubLive, NotFound } from './GitHub.js'; | ||
|
||
const builder = new SchemaBuilder({ | ||
plugins: [ErrorsPlugin, EffectPlugin], | ||
}); | ||
|
||
const ErrorInterface = builder.interfaceRef<Error>('Error').implement({ | ||
fields: (t) => ({ | ||
message: t.exposeString('message'), | ||
}), | ||
}); | ||
|
||
builder.objectType(Error, { | ||
interfaces: [ErrorInterface], | ||
name: 'BaseError', | ||
}); | ||
|
||
builder.objectType(NotFound, { | ||
interfaces: [ErrorInterface], | ||
name: 'NotFound', | ||
}); | ||
|
||
builder.objectType(ForbiddenUser, { | ||
interfaces: [ErrorInterface], | ||
name: 'ForbiddenUser', | ||
}); | ||
|
||
builder.queryType({ | ||
fields: t => ({ | ||
githubUserFollowers: t.effect({ | ||
args: { | ||
username: t.arg.string({ required: true }), | ||
}, | ||
effect: { | ||
layers: [FetchLive, GitHubLive], | ||
}, | ||
errors: { | ||
types: [NotFound, ForbiddenUser], | ||
}, | ||
resolve: (_parent, args) => | ||
pipe( | ||
GitHub, | ||
Effect.flatMap(github => github.getUser(args.username)), | ||
Effect.map(user => user.followers), | ||
), | ||
type: 'Int', | ||
}), | ||
}), | ||
}); | ||
|
||
const schema = builder.toSchema({ sortSchema: true }); | ||
|
||
const yoga = createYoga({ schema }); | ||
|
||
const server = createServer(yoga); | ||
|
||
server.listen(4000, () => { | ||
console.info('Server is running on http://localhost:4000/graphql'); | ||
}); |
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,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"allowImportingTsExtensions": false, | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src"] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.