@honojs/graphql-server
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,12 +0,12 @@ | ||
import { GraphQLError } from "graphql"; | ||
import type { GraphQLSchema, ValidationRule, GraphQLFormattedError } from "graphql"; | ||
import type { Context } from "hono"; | ||
import type { Next } from "hono"; | ||
import { GraphQLError } from 'graphql'; | ||
import type { GraphQLSchema, ValidationRule, GraphQLFormattedError } from 'graphql'; | ||
import type { Context } from 'hono'; | ||
export declare type RootResolver = (ctx?: Context) => Promise<unknown> | unknown; | ||
declare type Options = { | ||
schema: GraphQLSchema; | ||
rootValue?: unknown; | ||
rootResolver?: RootResolver; | ||
pretty?: boolean; | ||
validationRules?: ReadonlyArray<ValidationRule>; | ||
}; | ||
export declare const graphqlServer: (options: Options) => (c: Context, next: Next) => Promise<Response>; | ||
export declare const graphqlServer: (options: Options) => (c: Context) => Promise<Response>; | ||
export interface GraphQLParams { | ||
@@ -13,0 +13,0 @@ query: string | null; |
"use strict"; | ||
// Based on the code in the `express-graphql` package. | ||
// https://github.com/graphql/express-graphql/blob/main/src/index.ts | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -10,11 +8,10 @@ exports.errorMessages = exports.getGraphQLParams = exports.graphqlServer = void 0; | ||
const schema = options.schema; | ||
const rootValue = options.rootValue; | ||
const pretty = options.pretty ?? false; | ||
const validationRules = options.validationRules ?? []; | ||
// const showGraphiQL = options.graphiql ?? false | ||
return async (c, next) => { | ||
return async (c) => { | ||
// GraphQL HTTP only supports GET and POST methods. | ||
if (c.req.method !== "GET" && c.req.method !== "POST") { | ||
return c.json((0, exports.errorMessages)(["GraphQL only supports GET and POST requests."]), 405, { | ||
Allow: "GET, POST", | ||
if (c.req.method !== 'GET' && c.req.method !== 'POST') { | ||
return c.json((0, exports.errorMessages)(['GraphQL only supports GET and POST requests.']), 405, { | ||
Allow: 'GET, POST', | ||
}); | ||
@@ -35,3 +32,3 @@ } | ||
if (query == null) { | ||
return c.json((0, exports.errorMessages)(["Must provide query string."]), 400); | ||
return c.json((0, exports.errorMessages)(['Must provide query string.']), 400); | ||
} | ||
@@ -41,7 +38,7 @@ const schemaValidationErrors = (0, graphql_1.validateSchema)(schema); | ||
// Return 500: Internal Server Error if invalid schema. | ||
return c.json((0, exports.errorMessages)(["GraphQL schema validation error."], schemaValidationErrors), 500); | ||
return c.json((0, exports.errorMessages)(['GraphQL schema validation error.'], schemaValidationErrors), 500); | ||
} | ||
let documentAST; | ||
try { | ||
documentAST = (0, graphql_1.parse)(new graphql_1.Source(query, "GraphQL request")); | ||
documentAST = (0, graphql_1.parse)(new graphql_1.Source(query, 'GraphQL request')); | ||
} | ||
@@ -55,3 +52,3 @@ catch (syntaxError) { | ||
}); | ||
return c.json((0, exports.errorMessages)(["GraphQL syntax error."], [e]), 400); | ||
return c.json((0, exports.errorMessages)(['GraphQL syntax error.'], [e]), 400); | ||
} | ||
@@ -61,14 +58,11 @@ throw syntaxError; | ||
// Validate AST, reporting any errors. | ||
const validationErrors = (0, graphql_1.validate)(schema, documentAST, [ | ||
...graphql_1.specifiedRules, | ||
...validationRules, | ||
]); | ||
const validationErrors = (0, graphql_1.validate)(schema, documentAST, [...graphql_1.specifiedRules, ...validationRules]); | ||
if (validationErrors.length > 0) { | ||
// Return 400: Bad Request if any validation errors exist. | ||
return c.json((0, exports.errorMessages)(["GraphQL validation error."], validationErrors), 400); | ||
return c.json((0, exports.errorMessages)(['GraphQL validation error.'], validationErrors), 400); | ||
} | ||
if (c.req.method === "GET") { | ||
if (c.req.method === 'GET') { | ||
// Determine if this GET request will perform a non-query. | ||
const operationAST = (0, graphql_1.getOperationAST)(documentAST, operationName); | ||
if (operationAST && operationAST.operation !== "query") { | ||
if (operationAST && operationAST.operation !== 'query') { | ||
/* | ||
@@ -83,6 +77,7 @@ Now , does not support GraphiQL | ||
`Can only perform a ${operationAST.operation} operation from a POST request.`, | ||
]), 405, { Allow: "POST" }); | ||
]), 405, { Allow: 'POST' }); | ||
} | ||
} | ||
let result; | ||
const { rootResolver } = options; | ||
try { | ||
@@ -92,3 +87,3 @@ result = await (0, graphql_1.execute)({ | ||
document: documentAST, | ||
rootValue, | ||
rootValue: rootResolver ? await rootResolver(c) : null, | ||
variableValues: variables, | ||
@@ -106,3 +101,3 @@ operationName: operationName, | ||
// Return 400: Bad Request if any execution context errors exist. | ||
return c.json((0, exports.errorMessages)(["GraphQL execution context error."], [e]), 400); | ||
return c.json((0, exports.errorMessages)(['GraphQL execution context error.'], [e]), 400); | ||
} | ||
@@ -124,3 +119,3 @@ throw contextError; | ||
return c.text(payload, 200, { | ||
"Content-Type": "application/json", | ||
'Content-Type': 'application/json', | ||
}); | ||
@@ -131,3 +126,2 @@ } | ||
} | ||
await next(); // XXX | ||
}; | ||
@@ -137,12 +131,12 @@ }; | ||
const getGraphQLParams = async (request) => { | ||
const urlData = new URLSearchParams(request.url.split("?")[1]); | ||
const urlData = new URLSearchParams(request.url.split('?')[1]); | ||
const bodyData = await (0, parse_body_1.parseBody)(request); | ||
// GraphQL Query string. | ||
let query = urlData.get("query") ?? bodyData.query; | ||
if (typeof query !== "string") { | ||
let query = urlData.get('query') ?? bodyData.query; | ||
if (typeof query !== 'string') { | ||
query = null; | ||
} | ||
// Parse the variables if needed. | ||
let variables = (urlData.get("variables") ?? bodyData.variables); | ||
if (typeof variables === "string") { | ||
let variables = (urlData.get('variables') ?? bodyData.variables); | ||
if (typeof variables === 'string') { | ||
try { | ||
@@ -152,14 +146,14 @@ variables = JSON.parse(variables); | ||
catch { | ||
throw Error("Variables are invalid JSON."); | ||
throw Error('Variables are invalid JSON.'); | ||
} | ||
} | ||
else if (typeof variables !== "object") { | ||
else if (typeof variables !== 'object') { | ||
variables = null; | ||
} | ||
// Name of GraphQL operation to execute. | ||
let operationName = urlData.get("operationName") ?? bodyData.operationName; | ||
if (typeof operationName !== "string") { | ||
let operationName = urlData.get('operationName') ?? bodyData.operationName; | ||
if (typeof operationName !== 'string') { | ||
operationName = null; | ||
} | ||
const raw = urlData.get("raw") != null || bodyData.raw !== undefined; | ||
const raw = urlData.get('raw') != null || bodyData.raw !== undefined; | ||
const params = { | ||
@@ -166,0 +160,0 @@ query: query, |
{ | ||
"name": "@honojs/graphql-server", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"repository": "git@github.com:honojs/grahql-server.git", | ||
@@ -24,2 +24,4 @@ "author": "Minghe Huang <h.minghe@gmail.com>", | ||
"build": "rimraf dist && tsc --project tsconfig.build.json", | ||
"lint": "eslint --ext js,ts src .eslintrc.js", | ||
"lint:fix": "eslint --ext js,ts src .eslintrc.js --fix", | ||
"release": "npm publish" | ||
@@ -37,7 +39,20 @@ }, | ||
"jest-environment-miniflare": "^2.6.0", | ||
"@typescript-eslint/eslint-plugin": "^5.21.0", | ||
"@typescript-eslint/parser": "^5.21.0", | ||
"prettier": "^2.7.1", | ||
"eslint": "^8.14.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-define-config": "^1.4.0", | ||
"eslint-import-resolver-typescript": "^2.7.1", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-flowtype": "^8.0.3", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^28.0.5", | ||
"typescript": "^4.7.4" | ||
}, | ||
"engines": { | ||
"node": ">=11.0.0" | ||
} | ||
} |
@@ -7,4 +7,4 @@ # GraphQL Server Middleware | ||
```plain | ||
npm i graphql | ||
```sh | ||
npm i @honojs/graphql-server | ||
``` | ||
@@ -15,3 +15,3 @@ | ||
```plain | ||
yarn add graphql | ||
yarn add @honojs/graphql-server | ||
``` | ||
@@ -25,3 +25,3 @@ | ||
import { Hono } from 'hono' | ||
import { graphqlServer } from 'hono/graphql-server' | ||
import { graphqlServer } from '@hono/graphql-server' | ||
import { buildSchema } from 'graphql' | ||
@@ -37,4 +37,6 @@ | ||
const rootValue = { | ||
hello: () => 'Hello Hono!', | ||
const rootResolver = (ctx) => { | ||
return { | ||
hello: () => 'Hello Hono!', | ||
} | ||
} | ||
@@ -46,3 +48,3 @@ | ||
schema, | ||
rootValue, | ||
rootResolver, | ||
}) | ||
@@ -49,0 +51,0 @@ ) |
11161
50
19
231