Skip to content

Instantly share code, notes, and snippets.

@hanakslr
Created July 16, 2020 20:59
Show Gist options
  • Save hanakslr/3b2feaec961462d1779642e90a8aeaa9 to your computer and use it in GitHub Desktop.
Save hanakslr/3b2feaec961462d1779642e90a8aeaa9 to your computer and use it in GitHub Desktop.
Simple Apollo server with GraphQL mutation for Stripe
const { ApolloServer, gql } = require("apollo-server-lambda");
const Stripe = require("stripe");
const typeDefs = gql`
type StripeCustomer {
stripe_cust_id: String!
}
type Mutation {
createStripeCustomer(name: String!, email: String!): StripeCustomer!
}
`;
const resolvers = {
Mutation: {
createStripeCustomer: async (_parent, args, context) => {
const stripe = Stripe(context.headers.stripe_api_key);
const customer = await stripe.customers.create({
name: args.name,
email: args.email,
});
return { stripe_cust_id: customer.id };
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ event, context }) => ({
headers: event.headers,
functionName: context.functionName,
event,
context,
}),
playground: {
endpoint: "/dev/graphql",
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment