-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixups/tests/docs for derived context
Summary: Test Plan:
- Loading branch information
1 parent
a9438e4
commit 0e141b7
Showing
5 changed files
with
180 additions
and
24 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
31 changes: 31 additions & 0 deletions
31
website/docs/04-docblock-tags/snippets/04-derived-context-memoized.grats.ts
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,31 @@ | ||
// trim-start | ||
class DB { | ||
selectUser(): { name: string } { | ||
return { name: "Alice" }; | ||
} | ||
} | ||
|
||
// trim-end | ||
/** @gqlContext */ | ||
type Ctx = {}; | ||
|
||
// A derived context resolver cached using a WeakMap to ensure the value is | ||
// computed at most once per request. | ||
|
||
const DB_CACHE = new WeakMap<Ctx, DB>(); | ||
|
||
// highlight-start | ||
/** @gqlContext */ | ||
export function getDb(ctx: Ctx): DB { | ||
if (!DB_CACHE.has(ctx)) { | ||
DB_CACHE.set(ctx, new DB()); | ||
} | ||
return DB_CACHE.get(ctx)!; | ||
} | ||
// highlight-end | ||
|
||
/** | ||
* @gqlQueryField */ | ||
export function me(db: DB): string { | ||
return db.selectUser().name; | ||
} |
59 changes: 59 additions & 0 deletions
59
website/docs/04-docblock-tags/snippets/04-derived-context-memoized.out
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,59 @@ | ||
// trim-start | ||
class DB { | ||
selectUser(): { name: string } { | ||
return { name: "Alice" }; | ||
} | ||
} | ||
|
||
// trim-end | ||
/** @gqlContext */ | ||
type Ctx = {}; | ||
|
||
// A derived context resolver cached using a WeakMap to ensure the value is | ||
// computed at most once per request. | ||
|
||
const DB_CACHE = new WeakMap<Ctx, DB>(); | ||
|
||
// highlight-start | ||
/** @gqlContext */ | ||
export function getDb(ctx: Ctx): DB { | ||
if (!DB_CACHE.has(ctx)) { | ||
DB_CACHE.set(ctx, new DB()); | ||
} | ||
return DB_CACHE.get(ctx)!; | ||
} | ||
// highlight-end | ||
|
||
/** | ||
* @gqlQueryField */ | ||
export function me(db: DB): string { | ||
return db.selectUser().name; | ||
} | ||
|
||
=== SNIP === | ||
type Query { | ||
me: String | ||
} | ||
=== SNIP === | ||
import { me as queryMeResolver, getDb as getDb } from "./04-derived-context-memoized.grats"; | ||
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from "graphql"; | ||
export function getSchema(): GraphQLSchema { | ||
const QueryType: GraphQLObjectType = new GraphQLObjectType({ | ||
name: "Query", | ||
fields() { | ||
return { | ||
me: { | ||
name: "me", | ||
type: GraphQLString, | ||
resolve(_source, _args, context) { | ||
return queryMeResolver(getDb(context)); | ||
} | ||
} | ||
}; | ||
} | ||
}); | ||
return new GraphQLSchema({ | ||
query: QueryType, | ||
types: [QueryType] | ||
}); | ||
} |
25 changes: 25 additions & 0 deletions
25
website/docs/04-docblock-tags/snippets/04-derived-context.grats.ts
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,25 @@ | ||
// trim-start | ||
type DB = { | ||
selectUser(): { name: string }; | ||
/*...*/ | ||
}; | ||
|
||
// trim-end | ||
/** @gqlContext */ | ||
type Ctx = { db: DB }; | ||
|
||
// highlight-start | ||
/** @gqlContext */ | ||
export function getDb(ctx: Ctx): DB { | ||
return ctx.db; | ||
} | ||
// highlight-end | ||
|
||
/** | ||
* A field which reads a derived context. Grats will invoke the above `getDb` | ||
* function and pass it to this resolver function. | ||
* | ||
* @gqlQueryField */ | ||
export function me(db: DB): string { | ||
return db.selectUser().name; | ||
} |
58 changes: 58 additions & 0 deletions
58
website/docs/04-docblock-tags/snippets/04-derived-context.out
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,58 @@ | ||
// trim-start | ||
type DB = { | ||
selectUser(): { name: string }; | ||
/*...*/ | ||
}; | ||
|
||
// trim-end | ||
/** @gqlContext */ | ||
type Ctx = { db: DB }; | ||
|
||
// highlight-start | ||
/** @gqlContext */ | ||
export function getDb(ctx: Ctx): DB { | ||
return ctx.db; | ||
} | ||
// highlight-end | ||
|
||
/** | ||
* A field which reads a derived context. Grats will invoke the above `getDb` | ||
* function and pass it to this resolver function. | ||
* | ||
* @gqlQueryField */ | ||
export function me(db: DB): string { | ||
return db.selectUser().name; | ||
} | ||
|
||
=== SNIP === | ||
type Query { | ||
""" | ||
A field which reads a derived context. Grats will invoke the above `getDb` | ||
function and pass it to this resolver function. | ||
""" | ||
me: String | ||
} | ||
=== SNIP === | ||
import { me as queryMeResolver, getDb as getDb } from "./04-derived-context.grats"; | ||
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from "graphql"; | ||
export function getSchema(): GraphQLSchema { | ||
const QueryType: GraphQLObjectType = new GraphQLObjectType({ | ||
name: "Query", | ||
fields() { | ||
return { | ||
me: { | ||
description: "A field which reads a derived context. Grats will invoke the above `getDb`\nfunction and pass it to this resolver function.", | ||
name: "me", | ||
type: GraphQLString, | ||
resolve(_source, _args, context) { | ||
return queryMeResolver(getDb(context)); | ||
} | ||
} | ||
}; | ||
} | ||
}); | ||
return new GraphQLSchema({ | ||
query: QueryType, | ||
types: [QueryType] | ||
}); | ||
} |