Skip to content

Commit

Permalink
update new docs to reflect custom graphql API changes (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Jun 27, 2023
1 parent 83100db commit 36bfd5e
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 186 deletions.
13 changes: 9 additions & 4 deletions docs/docs/custom-data-access/custom-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ To expose the Ent accessors above to GraphQL as a [list](https://graphql.org/lea
```ts title="src/account.ts"
export class Account extends AccountBase {

@gqlField({ name: "openTodosPlural", type: "[Todo]" })
@gqlField({ class: "Account", name: "openTodosPlural", type: "[Todo]", async: true })
async openTodosPlural() {
return Todo.loadCustom(
this.viewer,
Expand Down Expand Up @@ -214,7 +214,7 @@ To expose the custom ent query above to GraphQL as a connection, use [`gqlConnec
```ts title="src/account.ts"
export class Account extends AccountBase {

@gqlField({ name: "openTodos", type: gqlConnection("Todo") })
@gqlField({ class: "Account", name: "openTodos", type: gqlConnection("Todo") })
openTodos() {
return new AccountToOpenTodosQuery(this.viewer, this);
}
Expand All @@ -227,8 +227,13 @@ To fetch a query that isn't source based or that's global to your database e.g.
```ts

@gqlQuery({ name: "closed_todos_last_day", type: gqlConnection("Todo") })
closedTodosLastDay(@gqlContextType() context: RequestContext) {
@gqlQuery({
class: "TodoResolver",
name: "closed_todos_last_day",
type: gqlConnection("Todo"),
args: [gqlContextType()],
})
closedTodosLastDay(context: RequestContext) {
const start = Interval.before(new Date(), { hours: 24 })
.start.toUTC()
.toISO();
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/custom-graphql/custom-accessors.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class User extends UserBase {

To do so, we'll dive into the Custom GraphQL API.

We use [TypeScript Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) to annotate methods to indicate what we're exposing to GraphQL.
We use [TypeScript Decorators](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#decorators) introduced in TypeScript 5.0 to annotate methods to indicate what we're exposing to GraphQL.

These decorators are evaluated during code generation and they do as little as possible (nothing) otherwise to reduce the overhead of using them.

Expand Down
83 changes: 43 additions & 40 deletions docs/docs/custom-graphql/custom-mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,56 @@ A common thing for a lot of applications is to implement authentication to log t
```ts title="src/graphql/mutations/auth.ts"
@gqlInputObjectType()
export class UserAuthInput {
@gqlField()
emailAddress: string;
@gqlField()
@gqlField({
class: 'UserAuthInput',
type: GraphQLString,
})
emailAddress: string;

@gqlField({
class: 'UserAuthInput',
type: GraphQLString,
})
password: string;

constructor(emailAddress: string, password: string) {
this.emailAddress = emailAddress;
this.password = password;
}
}

@gqlObjectType()
export class UserAuthPayload {
@gqlField({ type: GraphQLID })
@gqlField({
class: "UserAuthPayload",
type: GraphQLID,
})
viewerID: ID;

constructor(viewerID: ID) {
this.viewerID = viewerID;
}
}

export class AuthResolver {
@gqlMutation({ name: "userAuth", type: UserAuthPayload })
@gqlMutation({
class: "AuthResolver",
name: "userAuth",
type: UserAuthPayload,
async: true,
args: [
gqlContextType(),
{
name: "input",
type: "UserAuthInput",
},
]
})
async userAuth(
@gqlContextType() context: RequestContext,
@gqlArg("input") input: UserAuthInput,
context: RequestContext,
input: UserAuthInput,
): Promise<UserAuthPayload> {
return {viewerID : "1"};
return new UserAuthPayload("1");
}
}

Expand Down Expand Up @@ -68,47 +99,19 @@ Here's what's happening here:

This uses the following concepts to implement this:

* [gqlMutation](#gqlMutation)
* [gqlInputObjectType](#gqlInputObjectType)
* [gqlMutation](#gqlmutation)
* [gqlInputObjectType](/docs/custom-graphql/gql-input-object-type)
* [gqlField](/docs/custom-graphql/gql-field)
* [gqlObjectType](#gqlObjectType)
* [gqlObjectType](/docs/custom-graphql/gql-object-type)
* [gqlContextType](/docs/custom-graphql/gql-context)
* [gqlArg](/docs/custom-graphql/gql-arg)

## gqlMutation

This adds a new field to the GraphQL `Mutation` type. See example usage [above](#auth-example).

Accepts the following options which overlap with [gqlField](/docs/custom-graphql/gql-field):

* `class` for the name of the class the function is defined in.
* `name` for the name of the GraphQL field
* `description` of the field
* `type`: type returned by the field

## gqlInputObjectType

Adds a new input object to the schema. See example usage [above](#auth-example).

Options:

### name

Name of the input object. If not specified, defaults to the name of the class.

### description

Description nof the input object. Will be added to the Schema and exposed in tools like [GraphiQL](https://github.com/graphql/graphiql) or [Playground](https://github.com/graphql/graphql-playground).

## gqlObjectType

Adds a new object to the schema. See example usage [above](#auth-example).

Options:

### name

Name of the object. If not specified, defaults to the name of the class

### description

Description nof the object. Will be added to the Schema and exposed in tools like [GraphiQL](https://github.com/graphql/graphiql) or [Playground](https://github.com/graphql/graphql-playground).
48 changes: 28 additions & 20 deletions docs/docs/custom-graphql/custom-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ Here's an example which exposes `Viewer` :
export class ViewerType {
constructor(private viewer: Viewer) {}

@gqlField({ type: GraphQLID, nullable: true })
@gqlField({
class: 'ViewerType',
type: GraphQLID,
nullable: true,
async: true,
})
async viewerID() {
const user = await this.user();
if (!user) {
Expand All @@ -26,7 +31,12 @@ export class ViewerType {
return encodeGQLID(user);
}

@gqlField({ type: User, nullable: true })
@gqlField({
class: 'ViewerType',
type: User,
nullable: true,
async: true,
})
async user(): Promise<User | null> {
const v = this.viewer.viewerID;
if (!v) {
Expand All @@ -37,8 +47,15 @@ export class ViewerType {
}

export default class ViewerResolver {
@gqlQuery({ name: "viewer", type: ViewerType })
viewer(@gqlContextType() context: RequestContext): ViewerType {
@gqlQuery({
class: 'ViewerResolver',
name: "viewer",
type: 'ViewerType',
args: [
gqlContextType(),
],
})
viewer(context: RequestContext): ViewerType {
return new ViewerType(context.getViewer());
}
}
Expand All @@ -52,6 +69,10 @@ type Viewer {
viewerID: ID
user: User
}

type Query {
viewer: Viewer!
}
```

Here's what's happening here:
Expand All @@ -66,7 +87,7 @@ This uses the following concepts to implement this:

* [gqlQuery](#gqlquery)
* [gqlField](/docs/custom-graphql/gql-field)
* [gqlObjectType](#gqlobjecttype)
* [gqlObjectType](/docs/custom-graphql/gql-object-type)
* [gqlContextType](/docs/custom-graphql/gql-context)

## gqlQuery
Expand All @@ -75,20 +96,7 @@ This adds a new field to the GraphQL `Query` type. See example usage [above](#vi

Accepts the following options which overlap with gqlField:

* `name` for the name of the GraphQL field
* `class` for the name of the class the function is defined in.
* `name` for the name of the GraphQL field
* `description` of the field
* `type`: type returned by the field

## gqlObjectType

Adds a new object to the schema. See example usage [above](#viewer).

Options:

### name

Name of the object. If not specified, defaults to the name of the class

### description

Description of the object. Will be added to the Schema and exposed in tools like [GraphiQL](https://github.com/graphql/graphiql) or [Playground](https://github.com/graphql/graphql-playground).
23 changes: 19 additions & 4 deletions docs/docs/custom-graphql/file-uploads.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ Here's an example usage:

```ts
export class ImportGuestResolver {
@gqlMutation({ type: Event })
@gqlMutation({
class: "ImportGuestResolver",
async: true,
type: Event,
args: [
gqlContextType(),
{
name: "eventID",
type: GraphQLID,
},
{
name: "file",
type: gqlFileUpload,
},
],
})
async importGuests(
@gqlContextType() context: RequestContext,
@gqlArg("eventID", { type: GraphQLID }) eventID: ID,
@gqlArg("file", { type: gqlFileUpload }) file: Promise<FileUpload>,
context: RequestContext,
eventID: ID,
file: Promise<FileUpload>,
) {
}
}
Expand Down
57 changes: 0 additions & 57 deletions docs/docs/custom-graphql/gql-arg.md

This file was deleted.

4 changes: 2 additions & 2 deletions docs/docs/custom-graphql/gql-connection.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 8
sidebar_position: 7
---

# gqlConnection
Expand All @@ -17,7 +17,7 @@ For example:
```ts title="src/account.ts"
export class Account extends AccountBase {

@gqlField({ name: "openTodos", type: gqlConnection("Todo") })
@gqlField({ class: "Account", name: "openTodos", type: gqlConnection("Todo") })
openTodos() {
return new AccountToOpenTodosQuery(this.viewer, this);
}
Expand Down
19 changes: 15 additions & 4 deletions docs/docs/custom-graphql/gql-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ For example:
```ts title="src/graphql/mutations/auth.ts"

export class AuthResolver {
@gqlMutation({ name: "userAuth", type: UserAuthPayload })
@gqlMutation({
class: "AuthResolver",
name: "userAuth",
type: UserAuthPayload,
args: [
gqlContextType(),
{
name: 'input',
type: 'UserAuthInput',
},
],
})
async userAuth(
@gqlContextType() context: RequestContext,
@gqlArg("input") input: UserAuthInput,
context: RequestContext,
input: UserAuthInput,
): Promise<UserAuthPayload> {
return {viewerID : "1"};
return new UserAuthPayload("1");
}
}
```
Expand Down
Loading

0 comments on commit 36bfd5e

Please sign in to comment.