Skip to content

Instantly share code, notes, and snippets.

@imjakechapman
Last active May 7, 2020 18:38
Show Gist options
  • Save imjakechapman/a7e857ea02cbb4b7e717d346c51e29f2 to your computer and use it in GitHub Desktop.
Save imjakechapman/a7e857ea02cbb4b7e717d346c51e29f2 to your computer and use it in GitHub Desktop.
// import { UserInputError } from 'apollo-server'
import { IResolvers } from "graphql-tools";
import merge from "lodash.merge";
const ChargesRootResolvers: IResolvers = {
// Stripe returns either just a string as the charge id or an expanded object with all charge attributes.
// So we check if it's expanded by if the charge is an instanceof Object or a string
ChargeUnion: {
__resolveType(charge: string | { [key: string]: any }) {
if (charge instanceof Object) {
return "ChargeExpanded";
}
return "Charge";
}
}
};
const ChargesResolvers: IResolvers = merge(ChargesRootResolvers);
export { ChargesResolvers };
import { gql } from "apollo-server";
const ChargeDefs = gql`
union ChargeUnion = Charge | ChargeExpanded
interface ChargeInterface {
id: String!
}
type Charge implements ChargeInterface {
id: String!
}
type ChargeExpanded implements ChargeInterface {
id: String!
amount: Int!
amount_refunded: Int
balance_transaction: String!
billing_details: JSON
calculated_statement_descriptor: String
captured: Boolean
created: Int!
currency: String!
customer: String!
description: String
disputed: Boolean!
failure_code: String
failure_message: String
fraud_details: JSON
invoice: String!
livemode: Boolean!
metadata: JSON
outcome: JSON
paid: Boolean
payment_intent: String
payment_method: String
payment_method_details: JSON
receipt_email: String
receipt_number: String
receipt_url: String
refunded: Boolean!
shipping: JSON
statement_descriptor: String
statement_descriptor_suffix: String
status: String
}
`;
const ChargeSchemaDefs = [ChargeDefs];
export { ChargeSchemaDefs };
const GET_SUBSCRIPTION_DETAILS = gql`
query GET_SUBSCRIPTION_DETAILS($subscriptionExpand: [String], $customerExpand: [String], $invoiceExpand: [String]) {
invoices(expand: $invoiceExpand) {
data {
id
created
customer_email
number
amount_paid
status
invoice_pdf
charge {
... on ChargeExpanded {
id
amount
failure_code
failure_message
paid
status
refunded
receipt_url
}
}
}
}
customer(expand: $customerExpand) {
id
}
subscription(expand: $subscriptionExpand) {
id
status
current_period_end
cancel_at
cancel_at_period_end
canceled_at
plan {
nickname
interval
interval_count
}
customer {
id
}
default_payment_method {
... on PaymentMethodExpanded {
card {
brand
last4
exp_year
exp_month
}
}
}
}
}
`;
const { loading, error, data } = useQuery(GET_SUBSCRIPTION_DETAILS, {
variables: {
subscriptionExpand: ["latest_invoice"],
invoiceExpand: ["charge"],
customerExpand: []
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment