Skip to content

Conversation

@EmrysMyrddin
Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin commented Apr 27, 2023

Description

This PR aims to allow the customization of errors in Persisted Operations plugin.

Issue

fixes #2047

Concerns

I have implemented multiple ways to customize errors with polymorphic options. I'm not sure if this wanted, since it seems this pattern is not heavily used in the code base. But I think it can be a good DX, since the most general use-case will probably to add a static extensions such as HTTP status.

Don't hesitate If you have suggestion about option names, I'm not totally satisfied.

I also give access to the payload of the onParams function to the error factory. Perhaps I should filter to avoid passing setters and only give access to params and request ?

Usage example

import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
import { usePersistedOperations } from '@graphql-yoga/plugin-persisted-operations'
import { CustomErrorClass } from './custom-error-class'

const yoga = createYoga({
  plugins: [
    usePersistedOperations({
      customErrors: {
        // You can change the error message
        notFound: 'Not Found',
        // Or customize the error with a GraphqlError options object, allowing you to add extensions
        keyNotFound: {
          message: 'Key Not Found',
          extensions: {
            http: {
              status: 404
            }
          }
        },
        // Or customize with a factory function allowing you to use your own error class or format
        persistedQueryOnly: () => {
          return new CustomErrorClass('Only Persisted Operations are allowed')
        }
      }
    })
  ]
})

const server = createServer(yoga)
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

@changeset-bot
Copy link

changeset-bot bot commented Apr 27, 2023

🦋 Changeset detected

Latest commit: 88e090b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-yoga/plugin-persisted-operations Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@EmrysMyrddin EmrysMyrddin marked this pull request as ready for review April 27, 2023 21:35
@ardatan
Copy link
Member

ardatan commented May 8, 2023

Looks good! Thanks @EmrysMyrddin !

@ardatan ardatan merged commit e6aa0dd into graphql-hive:main May 8, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2023

✅ Benchmark Results

     ✓ no_errors{mode:graphql}
     ✓ expected_result{mode:graphql}
     ✓ no_errors{mode:graphql-jit}
     ✓ expected_result{mode:graphql-jit}
     ✓ no_errors{mode:graphql-response-cache}
     ✓ expected_result{mode:graphql-response-cache}
     ✓ no_errors{mode:graphql-no-parse-validate-cache}
     ✓ expected_result{mode:graphql-no-parse-validate-cache}

     checks.......................................: 100.00% ✓ 366284     ✗ 0     
     data_received................................: 1.5 GB  13 MB/s
     data_sent....................................: 74 MB   616 kB/s
     http_req_blocked.............................: avg=1.34µs   min=899ns    med=1.2µs    max=712.89µs p(90)=1.7µs    p(95)=1.9µs   
     http_req_connecting..........................: avg=4ns      min=0s       med=0s       max=556.09µs p(90)=0s       p(95)=0s      
     http_req_duration............................: avg=428.16µs min=216.19µs med=336.39µs max=27.52ms  p(90)=619.79µs p(95)=650.99µs
       { expected_response:true }.................: avg=428.16µs min=216.19µs med=336.39µs max=27.52ms  p(90)=619.79µs p(95)=650.99µs
     ✓ { mode:graphql-jit }.......................: avg=318.71µs min=216.19µs med=275.79µs max=21.65ms  p(90)=332.19µs p(95)=366.49µs
     ✓ { mode:graphql-no-parse-validate-cache }...: avg=670.19µs min=543.99µs med=616.89µs max=27.52ms  p(90)=692.39µs p(95)=754.09µs
     ✓ { mode:graphql-response-cache }............: avg=352.88µs min=253.39µs med=315.29µs max=12.35ms  p(90)=357.79µs p(95)=397.19µs
     ✓ { mode:graphql }...........................: avg=469.8µs  min=341.99µs med=402.69µs max=26.14ms  p(90)=481.59µs p(95)=545.34µs
     http_req_failed..............................: 0.00%   ✓ 0          ✗ 183142
     http_req_receiving...........................: avg=24.25µs  min=14.1µs   med=20.7µs   max=8.27ms   p(90)=31.1µs   p(95)=35.4µs  
     http_req_sending.............................: avg=10.11µs  min=5.2µs    med=6.3µs    max=5.12ms   p(90)=10.6µs   p(95)=12.4µs  
     http_req_tls_handshaking.....................: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting.............................: avg=393.79µs min=193.59µs med=300.89µs max=27.48ms  p(90)=592.09µs p(95)=618.29µs
     http_reqs....................................: 183142  1526.16338/s
     iteration_duration...........................: avg=650.5µs  min=404.09µs med=561.09µs max=27.76ms  p(90)=830.19µs p(95)=881.89µs
     iterations...................................: 183142  1526.16338/s
     vus..........................................: 1       min=1        max=1   
     vus_max......................................: 2       min=2        max=2   

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

Successfully merging this pull request may close these issues.

[v3] Persisted operations - how can I customize the not found error

2 participants