Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatArgumentValidationError causing typescript error #258

Closed
jansselt opened this issue Feb 22, 2019 · 18 comments
Closed

formatArgumentValidationError causing typescript error #258

jansselt opened this issue Feb 22, 2019 · 18 comments
Labels
Bug 🐛 Something isn't working Community 👨‍👧 Something initiated by a community Solved ✔️ The issue has been solved
Milestone

Comments

@jansselt
Copy link

Following along with @benawad fantastic tutorial. On the validation video.

I'm running into an issue when attempting to use formatArgumentValidationError.

import { ApolloServer } from 'apollo-server-express';
import Express from 'express';
import 'reflect-metadata';
import { buildSchema, formatArgumentValidationError } from 'type-graphql';
import { createConnection } from 'typeorm';

import { RegisterResolver } from './modules/user/Register';

const main = async () => {
  await createConnection();

  const schema = await buildSchema({
    resolvers: [RegisterResolver]
  });

  const apolloServer = new ApolloServer({ schema, formatError: formatArgumentValidationError });

  const app = Express();

  apolloServer.applyMiddleware({ app });

  app.listen(4000, () => {
    // tslint:disable-next-line:no-console
    console.log('Server started on http://localhost:4000/graphql');
  });
};

// tslint:disable-next-line:no-floating-promises
main(); 

This is resulting in the TS error below

$ ts-node-dev --respawn src/index.ts
Using ts-node version 8.0.2, typescript version 3.3.3333
[ERROR] 09:59:16 ⨯ Unable to compile TypeScript:
src/index.ts(16,51): error TS2322: Type '(err: GraphQLError) => { [key: string]: any; }' is not assignable to type '(error: GraphQLError) => GraphQLFormattedError'.
  Type '{ [key: string]: any; }' is missing the following properties from type 'GraphQLFormattedError': message, locations, path

I haven't seen anyone else complain of this, so it feels like an issue with my setup. But I cannot for the life of me figure out why I am getting it while no one else is.

@vfaramond
Copy link

Hi @jansselt, the typings of formatError have been updated on version 2.4.3 of apollo-server-express. Version 2.4.2 works 👍

@jansselt
Copy link
Author

Alright, will downgrade for now until this is updated to coincide. Thanks @vfaramond . Was going crazy.

@galkin
Copy link
Contributor

galkin commented Feb 23, 2019

@19majkel94, sorry for creating duplicate. Do we plan fix this issue?

@MichalLytek
Copy link
Owner

There is a lot of weird type changes in apollo-server right now:
apollographql/apollo-server#2346

I will wait for it to settle down and then re-investigate whether it really changed the signature or the API.

@vfaramond
Copy link

In the meantime, this is the custom implementation of formatError I use with the last version (2.4.6) of apollo-server-express:

formatError: (error: GraphQLError): GraphQLFormattedError => {
  if (error.originalError instanceof ApolloError) {
    return error;
  }

  if (error.originalError instanceof ArgumentValidationError) {
    const { extensions, locations, message, path } = error;

    error.extensions.code = 'GRAPHQL_VALIDATION_FAILED';

    return {
      extensions,
      locations,
      message,
      path,
    };
  }

  error.message = 'Internal Server Error';

  return error;
}

@MichalLytek
Copy link
Owner

Basically, looks like in new ApolloServer the formatArgumentValidationError is not needed anymore as the original error is reflected in extensions:

{
  "errors": [
    {
      "message": "Argument Validation Error",
      "locations": [
        {
          "line": 22,
          "column": 3
        }
      ],
      "path": [
        "addRecipe"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "validationErrors": [
            {
              "target": {
                "title": "Correct title",
                "description": "Too short description"
              },
              "value": "Too short description",
              "property": "description",
              "children": [],
              "constraints": {
                "length": "description must be longer than or equal to 30 characters"
              }
            }
          ],
          "stacktrace": [
            "Error: Argument Validation Error",
            "    at Object.<anonymous> (F:\\#Projekty\\type-graphql\\src\\resolvers\\validate-arg.ts:29:11)",
            "    at Generator.throw (<anonymous>)",
            "    at rejected (F:\\#Projekty\\type-graphql\\node_modules\\tslib\\tslib.js:105:69)",
            "    at processTicksAndRejections (internal/process/next_tick.js:81:5)"
          ]
        }
      }
    }
  ],
  "data": null
}

