This is a proof-of-concept to see if we can import apollo hooks straight from a .graphql
file
npm ci
npm run build-all
Running build-all
script does 2 things:
build-loaders
: This will build our loader calledapollo-hooks-loader
. This will be used in the next script.build-webpack
: This will compile the query, document and a react component intomain.js
that makes a query to https://graphql-pokemon.now.sh/ to get details of Pikachu!
Now, we can import the custom hook with document wired in like so:
import React from "react";
import { useGetPikachuQuery } from "./main.graphql";
function App() {
const { loading, error, data } = useGetPikachuQuery();
if (loading) {
return <div>Looking for pokemon...</div>;
}
if (error) {
return <div>Pokedex errror! {error}</div>;
}
return <div>{JSON.stringify(data)}</div>;
}
Open dist/index.html to see the result
- Custom hooks should be named correctly
- Check if the built file is sending multiple versions of apollo hooks to the client if there are multiple
.graphql
files?