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
// As far as is possible in all legal jurisdictions, I Jordan Last relinquish all rights to this code and place it in the public domain | |
// Execute asynchronous tasks in order | |
// Curry an arbitrary number of times | |
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
import {GraphQLServer} from 'graphql-yoga'; | |
import {makeExecutableSchema} from 'graphql-tools'; | |
import { | |
Prisma, | |
Query, | |
Mutation | |
} from './generated/prisma'; | |
import {readFileSync} from 'fs'; | |
import {parse} from 'graphql'; | |
import { |
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
const preparedFieldResolvers = addFragmentToFieldResolvers(parse(readFileSync('./schema/datamodel.graphql').toString()), `{ id }`) | |
const generatedFragmentReplacements = extractFragmentReplacements(preparedFieldResolvers); | |
const PrismaDBConnection = new Prisma({ | |
endpoint: 'http://127.0.0.1:4466/backend/dev', // the endpoint of the Prisma DB service | |
secret: 'mysecret123', // specified in database/prisma.yml //TODO obviously this should be controlled with environment variables | |
debug: true, // log all GraphQL queries & mutations | |
fragmentReplacements: generatedFragmentReplacements | |
}); |
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
export function addFragmentToFieldResolvers(schemaAST, fragmentSelection) { | |
return schemaAST.definitions.reduce((result, schemaDefinition) => { | |
if (schemaDefinition.kind === 'ObjectTypeDefinition') { | |
return { | |
...result, | |
[schemaDefinition.name.value]: schemaDefinition.fields.reduce((result, fieldDefinition) => { | |
//TODO this includes check is naive and will break for some strings | |
if (fragmentSelection.includes(fieldDefinition.name.value)) { | |
return result; | |
} |
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
export function prepareTopLevelResolvers(resolverObject: Query | Mutation) { | |
return Object.entries(resolverObject).reduce((result, entry) => { | |
const resolverName = entry[0]; | |
const resolverFunction = entry[1]; | |
return { | |
...result, | |
[resolverName]: async (parent, args, context, info) => { | |
return await resolverFunction(args, info); | |
} | |
}; |
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
const resolvers = { | |
Query: { | |
...preparedTopLevelQueryResolvers | |
}, | |
Mutation: { | |
...preparedTopLevelMutationResolvers, | |
signup: signupResolver, | |
login: loginResolver | |
}, | |
...preparedFieldResolvers |
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
const directiveResolvers = { | |
userOwns: userOwnsDirectiveResolver, | |
authenticated: authenticatedDirectiveResolver, | |
private: privateDirectiveResolver | |
}; |
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
export async function userOwnsDirectiveResolver(next, source, args, context) { | |
if (source[args.field] === await getUserId(context)) { | |
return await next(); | |
} | |
else { | |
throw new Error('Not authorized'); | |
} | |
} |
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
export async function authenticatedDirectiveResolver(next, source, args, context) { | |
if (getUserId(context)) { | |
return await next(); | |
} | |
else { | |
throw new Error('Not authenticated'); | |
} | |
} |
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
export async function privateDirectiveResolver(next, source, args, context) { | |
throw new Error('Private'); | |
} |
OlderNewer