dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.83k stars 1.33k forks source link

Improve errors output from codegen cli #5064

Closed xenoterracide closed 2 years ago

xenoterracide commented 3 years ago

this is what I'm running

    "codegen": "DEBUG=1 graphql-codegen --config server.codegen.yml && graphql-codegen --config client.codegen.yml",
> yarn graph codegen                                                                          # services -> feature/RS2-1286 $ !
➤ YN0000: [@bb/graph]: [11:31:09] Parse configuration [started]
➤ YN0000: [@bb/graph]: [11:31:09] Parse configuration [completed]
➤ YN0000: [@bb/graph]: [11:31:09] Generate outputs [started]
➤ YN0000: [@bb/graph]: [11:31:09] Generate src/generated/schema.ts [started]
➤ YN0000: [@bb/graph]: [11:31:09] Generate src/generated/schema.graphql [started]
➤ YN0000: [@bb/graph]: [11:31:09] Load GraphQL schemas [started]
➤ YN0000: [@bb/graph]: [11:31:09] Load GraphQL schemas [started]
➤ YN0000: [@bb/graph]: [11:31:09] Load GraphQL schemas [failed]
➤ YN0000: [@bb/graph]: [11:31:09] → Failed to load schema
➤ YN0000: [@bb/graph]: [11:31:09] Generate src/generated/schema.ts [failed]
➤ YN0000: [@bb/graph]: [11:31:09] → Failed to load schema
➤ YN0000: [@bb/graph]: [11:31:09] Load GraphQL schemas [failed]
➤ YN0000: [@bb/graph]: [11:31:09] → Failed to load schema
➤ YN0000: [@bb/graph]: [11:31:09] Generate src/generated/schema.graphql [failed]
➤ YN0000: [@bb/graph]: [11:31:09] → Failed to load schema
➤ YN0000: [@bb/graph]: [11:31:09] Generate outputs [failed]
➤ YN0000: [@bb/graph]: Something went wrong
➤ YN0000: The command failed for workspaces that are depended upon by other workspaces; can't satisfy the dependency graph
➤ YN0000: Failed with errors in 2s 819ms
>                                                                                        # services -> feature/RS2-1286 $ ! RC=1

but what is it failing on? what was it looking at? please improve the failure outputs to include what was being processed at the time. Here's the schema I've been working on

type PaymentTerminal {
  id: ID!
}
type CreateStripeConnectionTokenPayload {
  token: String!
}

extend type Mutation {
  """
  Creates a Stripe Connection Token to be used to activate a Stripe Terminal reader.
  """
  createStripeConnectionToken: CreateStripeConnectionTokenPayload!
}

type StripeClientSecret {
  clientSecret: String
}

"""
Describes an Authorization entity, which represents a payment authorized by a payment processor.
"""
type PaymentAuthorization {
  createdAt: DateTime!
  code: String!
  returns: StripeClientSecret
}

"""
Describes possible values for PaymentMethodType.
"""
enum PaymentMethodType {
  Card
}

enum PaymentProcessorType {
  Stripe
}

enum PaymentTransactionType {
  CardPresent
  CardToken
}
"""
id is the card token or any other lookup identifier to be determined by the PaymentMethodType and the TransactionType
"""
input CardPaymentMethodInput {
  type: PaymentMethodType!
  transactionType: TransactionType!
  id: ID
}

input AuthorizeCardPaymentInput {
  processor: PaymentProcessorType!
  amount: Int!
  currency: Currency!
  storefrontId: ID!
  terminalId: ID
  paymentMethod: CardPaymentMethodInput!
}

extend type Mutation {
  """
  Pre-authorizes a payment.
  """
  authorizeCardPayment(payment: AuthorizeCardPaymentInput!): PaymentAuthorization!
}

"""
Describes an Authorization entity, which represents a payment authorized by a payment processor.
"""
type Authorization {
  createdAt: DateTime!
  code: String!
  returns: StripeClientSecret
}

"""
Describes possible values for PaymentMethodType.
"""
enum PaymentMethodType {
  CardPresent
}

input PaymentMethodInput {
  type: PaymentMethodType!
}

input PaymentInput {
  amount: Int!
  currency: Currency!
  storefrontId: ID!
  terminalId: ID
  paymentMethod: PaymentMethodInput!
}

"""
Describes reasons for payment rejection by a payment processor.
"""
type PaymentRejectionReason {
  message: String
  code: String
}

extend type Mutation {
  """
  @deprecated use `authorizeCardPayment` instead
  """
  preAuthorize(payment: PaymentInput!): Authorization! @deprecated
}

DEBUG is a decent solution, but I need this #4946, also I think it could be just reduced to

    '        Failed to load schema from src/**/*.schema.graphql:\n' +
    '\n' +
    '        Type "TransactionType" not found in document.\n' 
dotansimha commented 3 years ago

@xenoterracide the error should be visible. Are you running this as a script from a package manager? (seems like Yarn 2?) can you please share a reproduction of that issue in a sandbox?

thanks!

xenoterracide commented 3 years ago

yes, yarn2, nodelinking, I'll see what I can do sometime this week

dotansimha commented 3 years ago

Got it, thanks. Can you please share a reproduction with the complete set of tooling you are using?

TheAschr commented 3 years ago

@dotansimha I am also getting this error. Here is a reproduction:

https://github.com/TheAschr/graphql-codegen-errors-reproduction

VincentAudibert commented 3 years ago

As a local & temporary workaround, I added a print of the document errors, here is the kind of helpful output I got this way :

Error [GraphQLDocumentError]: Variable "$serviceId" of type "String!" used in position expecting type "ID!". at [...]/src/graphql/service-operation-mutations.graphql:3:3

I put errors.forEach(console.log); in file node_modules/@graghql-tools/utils/index.js, line 960 as of version 8.0.2.

function checkValidationErrors(loadDocumentErrors) {
    if (loadDocumentErrors.length > 0) {
        const errors = [];
        for (const loadDocumentError of loadDocumentErrors) {
            ...
        }
        errors.forEach(console.log); // ADDED THIS UGLY THING
        throw new exports.AggregateError(errors, `GraphQL Document Validation failed with ${loadDocumentErrors.length} errors`);
    }
}

I'd be happy to fill a PR if given some insight on logging strategy of codegen !

VincentAudibert commented 3 years ago

In fact, since version 8.2.0 (see commit), the error message are much nicely built:

throw new AggregateError(
  errors,
  `GraphQL Document Validation failed with ${errors.length} errors;
  ${errors.map((error, index) => `Error ${index}: ${error.stack}`).join('\n\n')}`
);

@dotansimha Issue should be closed as on graphql-tools side & resolved.

jlmessenger commented 2 years ago

I found that listr was encapsulating errors in some cases. This PR unpacks those errors, and includes those details in the CLI error output.

https://github.com/dotansimha/graphql-code-generator/pull/6921

charlypoly commented 2 years ago

Hi all!

CLI errors output has been improved in @graphql-codegen/cli@2.5.0!

Thank you @jlmessenger for your contribution ⚡