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

How to reproduce NOT_FOUND error with context.mirageSchema.findBy() #78

Open
rgaiacs opened this issue Apr 17, 2024 · 2 comments
Open

Comments

@rgaiacs
Copy link
Contributor

rgaiacs commented Apr 17, 2024

Hi,

I have a custom resolver (like https://github.com/miragejs/graphql?tab=readme-ov-file#example-deleting-a-person). For a minimal example, consider that the database table person is empty and the result of

const person = context.mirageSchema.db.people.find(1)

is person be null. How do I create a NOT_FOUND error? Currently, Mirage JS GraphQL will return null but no error.

I would appreciate any help to reproduce the follow query to GitHub GraphQL API

Screenshot 2024-04-17 at 16-14-56 Explorer - GitHub Docs

@jneurock
Copy link
Collaborator

You can return or throw an error from a resolver. I don’t know exactly how GitHub is adding the "type" field to their errors but you can do something similar by using GraphQL’s built-in error class to create an error with some extra information in the response.

For example:

// From your resolver...

return new GraphQLError("Person not found", {
  extensions: {
    type: "NOT_FOUND"
  }
});

Your response will look something like this:

{
  "errors": [
    {
      "message": "Person not found",
      "locations": [
        {
          "line": 1,
          "column": 21
        }
      ],
      "path": [
        "person"
      ],
      "extensions": {
        "type": "NOT_FOUND"
      }
    }
  ],
  "data": {
    "person": null
  }
}

@rgaiacs
Copy link
Contributor Author

rgaiacs commented Apr 18, 2024

Thanks for the answer. Next week, I will submit a pull request to the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants