Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { makeExecutableSchema } from 'graphql-tools';
import { GraphQLSchema } from 'graphql';
import http from 'http';
import { GraphQLOptions } from 'apollo-server-core';
import { resolvers } from './resolvers';
import { typeDefs } from './typeDefs';
import { config } from '../config';
import { logger, log } from '../utils';
import { Book } from './models';
const schema: GraphQLSchema = makeExecutableSchema({ typeDefs, resolvers, logger: { log } });
async function main(): Promise {
const app: express.Application = express();
const graphqlOptions: GraphQLOptions = {
schema,
context: {
Book
},
formatError: err => {
logger.info('formatError');
const logErr = {
path: err.path,
locations: err.locations
};
// tslint:disable-next-line:no-console
console.error(logErr);
export function setupRoutes(app: express.Express, registration: Registration) {
const schema = makeExecutableSchema({
typeDefs,
// XXX: The types are javascript equivalent, but unreachable from the graphql-tools library
resolvers: resolver(registration) as any
});
// Set up graphql and graphiql routes
app.use(
"/graphql",
bodyParser.json(),
authenticateWithReject,
(request, response, next) => {
graphqlExpress({
schema,
context: request
})(request, response, next);
}
}`;
const RootMutaion = `type Mutation {
createUser(email: String!, userName: String!, password: String!): User
}`;
const SchemaDefinition = `schema {
query: Query
mutation: Mutation
}`;
// Combine all resolvers
const RootResolver = merge({}, userResolvers);
// Uses graphQL tools to make create a proper schema
const executableSchema = makeExecutableSchema({
typeDefs: [SchemaDefinition, RootQuery, RootMutaion, userSchema],
resolvers: RootResolver,
});
module.exports = function (app) {
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: executableSchema }));
app.use('/graphiql', graphiqlExpress({ endpointURL: './graphql' }));
};
import * as fs from 'fs'
import { GraphQLSchema } from 'graphql'
import { makeExecutableSchema } from 'graphql-tools'
// tslint:disable-next-line:no-circular-imports
import resolvers from './resolvers'
const typeDefs: string = fs.readFileSync('./src/graphql/schema.gql', 'utf-8')
const executableSchema: GraphQLSchema = makeExecutableSchema({
typeDefs,
resolvers
})
export default executableSchema
export default async function() {
await create();
const [{ default: resolvers }, { default: typeDefs }] = await Promise.all([import("./graphQL/resolver"), import("./graphQL/schema")]);
let db, schema;
let client = await MongoClient.connect(
nextConnectionString(),
{ useNewUrlParser: true, useUnifiedTopology: true }
);
db = client.db(process.env.databaseName || "mongo-graphql-starter");
schema = makeExecutableSchema({ typeDefs, resolvers, initialValue: { db: {} } });
return {
db,
schema,
close: () => client.close(),
runQuery: options => runQuery({ schema, db, ...options }),
queryAndMatchArray: options => queryAndMatchArray({ schema, db, ...options }),
runMutation: options => runMutation({ schema, db, ...options })
};
}
async updateMocks(mockFeatureMaps, allRemoteSchemas) {
let mergedSchema;
const resolvers = this.loadResolvers(mockFeatureMaps);
const [remoteSchemas, localTypeDefs] = await Promise.all([
allRemoteSchemas,
this.loadSchemas(mockFeatureMaps),
]);
try {
const typeDefs = localTypeDefs.filter(Boolean);
if (!typeDefs.length) return;
const localSchema = makeExecutableSchema({
typeDefs,
resolverValidationOptions: {
requireResolversForResolveType: false,
},
});
const schemas = [...remoteSchemas.filter(Boolean), localSchema];
if (!schemas.length) return;
mergedSchema = mergeSchemas({
schemas,
resolvers,
});
} catch (error) {
console.warn('Skip: GraphQL schemas merge failed.', error);
}
args.authorId = userId;
const { insertedId } = await Posts.insertOne(args);
return prepare(await Posts.findOne(ObjectId(insertedId)));
},
createComment: async (root, args, { userId }) => {
if (!userId) {
throw new Error("User not logged in.");
}
args.authorId = userId;
const { insertedId } = await Comments.insertOne(args);
return prepare(await Comments.findOne(ObjectId(insertedId)));
}
}
};
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const app = express();
app.use(morgan("dev"));
const corsMiddleware = cors({
origin: process.env.ORIGIN_URL,
credentials: true,
preflightContinue: false
});
app.use(corsMiddleware);
app.options(corsMiddleware);
setupAuthEndpoints(app);
employee(_obj, { id }: { id: number }, _context): Promise {
return Promise.resolve(employees.find(p => p.id === id));
},
projects() {
return allProjects;
},
project(_obj, { id }: { id: number }, _context): Promise {
return Promise.resolve(allProjects.find(p => p.id === id));
}
}
};
const jsSchema = makeExecutableSchema({
typeDefs: schema,
resolvers
});
export function addGraphQL(server) {
server.use('/graphql', bodyParser.json(),
graphqlExpress({
schema: jsSchema
}));
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
}
function mergeGraph() {
let should = false;
if (henri._graphql.typesList.length > 0) {
should = true;
henri._graphql.types = mergeTypes(henri._graphql.typesList);
}
if (henri._graphql.resolversList.length > 0) {
should = true;
henri._graphql.resolvers = mergeResolvers(henri._graphql.resolversList);
}
if (should) {
henri._graphql.schema = makeExecutableSchema({
typeDefs: henri._graphql.types,
resolvers: henri._graphql.resolvers,
});
}
}
price: Float
qty: Float
}
type OrderBook {
symbol: String
coin: Coin
exchange: Exchange
realToSymbol: String
last_updated: Float
asks: [Order]
bids: [Order]
}
`;
const schema = makeExecutableSchema({ typeDefs, resolvers });
module.exports = schema;