This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const typeDefs = gql` | |
| # define wrapper result type that could return either | |
| # the added Event on success, or an array of Errors | |
| # on error. Notice both fields are nullable. | |
| type AddEventResult { | |
| event: Event | |
| validationErrors: [FieldValidationError!] | |
| } | |
| # define how you want errors to be represented. This |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const typeDefs = gql` | |
| type Event { | |
| name: String! | |
| date: Date! | |
| capacity: Int! | |
| zipCode: String! | |
| } | |
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ApolloClient from 'apollo-boost'; | |
| const client = new ApolloClient({ | |
| uri: '<your graphql endpoint>', | |
| // Apollo Boost allows you to specify a custom error link for your client | |
| onError: ({ graphQLErrors, networkError, operation, forward }) => { | |
| if (graphQLErrors) { | |
| for (let err of graphQLErrors) { | |
| // handle errors differently based on its error code | |
| switch (err.extensions.code) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "message": "Failed to get events due to validation errors", | |
| "extensions": { | |
| "code": "BAD_USER_INPUT", | |
| "exception": { | |
| "validationErrors": { | |
| "zipCode": "This is not a valid zipcode" | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { UserInputError } from 'apollo-server'; | |
| const resolvers = { | |
| Query: { | |
| events(root, { zipCode }) { | |
| // do custom validation for user inputs | |
| const validationErrors = {}; | |
| if (!isValidZipCode(zipCode)) { | |
| validationErrors.zipCode = 'This is not a valid zipcode'; |
OlderNewer