Skip to content

Commit

Permalink
massage types with some expected errors for build
Browse files Browse the repository at this point in the history
  • Loading branch information
jbolda committed Dec 9, 2024
1 parent debeac8 commit 2182e45
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
9 changes: 7 additions & 2 deletions packages/github-api/src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function createResolvers(
): Resolvers {
return {
Query: {
// @ts-expect-error not a fully qualified return per type, TODO fill it out
viewer() {
let user = simulationStore.schema.users.selectById(
simulationStore.store.getState(),
Expand All @@ -18,6 +19,7 @@ export function createResolvers(
assert(!!user, `no logged in user`);
return toGraphql(simulationStore, "User", user);
},
// @ts-expect-error not a fully qualified return per type, TODO fill it out
organization(_: unknown, { login }: { login: string }) {
let orgs = simulationStore.schema.githubOrganizations.selectTableAsList(
simulationStore.store.getState()
Expand All @@ -29,10 +31,10 @@ export function createResolvers(
__typename === "githuborganization",
`incorrectly structured GitHubOrganization id ${org.id}`
);
console.dir({ org });
let shaped = toGraphql(simulationStore, "Organization", org);
return shaped;
},
// @ts-expect-error not a fully qualified return per type, TODO fill it out
organizations(pageArgs: PageArgs) {
let orgs = simulationStore.schema.githubOrganizations.selectTableAsList(
simulationStore.store.getState()
Expand All @@ -41,6 +43,7 @@ export function createResolvers(
toGraphql(simulationStore, "Organization", org)
);
},
// @ts-expect-error not a fully qualified return per type, TODO fill it out
repository(_, { owner, name }: { owner: string; name: string }) {
let repo = simulationStore.schema.githubRepositories
.selectTableAsList(simulationStore.store.getState())
Expand All @@ -52,6 +55,7 @@ export function createResolvers(
assert(!!repo, `no repository found for ${name}`);
return toGraphql(simulationStore, "Repository", repo);
},
// @ts-expect-error not a fully qualified return per type, TODO fill it out
repositoryOwner(_, { login }: { login: string }) {
let [org] = simulationStore.schema.githubOrganizations
.selectTableAsList(simulationStore.store.getState())
Expand All @@ -61,7 +65,8 @@ export function createResolvers(

let [userAccount] = simulationStore.schema.users
.selectTableAsList(simulationStore.store.getState())
.filter((u) => u?.githubAccount?.login === login);
// TODO should we use u?.githubAccount?.login here?
.filter((u) => u?.login === login);
assert(
!!userAccount,
`no github organization or account found for ${login}`
Expand Down
31 changes: 25 additions & 6 deletions packages/github-api/src/graphql/to-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import type {
User,
Repository,
Organization,
Team,
} from "../__generated__/resolvers-types";

interface GraphQLData {
User: User;
Repository: Repository;
Organization: Organization;
Team: Team;
}

export function toGraphql<T extends keyof GraphQLData>(
Expand All @@ -21,10 +23,11 @@ export function toGraphql<T extends keyof GraphQLData>(
): Pick<GraphQLData, T>[T] {
switch (__typename) {
case "User":
// @ts-expect-error not a fully qualified return per type, TODO fill it out
return {
__typename,
...entity,
...toGithubRepositoryOwner(simulationStore, __typename, entity),
...toGithubRepositoryOwner(simulationStore, __typename, entity as User),
name: entity.name,
bio: entity.bio,
createdAt: entity.createdAt,
Expand All @@ -42,6 +45,7 @@ export function toGraphql<T extends keyof GraphQLData>(
},
};
case "Repository":
// @ts-expect-error not a fully qualified return per type, TODO fill it out
return {
name: entity.name,
description: entity.description,
Expand Down Expand Up @@ -77,21 +81,27 @@ export function toGraphql<T extends keyof GraphQLData>(
isArchived: entity.isArchived,
};
case "Organization":
// @ts-expect-error not a fully qualified return per type, TODO fill it out
return {
...toGithubRepositoryOwner(simulationStore, __typename, entity),
...toGithubRepositoryOwner(
simulationStore,
__typename,
entity as Organization
),
__typename,
id: entity.id,
name: entity.name,
description: entity.description,
email: entity.email,
createdAt: entity.createdAt,
teams(pageArgs: PageArgs) {
return applyRelayPagination(entity.teams, pageArgs, (team) =>
return applyRelayPagination(entity.teams, pageArgs, (team: Team) =>
toGraphql(simulationStore, "Team", team)
);
},
};
default:
// @ts-expect-error not a fully qualified return per type, TODO fill it out
return entity;
}
}
Expand All @@ -102,14 +112,23 @@ export function toGraphql<T extends keyof GraphQLData>(
function toGithubRepositoryOwner(
simulationStore: ExtendedSimulationStore,
__typename: string,
entity: GithubOrganization | GithubAccount
entity: AnyState
) {
return {
avatarUrl: entity.avatarUrl,
login: entity.login,
repositories(pageArgs: PageArgs) {
return applyRelayPagination(entity.repositories, pageArgs, (repository) =>
toGraphql(simulationStore, "Repository", repository)
return applyRelayPagination(
// @ts-expect-error not a fully qualified return per type, TODO fill it out
simulationStore.schema.githubRepositories.selectByIds(
simulationStore.store.getState(),
{
ids: entity.repositories,
}
),
pageArgs,
(repository: Repository) =>
toGraphql(simulationStore, "Repository", repository)
);
},
resourcePath: entity.resourcePath,
Expand Down
11 changes: 8 additions & 3 deletions packages/github-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { createFoundationSimulationServer } from "@simulacrum/foundation-simulator";
import { extendStore } from "./store/index";
import {
createFoundationSimulationServer,
type FoundationSimulator,
} from "@simulacrum/foundation-simulator";
import { ExtendedSimulationStore, extendStore } from "./store/index";
import { extendRouter } from "./graphql/extend-api";
import { openapi } from "./rest/index";

export const simulation = createFoundationSimulationServer({
export type GitHubSimulator = FoundationSimulator<ExtendedSimulationStore>;

export const simulation: GitHubSimulator = createFoundationSimulationServer({
port: 3300, // default port
extendStore,
extendRouter,
Expand Down
62 changes: 57 additions & 5 deletions packages/github-api/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@ import type {
ExtendSimulationActions,
ExtendSimulationSelectors,
ExtendSimulationSchema,
TableOutput,
AnyState,
} from "@simulacrum/foundation-simulator";

export type ExtendedSchema = typeof inputSchema;
export type ExtendedSchema = ({ slice }: ExtendSimulationSchema) => {
users: (
n: string
) => TableOutput<GitHubUser, AnyState, GitHubUser | undefined>;
githubRepositories: (
n: string
) => TableOutput<
GitHubRepositories,
AnyState,
GitHubRepositories | undefined
>;
githubOrganizations: (
n: string
) => TableOutput<
GitHubOrganizations,
AnyState,
GitHubOrganizations | undefined
>;
blob: (
n: string
) => TableOutput<GitHubBlob, AnyState, GitHubBlob | undefined>;
};
type ExtendActions = typeof inputActions;
type ExtendSelectors = typeof inputSelectors;
export type ExtendedSimulationStore = SimulationStore<
Expand All @@ -14,20 +37,49 @@ export type ExtendedSimulationStore = SimulationStore<
ReturnType<ExtendSelectors>
>;

interface GitHubUser {
id: string;
firstName: string;
lastName: string;
login: string;
organizations: string[];
}

interface GitHubRepositories {
id: string;
name: string;
nameWithOwner: string;
packages?: string[];
}

interface GitHubOrganizations {
id: string;
login: string;
entityName: string;
name: string;
email: string;
description: string;
createdAt: number;
teams: string[] | undefined;
}

interface GitHubBlob {}

const inputSchema = ({ slice }: ExtendSimulationSchema) => {
let slices = {
users: slice.table({
users: slice.table<GitHubUser>({
initialState: {
"user:1": {
id: "user:1",
firstName: "Default",
lastName: "User",
login: "defaultUser",
organizations: ["githuborganization:1"],
},
},
}),
githubRepositories: slice.table(),
githubOrganizations: slice.table({
githubRepositories: slice.table<GitHubRepositories>(),
githubOrganizations: slice.table<GitHubOrganizations>({
initialState: {
"githuborganization:1": {
id: "githuborganization:1",
Expand All @@ -41,7 +93,7 @@ const inputSchema = ({ slice }: ExtendSimulationSchema) => {
},
},
}),
blob: slice.table(),
blob: slice.table<GitHubBlob>(),
};
return slices;
};
Expand Down

0 comments on commit 2182e45

Please sign in to comment.