Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GraphQL Better Errors

68139fbc12ae51c0a56cae151fc8c85f?s=47 ogom
October 13, 2019

GraphQL Better Errors

68139fbc12ae51c0a56cae151fc8c85f?s=128

ogom

October 13, 2019
Tweet

Transcript

  1. (SBQI2-#FUUFS&SSPST (SBQI2-0TBLB

  2. w (SBQI2-ͷΤϥʔͷϨεϙϯεͷϑΥʔϚοτ͸ܾ·ͬ ͍ͯ·͢ɻ w ͦͷ(SBQI2-ͷΤϥʔͷ࢓༷΍Τϥʔ͕ൃੜͨ͠ͱ͖ ͷϋϯυϦϯάΛ"QPMMP(SBQI2-΍(SBQI2-3VCZ ͳͲͷ࣮૷ྫͰ঺հ͠·͢ɻ

  3. (SBQI2-4QFD IUUQTHSBQIRMHJUIVCJPHSBQIRMTQFD

  4. Prerelease Working Draft Sat, Oct 5, 2019 Latest Release June

    2018 Sun, Jun 10, 2018 Release Notes October 2016 Mon, Oct 31, 2016 Release Notes April 2016 Thu, Apr 7, 2016 Release Notes October 2015 Thu, Oct 1, 2015 Release Notes July 2015 Thu, Jul 2, 2015 Release Notes GraphQL
  5. 3FTQPOTF'PSNBU { "data": { ... }, "errors": [ ... ]

    }
  6. 3FTQPOTF&SSPST Example No 189 { "errors": [ { "message": "Name

    for character with ID 1002 could not be fetched.", "locations": [ { "line": 6, "column": 7 } ], "path": [ "hero", "heroFriends", 1, "name" ], "extensions": { "code": "CAN_NOT_FETCH_BY_ID", "timestamp": "Fri Feb 9 14:33:09 UTC 2018" } } ] }
  7. "QPMMP4FSWFS IUUQTXXXBQPMMPHSBQIRMDPNEPDTBQPMMPTFSWFSEBUB

  8. &SSPS)BOEMJOH IUUQTXXXBQPMMPHSBQIRMDPNEPDTBQPMMPTFSWFSEBUBFSSPST "QPMMP4FSWFSQSPWJEFTBDPMMFDUJPOPG QSFEFpOFEFSSPST JODMVEJOH "VUIFOUJDBUJPO&SSPS 'PSCJEEFO&SSPS  6TFS*OQVU&SSPS BOEBHFOFSJD

    "QPMMP&SSPS
  9. "VUIFOUJDBUJPO&SSPS const { ApolloServer, gql, AuthenticationError } = require("apollo-server"); const

    typeDefs = gql` type Query { authenticationError: String } `; const resolvers = { Query: { authenticationError: (parent, args, context) => { throw new AuthenticationError("must authenticate"); } } };
  10. \ FSSPST< \ NFTTBHFNVTUBVUIFOUJDBUF  MPDBUJPOT< \ MJOF  DPMVNO

    ^ >  QBUI< BVUIFOUJDBUJPO&SSPS >  FYUFOTJPOT\ DPEF6/"65)&/5*$"5&%  ^ ^ >  EBUB\ BVUIFOUJDBUJPO&SSPSOVMM ^ ^
  11. "QPMMP$MJFOU IUUQTXXXBQPMMPHSBQIRMDPNEPDTSFBDU

  12. &SSPS)BOEMJOH IUUQTXXXBQPMMPHSBQIRMDPNEPDTSFBDUEBUBFSSPSIBOEMJOH • graphQLErrors: An array of errors from the

    GraphQL endpoint
  13. HSBQI2-&SSPST import { ApolloProvider, useQuery } from “@apollo/react-hooks"; function Element()

    { const { loading, error, data } = useQuery(gql` { authenticationError } `); if (loading) return <p>Loading...</p>; if (error) { return error.graphQLErrors.map(({ message, extensions }, i) => ( <div key={i}>{message}: {extensions.code}</div> )) } }
  14. NVTUBVUIFOUJDBUF6/"65)&/5*$"5&%

  15. (SBQI2-3VCZ IUUQTHSBQIRMSVCZPSHFSSPSTPWFSWJFX

  16. &SSPS)BOEMJOH IUUQTHSBQIRMSVCZPSHFSSPSTFSSPS@IBOEMJOH w  4FQU /FX'FBUVSFT w VTF(SBQI2-&YFDVUJPO&SSPSTQSPWJEFTFSSPS IBOEMJOHGPSUIFOFXJOUFSQSFUFS

  17. (SBQI2-&YFDVUJPO&SSPS class MySchema < GraphQL::Schema use GraphQL::Execution::Interpreter use GraphQL::Analysis::AST use

    GraphQL::Execution::Errors rescue_from(ActiveRecord::RecordNotFound) do |err, obj, args, ctx, field| GraphQL::ExecutionError.new( err.message, extensions: {code: ‘RecordNotFound'} ) end end
  18. { "data": null, "errors": [ { "message": "Couldn't find User",

    "locations": [ { "line": 2, "column": 3 } ], "path": [ "user" ], "extensions": { "code": "RecordNotFound" } } ] }
  19. $VTUPN4DBMBST IUUQTHSBQIRMSVCZPSHUZQF@EFpOJUJPOTTDBMBST w  "VH /FXGFBUVSFT w "EEFYUFOTJPOTJO$PFSDJPO&SSPS

  20. (SBQI2-$PFSDJPO&SSPS module Types class EmailAddress < Types::BaseScalar def self.coerce_input(value, _context)

    unless URI::MailTo::EMAIL_REGEXP.match(value) raise GraphQL::CoercionError.new( "#{value} is not a valid email address", extensions: { code: “Invalid_Email" } ) end value end end end
  21. { "errors": [ { "message": "example.com is not a valid

    email address", "locations": [ { "line": 2, "column": 3 } ], "path": [ "query", "user", "email" ], "extensions": { "code": "Invalid_Email", "typeName": "CoercionError" } } ] }
  22. (SBQI2-0TBLB