Skip to content
This repository was archived by the owner on May 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import gqlLoader from './loaders/gql.loader'
const { APPLICATION_NAME, SERVER_HOSTNAME, SERVER_PORT } = process.env

const bootstrap = async () => {
await databaseLoader()
const database = await databaseLoader()

const application = expressLoader()

gqlLoader(application)
gqlLoader(application, database)

application.listen({ hostname: SERVER_HOSTNAME, port: SERVER_PORT }, () => {
consola.info(APPLICATION_NAME)
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/context.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Connection, Repository } from 'typeorm'

import { Node } from '../relay/node.interface'

export interface Context {
database: Connection
repositories: Record<string, Repository<Node>>
}
17 changes: 12 additions & 5 deletions src/loaders/gql.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import { Express } from 'express'
import { GraphQLSchema } from 'graphql'
import { buildSchema, BuildSchemaOptions } from 'type-graphql'
import Container from 'typedi'
import { Connection } from 'typeorm'

import { NodeResolver } from '../relay/node.resolver'
import { Context } from '../interfaces/context.interface'
import { isDevelopment } from '../utils'

const { GRAPHQL_PATH } = process.env

export const createSchema = (
options?: Partial<BuildSchemaOptions>
): Promise<GraphQLSchema> => {
const defined = {
resolvers: [NodeResolver, `${__dirname}/../modules/**/*.resolver.{ts,js}`],
const defined: BuildSchemaOptions = {
resolvers: [`${__dirname}/../{modules,relay}/**/*.resolver.{ts,js}`],
container: Container,
emitSchemaFile: isDevelopment
}
Expand All @@ -24,11 +25,17 @@ export const createSchema = (
} as BuildSchemaOptions)
}

export default async (app: Express): Promise<void> => {
export default async (app: Express, database: Connection): Promise<void> => {
const schema = await createSchema()

const context: Context = {
database,
repositories: {}
}

const apolloServer = new ApolloServer({
schema
schema,
context
})

apolloServer.applyMiddleware({ app, path: GRAPHQL_PATH })
Expand Down
Empty file added src/modules/.gitkeep
Empty file.
13 changes: 0 additions & 13 deletions src/modules/product/inputs/product.input.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/modules/product/payloads/add-product.payload.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/modules/product/product.connection.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/modules/product/product.edge.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/modules/product/product.entity.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/modules/product/product.resolver.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/modules/product/product.service.ts

This file was deleted.

55 changes: 29 additions & 26 deletions src/relay/node.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ import { UserInputError } from 'apollo-server-express'
import { fromGlobalId, toGlobalId } from 'graphql-relay'
import {
Arg,
Ctx,
FieldResolver,
ID,
Info,
Query,
Resolver,
Root
} from 'type-graphql'
import { Inject } from 'typedi'

import { ProductService } from '../modules/product/product.service'
import { Context } from '../interfaces/context.interface'
import { Node } from './node.interface'

@Resolver(() => Node)
export class NodeResolver {
@Inject()
private readonly productService!: ProductService

@FieldResolver()
globalId(
@Root() { id }: { id: string },
Expand All @@ -27,39 +24,45 @@ export class NodeResolver {
return toGlobalId(name, id)
}

private async fetcher(
globalId: string,
{ repositories }: Context
): Promise<Node | undefined> {
const { type, id } = fromGlobalId(globalId)

const repository = repositories[type]

if (!repository) {
throw new UserInputError(
`Could not resolve to a node with the global ID of '${globalId}'`
)
}

return repository.findOne(id)
}

// TODO: use dataloader
// TODO: find a better way to automate and avoid if conditions
@Query(() => Node, {
nullable: true,
description: 'Fetches an object given its global ID.'
})
async node(
@Arg('id', () => ID, {
description: 'The global ID of the object.'
})
globalId: string
): Promise<Node | undefined> {
const { type, id } = fromGlobalId(globalId)

if (type == 'Product') {
return this.productService.findById(id)
}

throw new UserInputError(
`Could not resolve to a node with the global ID of '${globalId}'`
)
@Arg('id', () => ID, { description: 'The global ID of the object.' })
globalId: string,
@Ctx() context: Context
): ReturnType<NodeResolver['fetcher']> {
return this.fetcher(globalId, context)
}

@Query(() => [Node], {
nullable: 'items',
description: 'Fetches objects given their global IDs.'
})
async nodes(
@Arg('ids', () => [ID], {
description: 'The global IDs of the objects.'
})
globalIds: Array<string>
): Promise<Array<ReturnType<NodeResolver['node']>>> {
return globalIds.map(id => this.node(id))
@Arg('ids', () => [ID], { description: 'The global IDs of the objects.' })
globalIds: Array<string>,
@Ctx() context: Context
): Promise<Array<ReturnType<NodeResolver['fetcher']>>> {
return globalIds.map(id => this.fetcher(id, context))
}
}
Loading