Skip to content

Instantly share code, notes, and snippets.

@rphansen91
Created June 30, 2020 06:14
Show Gist options
  • Save rphansen91/d2a83a5e445152389299469bcee24871 to your computer and use it in GitHub Desktop.
Save rphansen91/d2a83a5e445152389299469bcee24871 to your computer and use it in GitHub Desktop.
import { makeExecutableSchema } from '@graphql-tools/schema'
import { addResolveFunctionsToSchema } from 'apollo-server'
import { graphqlTypeDate, graphqlTypeObjectId, makeAugmentedSchema, mongoTypeDefs } from 'ts-mongo-codegen'
// The following import is the file generated in previous step
import { mountainMutationResolvers, mountainQueryResolvers, mountainResolvers } from './types.generated'
import { composeResolvers } from '@graphql-tools/resolvers-composition'
import { mountainSchema } from './gql/mountains'
import { isAuthenticated } from './auth'
// Make an executable schema with the mongo types and our custom mountain schema type
const executableSchema = makeExecutableSchema({
typeDefs: [mongoTypeDefs, mountainSchema],
})
// Add CRUD operations to the Mountain type by augmenting the schema
export const schema = makeAugmentedSchema(executableSchema)
// The mountainResolvers, mountainMutationResolvers, and mountainQueryResolvers are generated types
// Run `yarn generate` to update types or add more
const resolvers = composeResolvers(
{
Date: graphqlTypeDate,
Mountain: mountainResolvers,
Mutation: {
...mountainMutationResolvers,
},
ObjectId: graphqlTypeObjectId,
Query: {
...mountainQueryResolvers,
},
},
{
// Make sure mutation requests are authenticated
Mutation: [isAuthenticated()],
}
)
// Finally we add our generated resolvers to the schema
addResolveFunctionsToSchema({
resolvers,
schema,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment