Skip to content

Commit

Permalink
docs: add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Oct 13, 2022
1 parent c40fa60 commit 2b710e0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# graphql-response

A reference implementation of GraphQL-over-HTTP request for JavaScript.

## GraphQL response from request

Create a GraphQL-over-HTTP compliant HTTP `Response` from an HTTP `Request`.

Example of creating a GraphQL response from a GraphQL request:

```ts
import { createResponse } from "https://deno.land/x/gaphql_response@$VERSION/mod.ts";
import { buildSchema } from "https://esm.sh/graphql@$VERSION";

const url = new URL("http://localhost/graphql");
const query = `query Test { greet }`;
url.searchParams.set("query", query);
const qqlRequest = new Request(url);

const schema = buildSchema(`type Query {
greet: String
}`);
const response = await createResponse(qqlRequest, {
schema,
rootValue: {
greet: () => "hello world!",
},
});
```

Note that this is asynchronous, since the request body may be read.

### GraphQL Execution parameters

`createRequest` accepts portions of GraphQL `ExecutionArgs` as is.

- schema
- rootValue
- contextValue
- fieldResolver
- typeResolver
- subscribeFieldResolver

The following parameters are obtained from the `Request` object and need not be
passed.

- document
- variableValues
- operationName

## License

Copyright © 2022-present [graphqland](https://github.com/graphqland).

Released under the [MIT](./LICENSE) license
23 changes: 11 additions & 12 deletions response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,25 @@ export function withCharset<T extends string>(value: T): `${T};charset=UTF-8` {
return `${value};charset=UTF-8`;
}

/** Create a GraphQL over HTTP compliant `Response` object.
/** Create a GraphQL-over-HTTP compliant HTTP `Response` from an HTTP `Request`.
*
* @example
* ```ts
* import {
* createResponse,
* } from "https://deno.land/x/graphql_http@$VERSION/mod.ts";
* import { createResponse } from "https://deno.land/x/gaphql_response@$VERSION/mod.ts";
* import { buildSchema } from "https://esm.sh/graphql@$VERSION";
*
* const schema = buildSchema(`query {
* hello: String!
* }`);
* const url = new URL("http://localhost/graphql");
* const query = `query Test { greet }`;
* url.searchParams.set("query", query);
* const qqlRequest = new Request(url);
*
* const res = createResponse({
* const schema = buildSchema(`type Query {
* greet: String
* }`);
* const response = await createResponse(qqlRequest, {
* schema,
* source: `query { hello }`,
* method: "POST",
* }, {
* rootValue: {
* hello: "world",
* greet: () => "hello world!",
* },
* });
* ```
Expand Down

0 comments on commit 2b710e0

Please sign in to comment.