Skip to content

Commit

Permalink
feat: include basenames
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Oct 12, 2022
1 parent 01cebfb commit 06aaeaf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,10 @@ import { createServer } from "@graphql-yoga/common";
import { fromManifest } from "$fresh_graphql/schema.ts";
import manifest from "../fresh_graphql.gen.ts";

const schema = fromManifest(manifest);

const yoga = createServer<HandlerContext>({
logging: true,
maskedErrors: false,
schema,
schema: fromManifest(manifest),
});

export const handler = async (req: Request, ctx: HandlerContext) => {
Expand All @@ -103,19 +101,32 @@ export const handler = async (req: Request, ctx: HandlerContext) => {

### Queries and Mutations

The example below follows the naming convension and use `mod.ts`, but you may
name your TypeScript and/or JavaScript files anyway you like.

```ts
// ./graphql/Query/test/mod.ts
// ./graphql/Query/joke.ts

export const schema = `
extend type Query {
foo: String!
joke: String!
}
`;

export const resolver = () => "Hello World!";
// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/
const JOKES = [
"Why do Java developers often wear glasses? They can't C#.",
"A SQL query walks into a bar, goes up to two tables and says “can I join you?”",
"Wasn't hard to crack Forrest Gump's password. 1forrest1.",
"I love pressing the F5 key. It's refreshing.",
"Called IT support and a chap from Australia came to fix my network connection. I asked “Do you come from a LAN down under?”",
"There are 10 types of people in the world. Those who understand binary and those who don't.",
"Why are assembly programmers often wet? They work below C level.",
"My favourite computer based band is the Black IPs.",
"What programme do you use to predict the music tastes of former US presidential candidates? An Al Gore Rhythm.",
"An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.",
];

export const resolver = () => {
return JOKES[Math.floor(Math.random() * JOKES.length)];
};
```

Schema level types, `Query`, `Mutation` and `Subscription`, will be
Expand All @@ -136,7 +147,7 @@ export const resolver = {
### Subscriptions

```ts
// graphql/Subscription/countdown/mod.ts
// graphql/Subscription/countdown.ts

export const schema = `
extend type Subscription {
Expand Down
13 changes: 11 additions & 2 deletions dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// dev.ts: A simple modification of $fresh/dev.ts for GraphQL.
import { ensureDir, walk } from "https://deno.land/[email protected]/fs/mod.ts";
import { dirname, join } from "https://deno.land/[email protected]/path/mod.ts";
import {
dirname,
join,
parse,
} from "https://deno.land/[email protected]/path/mod.ts";
import {
fromFileUrl,
toFileUrl,
Expand Down Expand Up @@ -65,7 +69,12 @@ ${
const manifest = {
modules: {
${modules.map((file, i) => `"${file.slice(1)}": $${i}`).join(",\n ")}
${
modules.map((file, i) => {
const { dir, name } = parse(file);
return `"${dir.slice(1)}/${name}": $${i}`;
}).join(",\n ")
}
},
baseUrl: import.meta.url,
};
Expand Down
22 changes: 6 additions & 16 deletions schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,18 @@ import { sep } from "https://deno.land/[email protected]/path/mod.ts";

export type Callable = (...args: any[]) => any;

export type GraphQLModule<
TSource,
TContext,
TArgs = Record<string, any>,
TReturn = any,
> = {
export type GraphQLModule = {
schema?: string;
resolver?: IResolvers | IFieldResolver<TSource, TContext, TArgs, TReturn>;
resolver?: IResolvers | IFieldResolver<any, any, any, any>;
};

export type Manifest<
TSource,
TContext,
TArgs = Record<string, any>,
TReturn = any,
> = {
modules: Record<string, GraphQLModule<TSource, TContext, TArgs, TReturn>>;
export type Manifest = {
modules: Record<string, GraphQLModule>;
baseUrl: string;
};

export const fromManifest = <
TManifest extends Manifest<any, any, any, any>,
TManifest extends Manifest,
TContext = unknown,
>(
manifest: TManifest,
Expand All @@ -53,7 +43,7 @@ export const fromManifest = <
if (!resolver) return;

const resolverObj: IResolvers = {};
const pathSegments = name.split(sep).slice(0, -1);
const pathSegments = name.split(sep);
const isSubscription = pathSegments[0] === "Subscription";

let currentPath = resolverObj;
Expand Down

0 comments on commit 06aaeaf

Please sign in to comment.