@MichalLytek MichalLytek added Bug 🐛 Something isn't working Community 👨‍👧 Something initiated by a community labels Feb 23, 2019
@MichalLytek MichalLytek added this to the 1.0.0 release milestone Feb 23, 2019
@MichalLytek
Copy link
Owner

I've tested that so I will remove the formatArgumentValidationError at all as it's not needed anymore. The first version of ApolloServer, as well as GraphQLYoga, stripped off additional properties, so I had to provide a format helper for that:

{
  "errors": [
    {
      "message": "Argument Validation Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": ["addRecipe"]
    }
  ],
  "data": null
}

@MichalLytek MichalLytek reopened this Feb 23, 2019
@MichalLytek
Copy link
Owner

Fixed via 526886c 🔒

Released in 0.17.0-beta.9 🎉

@MichalLytek MichalLytek added the Solved ✔️ The issue has been solved label Feb 23, 2019
@MichalKalita
Copy link

It probably doesn't works for validation in nested objects. 😞

@MichalLytek
Copy link
Owner

@Myiyk See #133 - nested inputs are not an instance, so they can't be validated using class-validator. It's not a format error fault.

@MichalLytek
Copy link
Owner

@git-no
Please read this thread once more - this helper was removed.

@venatir
Copy link

venatir commented Feb 12, 2020

You can't simply remove functionality. This is a breaking change.
Please either maintain compatibility with previous minor versions or bump the major.

@MichalLytek
Copy link
Owner

MichalLytek commented Feb 12, 2020

@venatir Please check twice before starting to complain:

opera_2020-02-12_20-02-39-2

It was clearly released as the new major version with a "breaking change" notice 😠

@galkin
Copy link
Contributor

galkin commented Feb 12, 2020

@venatir, hello my friend.

The package is released as 0.x version, so minor version can have breaking changes. Proof

@MichalLytek
Copy link
Owner

MichalLytek commented Feb 12, 2020

@galkin Oh, you think it's the source of confusion.

Basically major 0 release has a different semver:
0.MAJOR.MINOR
so there is no patch releases.

image

At least that's the way npm handles deps 😉

@venatir
Copy link

venatir commented Feb 12, 2020

My bad. Was aware of the above (0 Major different behaviour), but I read the version from a different package (apollo-server-core) and somehow was convinced this was v2.10.0 :) Please ignore me :) Either way it's great too see you guys are so active and answer this fast. Great maintenance work!

@ali26Y
Copy link

ali26Y commented Dec 8, 2020

In the meantime, this is the custom implementation of formatError I use with the last version (2.4.6) of apollo-server-express:

formatError: (error: GraphQLError): GraphQLFormattedError => {
  if (error.originalError instanceof ApolloError) {
    return error;
  }

  if (error.originalError instanceof ArgumentValidationError) {
    const { extensions, locations, message, path } = error;

    error.extensions.code = 'GRAPHQL_VALIDATION_FAILED';

    return {
      extensions,
      locations,
      message,
      path,
    };
  }

  error.message = 'Internal Server Error';

  return error;
}

I can confirm this works however add the following lines to your code:

import {  GraphQLError, GraphQLFormattedError } from 'graphql';
import { ApolloError } from 'apollo-server-express';
import { ArgumentValidationError } from 'type-graphql';

....ts

if (error && error.extensions) {
        error.extensions.code = 'GRAPHQL_VALIDATION_FAILED';
    }

....

return error;

@Adamkaram
Copy link

Adamkaram commented Jan 6, 2021

for newcommer from this toutorial u van add this

import {  GraphQLError, GraphQLFormattedError } from 'graphql';
import { ApolloError } from 'apollo-server-express';
import { ArgumentValidationError } from 'type-graphql';


  const apolloserver = new ApolloServer({
    schema, formatError: (error: GraphQLError): GraphQLFormattedError => {
      if (error && error.extensions) {
        error.extensions.code = 'GRAPHQL_VALIDATION_FAILED';
      }
      return error;
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 Something isn't working Community 👨‍👧 Something initiated by a community Solved ✔️ The issue has been solved
Projects
None yet
Development

No branches or pull requests

8 participants