Giter Site home page Giter Site logo

jaydenseric / graphql-api-koa Goto Github PK

View Code? Open in Web Editor NEW
52.0 5.0 10.0 297 KB

GraphQL execution and error handling middleware written from scratch for Koa.

Home Page: https://npm.im/graphql-api-koa

License: MIT License

JavaScript 100.00%
graphql koa api node npm maintained

graphql-api-koa's Introduction

graphql-api-koa logo

graphql-api-koa

GraphQL execution and error handling middleware written from scratch for Koa.

Installation

To install graphql-api-koa and its graphql peer dependency with npm, run:

npm install graphql-api-koa graphql

Setup the Koa middleware in this order:

  1. errorHandler, to catch errors from following middleware for a correctly formatted GraphQL response.
  2. A GraphQL multipart request processor like graphqlUploadKoa from graphql-upload, to support file uploads (optional).
  3. A request body parser like koa-bodyparser.
  4. execute, to execute GraphQL.

See the execute middleware examples to get started.

Requirements

Supported runtime environments:

  • Node.js versions ^14.17.0 || ^16.0.0 || >= 18.0.0.

Projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check comment:

Exports

The npm package graphql-api-koa features optimal JavaScript module design. It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the package.json field exports:

graphql-api-koa's People

Contributors

jaydenseric avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

graphql-api-koa's Issues

Resolver error status code is not respected

Hey πŸ‘‹

When error is thrown in middleware its status code is used in response.
However, when same error is thrown in resolver its status code doesn't affect response.

This feels strange and is probably why other libraries don't use custom http status codes in response πŸ€”

Use in Typescript

Hey friends!

I currently use graphql-api-koa version 2.0.2 because it was the latest version when this project was started in 2019. I've been looking at updating to the latest graphql version which requires updating to the latest version of this package, but that is blocked because our project is 100% Typescript and graphql-api-koa is 100% .mjs, which Typescript currently can't use.

Do you have any suggestions or recommendations for using this project without copying the whole thing into my project and changing the filetypes from .mjs to .ts?

Better schema validation error output

I've trying this package out and I'm liking it

But one problem has arisen when using it, when I make a mistake in my graphql schema while in development, I should see what the error is, at least in the console, but what appears is just a generic error:
InternalServerError: GraphQL execute middleware `schema` option has GraphQL schema validation errors.
is there a configuration to show that error? thanks, right now I've put a console.error in node_modules/graphql-api-koa/lib/checkGraphQLSchema.js

The presence of errors should not discard data

Looking at this line I see that the presence of result.errors causes graphql-api-koa to drop anything in result.data. I am quite sure that this is incorrect behavior.

It's quite common for an error to happen in one subset of a query. In this case graphql-js bubbles it up to the first nullable ancestor, preserving all other trees. We probably don't want to throw out everything in this case.

Add information to docs on how to add additional fields in error

Jesus, I've spent so much time to find how to do it.

http-errors has possibility to add additional fields to error like this:

createError(503, 'No work on the sabbath.', { type: 'DayError' , expose: true })

However, formatError function cuts all additional properties and produces only message field. To add this additional error fields you have to use extensions property like this:

createError(503, 'No work on the sabbath.', { extensions: { type: 'DayError' }, expose: true })

This will correctly show additional error information to user:

image

Please, add this to docs somewhere. I really hope it will help and save time for someone else.

❀️

Add possibility to reformat error in errorHandler

It would be nice to be able to reformat error in errorHandler. It's useful when you need to modify error before sending to client, for example:

  • translate Internal server error message to other language
  • catch internal db error, expose it with a nice message

The api might be as simple as:

function formatError(error) {
  // do something with error
}

errorHandler(formatError)

I keep hitting this actually simple cases where I just want to format my errors before sending them to client and current errorHandler is too restrictive in what I can do. However, I'm trying to avoid writing custom middleware instead of errorHandler and struggling to find solutions.

@jaydenseric help πŸ™

Add format error functionality

Hey πŸ‘‹

This is a feature request for a function, similar to apollo formatError one.

It is very useful when you want to handle all errors in a single place, log them, etc. Handling this in resolvers across all app is tiresome.

If any equivalent or workaround exists, please suggest.

Upd: from twitter dm I found out that koa middlewares is current way of handling this. However, I find using formatError prop easier to undestand and use, maybe it's possible to add it to current api:

Proposed api screenshot

Screen Shot 2019-04-20 at 02 23 11

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.