Skip to content

Commit

Permalink
feat: accept schema as string, build schema with error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 18, 2022
1 parent 23d0455 commit 3b81be1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import {
} from "https://deno.land/std@$VERSION/http/mod.ts";
import { buildSchema } from "https://esm.sh/graphql@$VERSION";

const schema = `type Query {
hello: String!
}
`;
const graphqlResponse = graphqlHttp({
schema: buildSchema(schema),
schema: `type Query {
hello: String!
}`,
rootValue: {
hello: () => "world",
},
Expand Down
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {
buildSchema,
type ExecutionResult,
getOperationAST,
graphql,
Expand Down
30 changes: 21 additions & 9 deletions graphql_http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { validatePlaygroundRequest, validateRequest } from "./validates.ts";
import {
accepts,
buildSchema,
contentType,
ExecutionResult,
getOperationAST,
Expand All @@ -26,7 +27,7 @@ export type Params =
& PartialBy<Omit<GraphQLArgs, "schema">, "source">
& {
/** A GraphQL schema from graphql-js. */
schema: GraphQLSchema;
schema: GraphQLSchema | string;

/** Overwrite actual response.
* ```ts
Expand Down Expand Up @@ -62,7 +63,11 @@ export type ResponseContext = {
};

/** Make a GraphQL `Response` Object that validate to `Request` Object.
* @throws AggregateError - When graphql schema validation is fail.
* @throws {@link AggregateError}
* When graphql schema validation is fail.
*
* @throws {@link GraphQLError}
* When schema building is fail.
* ```ts
* import { graphqlHttp } from "https://deno.land/x/graphql_http@$VERSION/mod.ts";
* import {
Expand All @@ -72,12 +77,10 @@ export type ResponseContext = {
* } from "https://deno.land/std@$VERSION/http/mod.ts";
* import { buildSchema } from "https://esm.sh/graphql@$VERSION";
*
* const schema = `type Query {
* hello: String!
* }
* `;
* const graphqlResponse = graphqlHttp({
* schema: buildSchema(schema),
* schema: `type Query {
* hello: String!
* }`,
* rootValue: {
* hello: () => "world",
* },
Expand All @@ -103,11 +106,19 @@ export default function graphqlHttp(
response = (res) => res,
playground,
playgroundOptions = { endpoint: "/graphql" },
schema,
schema: _schema,
...rest
}: Readonly<Params>,
): (req: Request) => Promise<Response> {
const validateSchemaResult = validateSchema(schema);
if (isString(_schema)) {
try {
_schema = buildSchema(_schema);
} catch (e) {
throw e as GraphQLError;
}
}

const validateSchemaResult = validateSchema(_schema);
if (validateSchemaResult.length) {
throw new AggregateError(validateSchemaResult, "Schema validation error");
}
Expand All @@ -119,6 +130,7 @@ export default function graphqlHttp(
};

async function process(req: Request): Promise<[Response, ResponseContext]> {
const schema = _schema as GraphQLSchema;
const responseCtx: ResponseContext = { request: req };
const mediaType = getMediaType(req);
const preferContentType = withCharset(mediaType);
Expand Down

0 comments on commit 3b81be1

Please sign in to comment.