Skip to content

Commit

Permalink
Use Apollo Server Context type more widely (OpenBeta#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
zichongkao authored and l4u532 committed Dec 10, 2023
1 parent 67176e2 commit f778c43
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 47 deletions.
10 changes: 5 additions & 5 deletions src/graphql/area/AreaQueries.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AreaType } from '../../db/AreaTypes'
import type AreaDataSource from '../../model/AreaDataSource'
import { Context } from '../../types'

const AreaQueries = {
cragsWithin: async (_, { filter }, { dataSources }): Promise<AreaType | null> => {
const { areas }: { areas: AreaDataSource } = dataSources
cragsWithin: async (_, { filter }, { dataSources }: Context): Promise<AreaType | null> => {
const { areas } = dataSources
const { bbox, zoom } = filter
return await areas.findCragsWithin(bbox, zoom)
},

countries: async (_, params, { dataSources }): Promise<AreaType[]> => {
const { areas }: { areas: AreaDataSource } = dataSources
countries: async (_, params, { dataSources }: Context): Promise<AreaType[]> => {
const { areas } = dataSources
return await areas.listAllCountries()
}

Expand Down
14 changes: 7 additions & 7 deletions src/graphql/history/HistoryQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
GetAreaHistoryInputFilterType,
GetOrganizationHistoryInputFilterType
} from '../../db/ChangeLogType.js'
import { DataSourcesType } from '../../types.js'
import { Context } from '../../types.js'

const HistoryQueries = {
getChangeHistory: async (_, { filter }, { dataSources }): Promise<any> => {
const { history }: DataSourcesType = dataSources
getChangeHistory: async (_, { filter }, { dataSources }: Context): Promise<any> => {
const { history } = dataSources
const { uuidList }: GetHistoryInputFilterType = filter ?? {}
// Note: userUuid, fromDate, toDate filters don't currently work.
// Note: though we pull uuidList, we don't use it either.
Expand All @@ -19,15 +19,15 @@ const HistoryQueries = {
return await history.getChangeSets(muidList)
},

getAreaHistory: async (_, { filter }, { dataSources }): Promise<any> => {
const { history }: DataSourcesType = dataSources
getAreaHistory: async (_, { filter }, { dataSources }: Context): Promise<any> => {
const { history } = dataSources
const { areaId }: GetAreaHistoryInputFilterType = filter ?? {}
const id = muid.from(areaId)
return await history.getAreaChangeSets(id)
},

getOrganizationHistory: async (_, { filter }, { dataSources }): Promise<any> => {
const { history }: DataSourcesType = dataSources
getOrganizationHistory: async (_, { filter }, { dataSources }: Context): Promise<any> => {
const { history } = dataSources
const { orgId }: GetOrganizationHistoryInputFilterType = filter ?? {}
return await history.getOrganizationChangeSets(orgId)
}
Expand Down
10 changes: 5 additions & 5 deletions src/graphql/media/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import muid from 'uuid-mongodb'
import { DataSourcesType } from '../../types.js'
import { Context } from '../../types.js'

const MediaMutations = {
setTag: async (_: any, { input }, { dataSources }) => {
const { media }: DataSourcesType = dataSources
setTag: async (_: any, { input }, { dataSources }: Context) => {
const { media } = dataSources
return await media.setTag({
...input,
mediaUuid: muid.from(input.mediaUuid),
destinationId: muid.from(input.destinationId)
})
},

removeTag: async (_: any, { tagId }, { dataSources }) => {
const { media }: DataSourcesType = dataSources
removeTag: async (_: any, { tagId }, { dataSources }: Context) => {
const { media } = dataSources
return await media.removeTag(tagId)
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/graphql/media/queries.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { TagsLeaderboardType, MediaObject, MediaByUsers, UserMediaQueryInput, MediaForFeedInput } from '../../db/MediaObjectTypes.js'
import { DataSourcesType } from '../../types.js'
import { Context } from '../../types.js'

const MediaQueries = {

getMediaForFeed: async (_, { input }, { dataSources }): Promise<MediaByUsers[]> => {
const { media }: DataSourcesType = dataSources
getMediaForFeed: async (_, { input }, { dataSources }: Context): Promise<MediaByUsers[]> => {
const { media } = dataSources
const { maxUsers = 10, maxFiles = 20 } = input as MediaForFeedInput
return await media.getMediaByUsers({ maxUsers, maxFiles })
},

getUserMedia: async (_: any, { input }, { dataSources }): Promise<MediaObject[]> => {
const { media }: DataSourcesType = dataSources
getUserMedia: async (_: any, { input }, { dataSources }: Context): Promise<MediaObject[]> => {
const { media } = dataSources
const { userUuid, maxFiles = 1000 } = input as UserMediaQueryInput
return await media.getOneUserMedia(userUuid, maxFiles)
},

getTagsLeaderboard: async (_, { limit = 30 }: { limit: number }, { dataSources }): Promise<TagsLeaderboardType> => {
const { media }: DataSourcesType = dataSources
getTagsLeaderboard: async (_, { limit = 30 }: { limit: number }, { dataSources }: Context): Promise<TagsLeaderboardType> => {
const { media } = dataSources
return await media.getTagsLeaderboard(limit)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/organization/OrganizationQueries.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type OrganizationDataSource from '../../model/OrganizationDataSource'
import { QueryByIdType, OrganizationGQLFilter, Sort } from '../../types'
import { QueryByIdType, OrganizationGQLFilter, Sort, Context } from '../../types'

const OrganizationQueries = {
organization: async (_: any,
{ muuid }: QueryByIdType,
context, info) => {
context: Context, info) => {
const { dataSources } = context
const { organizations }: { organizations: OrganizationDataSource } = dataSources
if (muuid !== undefined) {
Expand All @@ -16,7 +16,7 @@ const OrganizationQueries = {
organizations: async (
_,
{ filter, sort, limit = 40 }: { filter?: OrganizationGQLFilter, sort?: Sort, limit?: number },
{ dataSources }
{ dataSources }: Context
) => {
const { organizations }: { organizations: OrganizationDataSource } = dataSources
const filtered = await organizations.findOrganizationsByFilter(filter)
Expand Down
7 changes: 3 additions & 4 deletions src/graphql/posts/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AddPostInputType, RemovePostInputType } from '../../db/PostTypes'
import { getPostModel } from '../../db/PostSchema.js'
import { DataSourcesType } from '../../types'
import { Context } from '../../types'
import muid from 'uuid-mongodb'
import { XMediaType } from '../../db/XMediaTypes'

Expand All @@ -9,10 +9,9 @@ const PostMutations = {
addPost: async (
_: any,
{ input }: { input: AddPostInputType },
{ dataSources }
{ dataSources }: Context
) => {
const { xmedia }: DataSourcesType = dataSources
const { post }: DataSourcesType = dataSources
const { xmedia, post } = dataSources
const { userId, description, photoUrls }: AddPostInputType = input
const userMMUID = muid.from(userId)
// Create new XMedia documents
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/xmedia/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { XMediaType, RemoveXMediaInputType } from '../../db/XMediaTypes'
import { getXMediaModel } from '../../db/XMediaSchema.js'
import { DataSourcesType } from '../../types'
import { Context } from '../../types'

const XMediaMutations = {
// addXMedia
addXMedia: async (
_: any,
{ input }: { input: XMediaType },
{ dataSources }
{ dataSources }: Context
) => {
const { xmedia }: DataSourcesType = dataSources
const { xmedia } = dataSources

try {
const newXMedia = await xmedia.addXMedia({
Expand Down
2 changes: 1 addition & 1 deletion src/model/MediaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TagsLeaderboardType, AllTimeTagStats, MediaByUsers, MediaForFeedInput,
const HARD_MAX_FILES = 1000
const HARD_MAX_USERS = 100

export default class MediaDataSourcmnee extends MongoDataSource<MediaObject> {
export default class MediaDataSource extends MongoDataSource<MediaObject> {
mediaObjectModel = getMediaObjectModel()

/**
Expand Down
27 changes: 15 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@ import { createContext, permissions } from './auth/index.js'
import XMediaDataSource from './model/XMediaDataSource.js'
import PostDataSource from './model/PostDataSource.js'
import { createInstance as createNewOrgDS } from './model/MutableOrganizationDataSource.js'
import type { Context } from './types.js'
import type { DataSources } from 'apollo-server-core/dist/graphqlOptions'

export async function createServer (): Promise<ApolloServer> {
const schema = applyMiddleware(
graphqlSchema,
permissions.generate(graphqlSchema)
)
const dataSources: () => DataSources<Context> = () => ({
climbs: createNewClimbDS(),
areas: createNewAreaDS(),
organizations: createNewOrgDS(),
ticks: new TickDataSource(mongoose.connection.db.collection('ticks')),
history: changelogDataSource, // see source for explantion why we don't instantiate the object
media: new MutableMediaDataSource(
mongoose.connection.db.collection('media')
),
xmedia: new XMediaDataSource(mongoose.connection.db.collection('xmedia')),
post: new PostDataSource(mongoose.connection.db.collection('post'))
})
const server = new ApolloServer({
introspection: true,
schema,
context: createContext,
dataSources: () => ({
climbs: createNewClimbDS(),
areas: createNewAreaDS(),
organizations: createNewOrgDS(),
ticks: new TickDataSource(mongoose.connection.db.collection('ticks')),
history: changelogDataSource, // see source for explantion why we don't instantiate the object
media: new MutableMediaDataSource(
mongoose.connection.db.collection('media')
),
xmedia: new XMediaDataSource(mongoose.connection.db.collection('xmedia')),
post: new PostDataSource(mongoose.connection.db.collection('post'))
}),
dataSources,
cache: 'bounded'
})

Expand Down

0 comments on commit f778c43

Please sign in to comment.