Skip to content

Commit

Permalink
port to jsr
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Mar 10, 2024
1 parent f31b4ea commit 670e716
Show file tree
Hide file tree
Showing 23 changed files with 216 additions and 676 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-yoga.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Upload to Deno Deploy
uses: denoland/deployctl@v1
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/jsr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish
on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4

- name: Publish package
run: npx jsr publish
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"deno.enable": true,
"deno.lint": true,
"editor.defaultFormatter": "denoland.vscode-deno"
"editor.defaultFormatter": "denoland.vscode-deno",
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
74 changes: 26 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# fresh_graphql
# fresh-graphql

GraphQL development for Deno Fresh.

Expand All @@ -7,64 +7,39 @@ GraphQL development for Deno Fresh.
1. [`graphql-yoga`](https://fresh-graphql-yoga.deno.dev/graphql)
1. `apollo-server` (soon™)

## Why `fresh_graphql`?
## Why `fresh-graphql`?

1. Familiar developer experience with `fresh` projects.
1. `deno deploy` has no dynamic imports.
1. [`tRPC`](https://trpc.io) doesn't support deno (yet).

## Installation

`fresh_graphql` hooks into the `dev.ts` lifecycle of fresh,
[create a fresh project](https://fresh.deno.dev/docs/getting-started/create-a-project)
if you haven't done so.
```bash
# deno
deno add @vicary/fresh-graphql

You need to patch 3 files from an existing fresh project:
# npm
npx jsr add @vicary/fresh-graphql

### 1. `import_map.json`
# yarn
yarn dlx jsr add @vicary/fresh-graphql

Versions are removed for clarity, should be compatible with `fresh@^1.1.1`.

```diff
{
"imports": {
"$fresh/": "https://deno.land/x/fresh/",
+ "$fresh_graphql/": "https://deno.land/x/fresh_graphql/",

"preact": "https://esm.sh/preact",
"preact/": "https://esm.sh/preact/",

"preact-render-to-string": "https://esm.sh/*preact-render-to-string",

"twind": "https://esm.sh/twind",
"twind/": "https://esm.sh/twind/"
}
}
```

### 2. `deno.json`

```diff
{
"tasks": {
- "start": "deno run -A --watch=static/,routes/ dev.ts"
+ "start": "deno run -A --watch=static/,routes/,graphql/ dev.ts"
}
}
# pnpm
pnpm dlx jsr add @vicary/fresh-graphql
```

### 3. `dev.ts`
1. [Create a fresh project](https://fresh.deno.dev/docs/getting-started/create-a-project)
or checkout your existing Fresh project.
1. Add the following lines to your `dev.ts`:

```diff
- #!/usr/bin/env -S deno run -A --watch=static/,routes/
+ #!/usr/bin/env -S deno run -A --watch=static/,routes/,graphql/

import "https://deno.land/x/dotenv/load.ts";

import dev from "$fresh/dev.ts";
+ import { dev as graphqlDev } from "$fresh_graphql/mod.ts";
+ import { dev as graphql } from "@vicary/fresh-graphql";

+ await graphqlDev(import.meta.url);
+ await graphql(import.meta.url);
await dev(import.meta.url, "./main.ts");
```

Expand All @@ -80,10 +55,11 @@ convension.

import type { HandlerContext } from "$fresh/server.ts";
import { createServer } from "@graphql-yoga/common";
import { fromManifest } from "$fresh_graphql/schema.ts";
import manifest from "../fresh_graphql.gen.ts";
import { fromManifest } from "@vicary/fresh-graphql";
import manifest from "../graphql.gen.ts";

const yoga = createServer<HandlerContext>({
graphiql: Deno.env.has("FRSH_GQL_DEV"), // Enable GraphiQL in development
logging: true,
maskedErrors: false,
schema: fromManifest(manifest),
Expand All @@ -94,11 +70,6 @@ export const handler = async (req: Request, ctx: HandlerContext) => {
};
```

1. `@graphql-yoga/common` is chosen for it's simplicity, you may use any GraphQL
serveres compatible with `@graphql-tools/schema`.
1. `fresh_graphql.gen.ts` This is the manifest file generated whenever you make
changes to source codes in the `./graphql` directory.

### Queries and Mutations

```ts
Expand Down Expand Up @@ -161,6 +132,13 @@ export const resolver = async function* (_, { from }) {
};
```

### Side notes

1. `@graphql-yoga/common` is chosen for it's simplicity, you may use any GraphQL
serveres compatible with `@graphql-tools/schema`.
1. `graphql.gen.ts` This is the manifest file generated whenever you make
changes to source codes in the `./graphql` directory.

## Sponsorship

If you think I did a good job or want to see a feature happening,
Expand Down
13 changes: 4 additions & 9 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{
"tasks": {
"deploy:yoga": "deployctl deploy --project=fresh-graphql-yoga examples/graphql-yoga/main.ts",
"start:yoga": "deno run -A --watch=static/,routes/,graphql/ examples/graphql-yoga/dev.ts"
},
"importMap": "./import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
"name": "@vicary/fresh-graphql",
"version": "0.1.0",
"exports": "./mod.ts",
"lock": false
}
61 changes: 29 additions & 32 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
export { ensureMinDenoVersion } from "$fresh/src/dev/mod.ts";
export { ensureDir, walk } from "https://deno.land/std@0.159.0/fs/mod.ts";
export { assert } from "jsr:@std/assert@^0.219.1";
export { ensureDir, walk } from "jsr:@std/fs@^0.219.1";
export {
dirname,
fromFileUrl,
join,
parse as parsePath,
toFileUrl,
} from "https://deno.land/[email protected]/path/mod.ts";
export { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
export { makeExecutableSchema } from "https://esm.sh/@graphql-tools/[email protected]?external=graphql";
export type { IExecutableSchemaDefinition } from "https://esm.sh/@graphql-tools/[email protected]?external=graphql";
} from "jsr:@std/path@^0.219.1";
export {
type IExecutableSchemaDefinition,
makeExecutableSchema,
} from "npm:@graphql-tools/schema@^10.0.3";
export {
type ArgumentMapper,
type EnumTypeMapper,
type EnumValueMapper,
type FieldMapper,
type GenericFieldMapper,
getDirective,
type IFieldResolver,
type InputFieldMapper,
type InputObjectTypeMapper,
type InterfaceTypeMapper,
type IResolvers,
MapperKind,
mapSchema,
} from "https://esm.sh/@graphql-tools/[email protected]?external=graphql";
export type {
ArgumentMapper,
EnumTypeMapper,
EnumValueMapper,
FieldMapper,
GenericFieldMapper,
IFieldResolver,
InputFieldMapper,
InputObjectTypeMapper,
InterfaceTypeMapper,
IResolvers,
ObjectTypeMapper,
ScalarTypeMapper,
SchemaMapper,
UnionTypeMapper,
} from "https://esm.sh/@graphql-tools/[email protected]?external=graphql";
type ObjectTypeMapper,
type ScalarTypeMapper,
type SchemaMapper,
type UnionTypeMapper,
} from "npm:@graphql-tools/utils@^10.1.0";
export {
defaultFieldResolver,
type DirectiveDefinitionNode,
type GraphQLFieldConfig,
type GraphQLFieldResolver,
type GraphQLInputFieldConfig,
type GraphQLScalarType,
type GraphQLSchema,
parse as parseGraphQL,
} from "https://esm.sh/[email protected]";
export type {
DirectiveDefinitionNode,
GraphQLFieldConfig,
GraphQLFieldResolver,
GraphQLInputFieldConfig,
GraphQLScalarType,
GraphQLSchema,
} from "https://esm.sh/[email protected]";
} from "npm:graphql@^16.8.0";
Loading

0 comments on commit 670e716

Please sign in to comment.