Skip to content

Instantly share code, notes, and snippets.

@lastmjs lastmjs/index.ts
Created Mar 12, 2018

Embed
What would you like to do?
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 {
signupResolver,
loginResolver
} from './resolvers';
import {
userOwnsDirectiveResolver,
privateDirectiveResolver,
authenticatedDirectiveResolver
} from './directive-resolvers';
import {extractFragmentReplacements} from 'prisma-binding';
import {mergeTypes} from 'merge-graphql-schemas';
import {prepareTopLevelResolvers, addFragmentToFieldResolvers} from './services/utilities';
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
});
const preparedTopLevelQueryResolvers = prepareTopLevelResolvers(PrismaDBConnection.query);
const preparedTopLevelMutationResolvers = prepareTopLevelResolvers(PrismaDBConnection.mutation);
const resolvers = {
Query: {
...preparedTopLevelQueryResolvers
},
Mutation: {
...preparedTopLevelMutationResolvers,
signup: signupResolver,
login: loginResolver
},
...preparedFieldResolvers
};
const directiveResolvers = {
userOwns: userOwnsDirectiveResolver,
authenticated: authenticatedDirectiveResolver,
private: privateDirectiveResolver
};
const ultimateSchemaString = mergeTypes([
readFileSync('./schema/datamodel.graphql').toString(),
readFileSync('./schema/dataops.graphql').toString(),
readFileSync('./schema/directives.graphql').toString(),
readFileSync('./generated/prisma.graphql').toString()
], {
all: true
});
const ultimateSchema = makeExecutableSchema({
typeDefs: ultimateSchemaString,
resolvers,
directiveResolvers
});
const server = new GraphQLServer({
schema: ultimateSchema,
context: (req) => {
return {
...req,
db: PrismaDBConnection
};
}
});
server.start(() => console.log('Server is running on http://localhost:4000'));
@zwhitchcox

This comment has been minimized.

Copy link

commented May 19, 2019

The imports

import {
    Query,
    Mutation
} from './generated/prisma';

are not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.