本文整理汇总了TypeScript中graphql-yoga.GraphQLServer类的典型用法代码示例。如果您正苦于以下问题:TypeScript GraphQLServer类的具体用法?TypeScript GraphQLServer怎么用?TypeScript GraphQLServer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GraphQLServer类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: PubSub
import { GraphQLServer, PubSub } from 'graphql-yoga';
import { formatError } from 'apollo-errors';
import * as jwt from 'express-jwt';
import * as faker from 'faker/locale/en';
import * as compression from 'compression';
import resolvers from './resolvers';
const { JWT_SECRET } = process.env;
const pubsub = new PubSub();
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
context: req => ({
...req,
jwtSecret: JWT_SECRET,
faker,
pubsub
})
});
server.express.disable('x-powered-by');
server.express.use(
'/graphql',
jwt({
secret: JWT_SECRET,
credentialsRequired: false
})
);
示例2: GraphQLServer
import { GraphQLServer } from 'graphql-yoga'
import { prisma } from './generated/prisma-client'
import { resolvers } from './resolvers'
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
context: {
prisma,
},
} as any)
server.start(() => console.log('Server is running on http://localhost:4000'))
示例3: GraphQLServer
import { GraphQLServer } from "graphql-yoga";
import { default as typeDefs } from './typeDefs'
import { default as resolvers } from './resolvers'
const options = { port: 4004 };
const server = new GraphQLServer({
typeDefs,
resolvers
});
server.start(options, () =>
console.log(`Server is running ⚡ on localhost:${options.port}`))
.catch(err => console.error('connection Error', err));
示例4: Stripe
formatError,
};
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
export const db = new Prisma({
endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
debug: true, // log all GraphQL queries & mutations
});
export const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
context: req => ({
...req,
db,
mailer,
stripe,
}),
});
server.start(options, () => console.log(`Server is running on http://localhost:4000`));
const rawBodyParser = require('body-parser').raw({ type: '*/*' });
server.express.post('/stripe', rawBodyParser, async (req, res, next) => {
let data;
// Check if webhook signing is configured.
if (process.env.STRIPE_WEBHOOK_SECRET) {
// Retrieve the event by verifying the signature using the raw body and secret.
let event;
示例5: async
export const startServer = async () => {
const server = new GraphQLServer({
schema : genSchema(),
context: ({ request }) => ({
redis,
url: `${request.protocol}://${request.get("host")}`,
session: request.session // req now has a session to it
})
});
// add express-server middleware
server.express.use(
session({
store: new RedisStore({
client: redis as any
}),
name: "qid",
secret: process.env.SESSION_SECRET as string,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days
}
})
);
// server.express.use(
// // session options
// session({
// store: new RedisStore({
// client: redis as any
// }),
// name: "qid",
// secret: 'secret',
// resave: false,
// saveUninitialized: false, // dont store cookie unless data on user needs to be added
// cookie: { // cookie settings
// httpOnly: true, // JS cannot access cookie --> security
// secure: process.env.NODE_ENV === "production",
// maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days
// }
// })
// );
// send a cookie back
const cors = {
credentials: true,
origin: process.env.NODE_ENV === "test"
? '*'
: process.env.FRONTEND_HOST as string // front end server host
}
// REST endpoint for email confirmation link
// Once user clicks on confirmation link
server.express.get("/confirm/:id", confirmEmail);
await createTypeormConn();
const app = await server.start({
cors,
port: process.env.NODE_ENV === 'test' ? 0 : 4000
});
console.log('Server is running on localhost:4000');
return app;
}
示例6: GraphQLServer
import { GraphQLServer } from 'graphql-yoga';
const typeDefs = `
type Query {
greet: String
}
`
const resolvers = {
Query: {
greet: () => 'hello world'
}
}
const server = new GraphQLServer({typeDefs, resolvers})
server.start(() => console.log('⚡️ Waiting to greet you at http://localhost:4000'))