Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Improve apollo-client example
Browse files Browse the repository at this point in the history
  • Loading branch information
timkendall committed Jun 24, 2021
1 parent aaf938c commit bfbce55
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 134 deletions.
40 changes: 22 additions & 18 deletions examples/apollo-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloClient, InMemoryCache } from "@apollo/client";
import fetch from "node-fetch";
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client";

import { Result } from "../../src";
import {
Expand All @@ -8,25 +9,28 @@ import {
Episode,
} from "../../__tests__/starwars/starwars.sdk";

const client = new ApolloClient({ cache: new InMemoryCache() });
(async () => {
const client = new ApolloClient({
link: new HttpLink({ uri: "http://localhost:8080/graphql", fetch }),
cache: new InMemoryCache(),
});

const example = query("Apollo Example", (t) => [
t.character({ id: "foo" }, (t) => [t.id(), t.friends((t) => [t.id()])]),
const example = query("ApolloExample", (t) => [
t.character({ id: "foo" }, (t) => [t.id(), t.friends((t) => [t.id()])]),

t.starship({ id: "foo" }, (t) => [
t.id(),
t.name(),
t.length({ unit: LengthUnit.FOOT }),
t.coordinates(),
]),
t.starship({ id: "foo" }, (t) => [
t.id(),
t.name(),
t.length({ unit: LengthUnit.FOOT }),
t.coordinates(),
]),

t.reviews({ episode: Episode.JEDI }, (t) => [t.commentary()]),
]);
t.reviews({ episode: Episode.JEDI }, (t) => [t.commentary()]),
]);

const apolloQuery = client.query<
Result<IQuery, typeof example["selectionSet"]>
>({ query: example.toDocument() });
const result = await client.query<
Result<IQuery, typeof example["selectionSet"]>
>({ query: example.toDocument() });

apolloQuery.then((result) => {
console.log(result.data.starship?.name);
});
console.log(`Found Starship "${result.data.starship?.name}"!`);
})();
Loading

0 comments on commit bfbce55

Please sign in to comment.