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 React from 'react'; | |
| import { render } from 'react-dom'; | |
| import ApolloClient from 'apollo-boost'; | |
| import { ApolloProvider } from 'react-apollo'; | |
| // Pass your GraphQL endpoint to uri | |
| const client = new ApolloClient({ | |
| uri: 'https://nx9zvp49q7.lp.gql.zone/graphql' | |
| }); |
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
| { | |
| "errors": [{ | |
| "message": "Something went wrong", | |
| "locations": [ | |
| { | |
| "line": 3, | |
| "column": 5 | |
| } | |
| ], | |
| }] |
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 { AuthenticationError } from 'apollo-server' | |
| const resolvers = { | |
| Mutation: { | |
| protectedAction(root, args , { user }) { | |
| if (!user) { | |
| throw new AuthenticationError('You must be logged in'); | |
| } | |
| } | |
| } | |
| }; |
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
| { | |
| "error": { | |
| "graphQLErrors": [ | |
| { | |
| "message": "forbidden", | |
| "locations": [], | |
| "path": [ | |
| "protectedAction" | |
| ], | |
| "extensions": { |
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 { onError } from 'apollo-link-error'; | |
| const ReauthenticationLink = onError( | |
| ({ graphQLErrors, networkError, operation, forward }) => { | |
| if (graphQLErrors) { | |
| for (let err of graphQLErrors) { | |
| // handle errors differently based on its error code | |
| switch (err.extensions.code) { | |
| case 'UNAUTHENTICATED': | |
| // old token has expired throwing AuthenticationError, | |
| // one way to handle is to obtain a new token and |
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-client'; | |
| import { InMemoryCache } from 'apollo-cache-inmemory'; | |
| import { HttpLink } from 'apollo-link-http'; | |
| const client = new ApolloClient({ | |
| link: ApolloLink.from([ | |
| // other links go here | |
| ReauthenticatonLink, | |
| new HttpLink({ | |
| uri: 'http://graphql-endpoint:4000', | |
| }), |
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 MovieSearchResult { | |
| movies: [Movie] | |
| recommendedForYou: [Movie] | |
| } | |
| type Query { | |
| searchMovies(keyword: String!): MovieSearchResult! | |
| } | |
| ` |
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 React from 'react'; | |
| import { Query } from 'react-apollo'; | |
| const MovieSearchResults = ({ keyword }) => ( | |
| <Query query={SEARCH_MOVIES} variables={{ keyword }}> | |
| {(data, loading, error) => { | |
| if (loading) return <LoadingIndicator />; | |
| // if networkError is present, we can be sure that no data | |
| // was returned. We can simply display an error component. | |
| if (error && error.networkError) return <ErrorDisplay />; |
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 MovieSearchResult { | |
| movies: [Movie!]! | |
| recommendedForYou: [Movie!] | |
| } | |
| ... | |
| ` | |
| // Note: since it makes no sense that we have a null | |
| // Movie returned in the list, we should mark each Movie | |
| // as non-nullable too. |
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
| { | |
| "data": { | |
| "searchMovies": { | |
| "movies": [ | |
| { | |
| "name": "Avengers Infinity War", | |
| } | |
| ], | |
| "recommendedForYou": null, | |
| } |
OlderNewer