-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(magidoc): add custom pages and appLogo [skip publish]
- Loading branch information
Showing
9 changed files
with
347 additions
and
95 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ coverage/ | |
api/ | ||
dist/ | ||
codegen/ | ||
docs/ | ||
docs/generated-docs/ | ||
|
||
## Scripts | ||
scripts/tiers.ts | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* MIT License | ||
* Copyright (c) 2022 Sunny Pelletier | ||
*/ | ||
|
||
import { readdir, readFile, stat } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
/** | ||
* Scans the directory for files and collects them | ||
* @param {import('node:fs').PathLike} dir The directory to scan the files from | ||
* @returns {Promise<Array<{ dir: boolean; name: string; path: string; }>>} The files in the directory | ||
*/ | ||
async function getFiles(dir) { | ||
/** | ||
* @type Array<{ dir: boolean; name: string; path: string; }> | ||
*/ | ||
const result = []; | ||
const dirAsPathString = dir instanceof URL ? fileURLToPath(dir) : dir; | ||
|
||
const dirContents = await readdir(dir); | ||
|
||
for (const item of dirContents) { | ||
const currentPath = join(dirAsPathString, item); | ||
|
||
const currentPathStat = await stat(currentPath); | ||
|
||
result.push({ | ||
dir: currentPathStat.isDirectory(), | ||
name: item, | ||
path: currentPath | ||
}); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Get all items as page objects | ||
* @param {{ dir: boolean; name: string; path: string; }} item The item to parse | ||
* @returns {{ title: string; content: any }} Returns the item as a Page | ||
*/ | ||
async function asPage(item) { | ||
const title = item.name.split('.')[1]; | ||
|
||
if (item.dir) { | ||
const filesInDirectory = await getFiles(item.path); | ||
|
||
return { | ||
title, | ||
content: await Promise.all(filesInDirectory.map((item) => asPage(item))) | ||
}; | ||
} | ||
|
||
return { | ||
title, | ||
content: await readFile(item.path, { encoding: 'utf-8' }) | ||
}; | ||
} | ||
|
||
const pagesDirectory = new URL('./pages', import.meta.url); | ||
|
||
const filesInPagesDirectory = await getFiles(pagesDirectory); | ||
|
||
export const pages = await Promise.all(filesInPagesDirectory.map((item) => asPage(item))); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Welcome | ||
|
||
Welcome to the `graphql-pokemon` project! | ||
|
||
This projects contains a GraphQL API for retrieving information about Pokémon. | ||
The API allows users to query for information about Pokémon, including their | ||
types, abilities, and moves. The data for the Pokémon is sourced from the | ||
popular video game series, provided by external sources such as | ||
[Smogon](https://www.smogon.com), [Serebii](https://serebii.net), and | ||
[Bulbapedia](https://bulbapedia.bulbagarden.net). | ||
|
||
The easiest way to get to know this API is to try it out in the | ||
[playground](https://graphqlpokemon.favware.tech/v7). | ||
|
||
For implementing the API in your own code, choose your preferred language and | ||
method of making Web requests. Beyond that point you will need to learn the | ||
GraphQL syntax, and the documentation on this website as well as the queries | ||
provided by the [playground](https://graphqlpokemon.favware.tech/v7) will help | ||
you get started. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# JavaScript Examples | ||
|
||
_These examples are written as based on TypeScript. For JavaScript simply change | ||
out the imports to `require` syntax and remove any type information._ | ||
|
||
## Using `Fetch` | ||
|
||
_note: for a working example see [dragonite]_ | ||
|
||
```ts | ||
import type { Query } from '@favware/graphql-pokemon'; | ||
|
||
interface GraphQLPokemonResponse<K extends keyof Omit<Query, '__typename'>> { | ||
data: Record<K, Omit<Query[K], '__typename'>>; | ||
} | ||
|
||
fetch('https://graphqlpokemon.favware.tech/v7', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
query: ` | ||
{ | ||
getPokemon(pokemon: dragonite) { | ||
sprite | ||
num | ||
species | ||
color | ||
} | ||
} | ||
` | ||
}) | ||
}) | ||
.then((res) => res.json() as Promise<GraphQLPokemonResponse<'getPokemon'>>) | ||
.then((json) => console.log(json.data)); | ||
``` | ||
|
||
## Using `Apollo Boost` | ||
|
||
_note: for a working example see [dexa]_ | ||
|
||
```ts | ||
import type { Query, QueryGetFuzzyPokemonArgs } from '@favware/graphql-pokemon'; | ||
import ApolloClient from 'apollo-boost'; | ||
import fetch from 'cross-fetch'; | ||
import gql from 'graphql-tag'; | ||
|
||
type GraphQLPokemonResponse<K extends keyof Omit<Query, '__typename'>> = Record< | ||
K, | ||
Omit<Query[K], '__typename'> | ||
>; | ||
|
||
const getFuzzyPokemon = gql` | ||
query getFuzzyPokemon($pokemon: String!) { | ||
getFuzzyPokemon(pokemon: $pokemon) { | ||
sprite | ||
num | ||
species | ||
color | ||
} | ||
} | ||
`; | ||
|
||
const apolloClient = new ApolloClient({ | ||
uri: 'https://graphqlpokemon.favware.tech/v7', | ||
fetch | ||
}); | ||
|
||
const { | ||
data: { getFuzzyPokemon: pokemonData } | ||
} = await apolloClient.query< | ||
GraphQLPokemonResponse<'getFuzzyPokemon'>, | ||
QueryGetFuzzyPokemonArgs | ||
>({ | ||
query: getFuzzyPokemon, | ||
variables: { pokemon: 'dragonite' } | ||
}); | ||
|
||
console.log(pokemonData); | ||
``` | ||
|
||
## Using `Apollo Client React` | ||
|
||
**First setup your Apollo Client** | ||
|
||
```ts | ||
import { ApolloClient } from 'apollo-client'; | ||
import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
import { HttpLink } from 'apollo-link-http'; | ||
|
||
// Instantiate required constructor fields | ||
const cache = new InMemoryCache(); | ||
const link = new HttpLink({ | ||
uri: 'https://graphqlpokemon.favware.tech/v7' | ||
}); | ||
|
||
export const client = new ApolloClient({ | ||
// Provide required constructor fields | ||
cache: cache, | ||
link: link, | ||
|
||
// Provide some optional constructor fields | ||
name: 'graphql-pokemon-client', | ||
version: '1.0', | ||
queryDeduplication: false, | ||
defaultOptions: { | ||
watchQuery: { | ||
fetchPolicy: 'cache-and-network' | ||
} | ||
} | ||
}); | ||
``` | ||
|
||
**Then use the client in your component** | ||
|
||
```ts | ||
import React from 'react'; | ||
import gql from 'graphql-tag'; | ||
import { useQuery } from '@apollo/react-hooks'; | ||
import type { Query } from '@favware/graphql-pokemon'; | ||
import { client } from './ApolloClient'; | ||
|
||
interface GraphQLPokemonResponse<K extends keyof Omit<Query, '__typename'>> { | ||
data: Record<K, Omit<Query[K], '__typename'>>; | ||
} | ||
|
||
const GET_POKEMON_DETAILS = gql` | ||
{ | ||
getPokemon(pokemon: dragonite) { | ||
sprite | ||
num | ||
species | ||
color | ||
} | ||
} | ||
`; | ||
|
||
export const Pokemon: React.FC = () => { | ||
const { loading, error, data } = useQuery< | ||
GraphQLPokemonResponse<'getPokemon'> | ||
>(GET_POKEMON_DETAILS, { | ||
client: client | ||
}); | ||
|
||
if (loading) return 'Loading...'; | ||
if (error) return `Error! ${error.message}`; | ||
|
||
return <div> Insert how you want to display the data here </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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.base.json", | ||
"include": ["src", "scripts", "tests", "magidoc.mjs", "vitest.config.ts", "tsup.config.ts", "tsup.config-codegen.ts", ".prettierrc.cjs"] | ||
"include": ["src", "scripts", "tests", "docs", "vitest.config.ts", "tsup.config.ts", "tsup.config-codegen.ts", ".prettierrc.cjs"] | ||
} |
Oops, something went wrong.