Created
October 3, 2022 05:18
-
-
Save DilLip-Chowdary-Codes/8a5fbe99ada4ad5cd3328e7cc6ebc366 to your computer and use it in GitHub Desktop.
Sample GraphQL App.
This file contains 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 { useQuery, gql } from "@apollo/client"; | |
const GET_LOCATIONS = gql` | |
query getLocations { | |
locations { | |
id | |
name | |
description | |
photo | |
} | |
} | |
`; | |
function DisplayLocations() { | |
const { loading, error, data } = useQuery(GET_LOCATIONS); | |
if (loading) return <p>Loading...</p>; | |
if (error) return <p>Error :(</p>; | |
return data.locations.map(({ id, name, description, photo }) => ( | |
<div key={id}> | |
<h3>{name}</h3> | |
<img width="400" height="250" alt="location-reference" src={`${photo}`} /> | |
<br /> | |
<b>About this location:</b> | |
<p>{description}</p> | |
<br /> | |
</div> | |
)); | |
} | |
export default function App() { | |
return ( | |
<div> | |
<h2> | |
My first Apollo app | |
<span role="img" aria-label="rocket"> | |
🚀 | |
</span> | |
</h2> | |
<br /> | |
<DisplayLocations /> | |
</div> | |
); | |
} |
This file contains 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 ReactDOM from "react-dom/client"; | |
import "./index.css"; | |
import App from "./App"; | |
import reportWebVitals from "./reportWebVitals"; | |
import { | |
ApolloClient, | |
InMemoryCache, | |
ApolloProvider, | |
gql, | |
} from "@apollo/client"; | |
const root = ReactDOM.createRoot(document.getElementById("root")); | |
const client = new ApolloClient({ | |
uri: "http://localhost:4444/graphql/", | |
cache: new InMemoryCache() | |
}); | |
// const client = ... | |
client | |
.query({ | |
query: gql` | |
query GetLocations { | |
locations { | |
id | |
name | |
description | |
photo | |
} | |
} | |
`, | |
}) | |
.then((result) => console.log(result)); | |
root.render( | |
<ApolloProvider client={client}> | |
<App /> | |
</ApolloProvider> | |
); | |
// If you want to start measuring performance in your app, pass a function | |
// to log results (for example: reportWebVitals(console.log)) | |
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals | |
reportWebVitals(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment