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

Custom graphql query error response not correct #10468

Open
gor3a opened this issue Jan 9, 2025 · 2 comments
Open

Custom graphql query error response not correct #10468

gor3a opened this issue Jan 9, 2025 · 2 comments
Labels
status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@gor3a
Copy link

gor3a commented Jan 9, 2025

Describe the Bug

Hey
I written a custom graphql queries and when try to fire an error from the resolver the error I get is the standrad one from payloadcms

My queries file:

const queries = (
  graphQL: typeof GraphQL,
  payload: {
    config: SanitizedConfig
  } & GraphQLInfo,
) => {
  return {
    myTemplateQuery: myTemplateQuery(graphQL, payload),
  }
}

myTemplateQuery query:

import { GraphQL } from '@payloadcms/graphql/types'
import myTemplateQueryResolver from './resolver'
import { GraphQLInfo, SanitizedConfig } from 'payload'

const myTemplateQuery = (
  graphQL: typeof GraphQL,
  payload: {
    config: SanitizedConfig
  } & GraphQLInfo,
) => {
  return {
    type: new graphQL.GraphQLObjectType({
      name: 'MyTemplateQuery',
      fields: {
        ID: {
          type: graphQL.GraphQLString,
        },
        text: {
          type: graphQL.GraphQLString,
        },
        Nice: {
          type: graphQL.GraphQLString,
        },
        someNumberField: {
          type: graphQL.GraphQLFloat,
        },
      },
    }),
    args: {
      ID: {
        type: new graphQL.GraphQLNonNull(graphQL.GraphQLID),
      },
    },
    resolve: myTemplateQueryResolver(graphQL, payload),
  }
}
export default myTemplateQuery

myTemplateQueryResolver the resolver:

import { GraphQL } from '@payloadcms/graphql/types'
import { GraphQLInfo, SanitizedConfig } from 'payload'
import handleGraphQLError from '@/graphql/utils/handleGraphQLError'

const myTemplateQueryResolver =
  (
    graphQL: typeof GraphQL,
    payload: {
      config: SanitizedConfig
    } & GraphQLInfo,
  ) =>
  async (parent: any, args: any, context: any, info: any) => {
    handleGraphQLError(graphQL, payload, 'You are not authorized to perform this action', 401)
    return {
      ID: args.ID,
      text: 'Hello, world This is a template!',
      someNumberField: 123,
      Nice: 'Nice that it works!',
    }
  }
export default myTemplateQueryResolver

handleGraphQLError function that manage to fire the error message and status code:

import { GraphQL } from '@payloadcms/graphql/types'
import { GraphQLInfo, SanitizedConfig } from 'payload'

const handleGraphQLError = (
  graphQL: typeof GraphQL,
  payload: {
    config: SanitizedConfig
  } & GraphQLInfo,
  message: string,
  statusCode: number,
) => {
  console.error('Error Message: ', message, 'status: ', statusCode)
  throw new graphQL.GraphQLError(message, {
    extensions: {
      name: 'GraphQLError',
      statusCode,
    },
  })
}

export default handleGraphQLError

The response I got in postman

{
    "errors": [
        {
            "extensions": {
                "name": "GraphQLError",
                "statusCode": 500
            },
            "locations": [
                {
                    "line": 3,
                    "column": 5
                }
            ],
            "message": "Something went wrong.",
            "path": [
                "myTemplateQuery"
            ]
        }
    ],
    "data": {
        "myTemplateQuery": null
    }
}

I didn't get message You are not authorized to perform this action or status code 401 that I want

Link to the code that reproduces this issue

pnpx create-payload-app@latest -t blank

Reproduction Steps

  • Install PayloadCMS blank
  • Add custom graphql query to payload.config.ts
  • Create a test query and resolver
  • Try to fire an error on response

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Binaries:
  Node: 20.18.1
  npm: 11.0.0
  Yarn: 1.22.22
  pnpm: 9.1.4
Relevant Packages:
  payload: 3.11.0
  next: 15.1.1
  @payloadcms/db-mongodb: 3.11.0
  @payloadcms/db-postgres: 3.11.0
  @payloadcms/email-nodemailer: 3.11.0
  @payloadcms/graphql: 3.11.0
  @payloadcms/next/utilities: 3.11.0
  @payloadcms/payload-cloud: 3.11.0
  @payloadcms/richtext-lexical: 3.11.0
  @payloadcms/translations: 3.11.0
  @payloadcms/ui/shared: 3.11.0
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:02:27 PDT 2024; root:xnu-11215.41.3~2/RELEASE_X86_64
  Available memory (MB): 32768
  Available CPU cores: 12
@gor3a gor3a added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Jan 9, 2025
Copy link
Contributor

github-actions bot commented Jan 9, 2025

Please add a reproduction in order for us to be able to investigate.

Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.

Why was this issue marked with the invalid-reproduction label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with create-payload-app@beta -t blank or a forked/branched version of this repository with tests added (more info in the reproduction-guide).

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.

Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

Useful Resources

@amrsaud
Copy link

amrsaud commented Jan 13, 2025

I hope this problem will be solved ASAP because it's really a big problem since we can't know what is the error @DracoBlue @denolfe @colinramsay @jmas , thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

2 participants