diff --git a/README.md b/README.md index ee1acda..eca3309 100644 --- a/README.md +++ b/README.md @@ -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", }, diff --git a/deps.ts b/deps.ts index ab618b9..4d32862 100644 --- a/deps.ts +++ b/deps.ts @@ -1,4 +1,5 @@ export { + buildSchema, type ExecutionResult, getOperationAST, graphql, diff --git a/graphql_http.ts b/graphql_http.ts index 97ed890..2616b0f 100644 --- a/graphql_http.ts +++ b/graphql_http.ts @@ -1,6 +1,7 @@ import { validatePlaygroundRequest, validateRequest } from "./validates.ts"; import { accepts, + buildSchema, contentType, ExecutionResult, getOperationAST, @@ -26,7 +27,7 @@ export type Params = & PartialBy, "source"> & { /** A GraphQL schema from graphql-js. */ - schema: GraphQLSchema; + schema: GraphQLSchema | string; /** Overwrite actual response. * ```ts @@ -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 { @@ -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", * }, @@ -103,11 +106,19 @@ export default function graphqlHttp( response = (res) => res, playground, playgroundOptions = { endpoint: "/graphql" }, - schema, + schema: _schema, ...rest }: Readonly, ): (req: Request) => Promise { - 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"); } @@ -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);