Created
July 16, 2020 20:59
-
-
Save hanakslr/3b2feaec961462d1779642e90a8aeaa9 to your computer and use it in GitHub Desktop.
Simple Apollo server with GraphQL mutation for Stripe
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 { 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