Skip to content

Commit

Permalink
feat: export resolveRequest function that take out graphql paramete…
Browse files Browse the repository at this point in the history
…rs from `Request` object safety
  • Loading branch information
TomokiMiyauci committed Jul 21, 2022
1 parent dec34c4 commit 53b04ae
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 19 deletions.
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,50 @@ if (!err) {

`[data: Request, error: undefined] | [data: undefined, error: TypeError]`

#### resolveResponse
### resolveRequest

Resolve GraphQL over HTTP request, take out GraphQL parameters safety.

#### Example

```ts
import {
resolveRequest,
} from "https://deno.land/x/graphql_http@$VERSION/mod.ts";

const req = new Request("<graphql-endpoint>"); // any Request
const [data, err] = await resolveRequest(req);
if (data) {
const { query, variableValues, operationName, extensions } = data;
}
```

#### Parameters

| Name | Required | Description |
| ---- | :----------------: | ------------------------------ |
| req | :white_check_mark: | `Request`<br> `Request` object |

#### ReturnType

`Promise<RequestResult>` | `RequestResult`

RequestResult:

| N | Name | Description |
| - | -------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| 1 | data | Bellow records &#124; `undefined`<br>GraphQL parameters. |
| | query | `string`<br>A Document containing GraphQL Operations and Fragments to execute. |
| | variableValues | `Record<string, json>` &#124; `null`<br>Values for any Variables defined by the Operation. |
| | operationName | `string` &#124; `null`<br>The name of the Operation in the Document to execute. |
| | extensions | `Record<string, json>` &#124; `null`<br>Reserved for implementors to extend the protocol however they see fit. |
| 2 | error | `HttpError` &#124; `undefined`<br>The base class that all derivative HTTP extend, providing a status and an expose property. |

#### Remark

No error is thrown and `reject` is never called.

### resolveResponse

Resolve GraphQL over HTTP response safety.

Expand All @@ -363,9 +406,9 @@ const { data, errors, extensions } = await resolveResponse(res);

#### Parameters

| Name | Required | Description |
| ---- | :----------------: | ------------------------------ |
| res | :white_check_mark: | `Request`<br> `Request` object |
| Name | Required | Description |
| ---- | :----------------: | -------------------------------- |
| res | :white_check_mark: | `Response`<br> `Response` object |

#### ReturnType

Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as graphqlHttp, type Params } from "./graphql_http.ts";
export { default as gqlFetch } from "./fetch.ts";
export { createRequest } from "./requests.ts";
export { createRequest, resolveRequest } from "./requests.ts";
export { resolveResponse } from "./responses.ts";
35 changes: 29 additions & 6 deletions requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,29 @@ function addQueryString(
return [url, undefined];
}

type ResolveRequestResult = [data: GraphQLParameters] | [
export type RequestResult = [data: GraphQLParameters] | [
data: undefined,
error: HttpError,
];

/** Resolve GraphQL over HTTP request, take out GraphQL parameters safety.
* @params req `Request` object
* @remark No error is thrown and `reject` is never called.
* ```ts
* import {
* resolveRequest,
* } from "https://deno.land/x/graphql_http@$VERSION/mod.ts";
*
* const req = new Request("<graphql-endpoint>"); // any Request
* const [data, err] = await resolveRequest(req);
* if (data) {
* const { query, variableValues, operationName, extensions } = data;
* }
* ```
*/
export function resolveRequest(
req: Request,
): ResolveRequestResult | Promise<ResolveRequestResult> {
): RequestResult | Promise<RequestResult> {
const method = req.method;

switch (method) {
Expand All @@ -186,7 +201,7 @@ export function resolveRequest(
}
}

export function resolveGetRequest(req: Request): ResolveRequestResult {
export function resolveGetRequest(req: Request): RequestResult {
const acceptResult = resolveAcceptHeader(req);

if (acceptResult[1]) {
Expand All @@ -202,7 +217,7 @@ export function resolveGetRequest(req: Request): ResolveRequestResult {
createHttpError(Status.BadRequest, `The parameter is required. "query"`),
];
}
let variableValues: Record<string, unknown> | null = null;
let variableValues: GraphQLParameters["variableValues"] | null = null;
const variables = url.searchParams.get("variables");
if (isString(variables)) {
const [data, err] = JSON.parse(variables);
Expand All @@ -226,11 +241,13 @@ export function resolveGetRequest(req: Request): ResolveRequestResult {
query: source,
variableValues,
operationName,
extensions: null,
}];
}

export async function resolvePostRequest(
req: Request,
): Promise<ResolveRequestResult> {
): Promise<RequestResult> {
const acceptHeader = resolveAcceptHeader(req);

if (acceptHeader[1]) {
Expand Down Expand Up @@ -339,6 +356,7 @@ export async function resolvePostRequest(
query,
operationName,
variableValues: variables,
extensions: null,
}];
}

Expand All @@ -356,7 +374,12 @@ export async function resolvePostRequest(
),
];
}
return [{ query }];
return [{
query,
operationName: null,
variableValues: null,
extensions: null,
}];
}

default: {
Expand Down
37 changes: 33 additions & 4 deletions requests_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ it(
query: "query",
variableValues: null,
operationName: null,
extensions: null,
});
},
);
Expand All @@ -270,6 +271,7 @@ it(
query: "query",
variableValues: null,
operationName: null,
extensions: null,
});
},
);
Expand Down Expand Up @@ -316,6 +318,7 @@ it(
test: "test",
},
operationName: "query",
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand Down Expand Up @@ -528,6 +531,7 @@ it(describePostTests, `application/json`, async (t) => {
query: "test",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand All @@ -549,6 +553,7 @@ it(describePostTests, `application/json`, async (t) => {
query: "from body",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand All @@ -569,6 +574,7 @@ it(describePostTests, `application/json`, async (t) => {
query: "from body",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand Down Expand Up @@ -608,6 +614,7 @@ it(describePostTests, `application/json`, async (t) => {
query: "query",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand All @@ -628,6 +635,7 @@ it(describePostTests, `application/json`, async (t) => {
query: "query",
operationName: null,
variableValues: { abc: [] },
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand Down Expand Up @@ -667,6 +675,7 @@ it(describePostTests, `application/json`, async (t) => {
query: "query",
operationName: "subscription",
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
Expand Down Expand Up @@ -707,7 +716,12 @@ it(
},
}),
);
expect(result[0]).toEqual({ query: "test" });
expect(result[0]).toEqual({
query: "test",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
);
Expand All @@ -723,7 +737,12 @@ it(
},
}),
);
expect(result[0]).toEqual({ query: "test" });
expect(result[0]).toEqual({
query: "test",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
);
Expand All @@ -740,7 +759,12 @@ it(
},
}),
);
expect(result[0]).toEqual({ query: "test" });
expect(result[0]).toEqual({
query: "test",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
);
Expand All @@ -757,7 +781,12 @@ it(
},
}),
);
expect(result[0]).toEqual({ query: "from body" });
expect(result[0]).toEqual({
query: "from body",
operationName: null,
variableValues: null,
extensions: null,
});
expect(result[1]).toBeUndefined();
},
);
Expand Down
2 changes: 1 addition & 1 deletion responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function resolveError(er: unknown): GraphQLError {

/**
* Resolve GraphQL over HTTP response safety.
* @param res `Request` object
* @param res `Response` object
* @throws Error
* @throws AggregateError
* @throws SyntaxError
Expand Down
6 changes: 3 additions & 3 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export type GraphQLParameters = {
query: string;

/** Values for any Variables defined by the Operation. */
variableValues?: Record<string, unknown> | null;
variableValues: Record<string, json> | null;

/** The name of the Operation in the Document to execute. */
operationName?: string | null;
operationName: string | null;

/** Reserved for implementors to extend the protocol however they see fit. */
extensions?: Record<string, unknown> | null;
extensions: Record<string, json> | null;
};

0 comments on commit 53b04ae

Please sign in to comment.