@nhost/stripe-graphql-js 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,3 @@
1
- <h1>⚠️ Work In Progress ⚠️</h1>
2
-
3
- **This package being actively worked on and is NOT stable!**
4
-
5
1
  <h1 align="center">@nhost/stripe-graphql-js</h1>
6
2
  <h2 align="center">Stripe GraphQL API</h2>
7
3
 
@@ -13,9 +9,28 @@
13
9
  </a>
14
10
  </p>
15
11
 
16
- Stripe GraphQL API with Hasura Remote schemas.
12
+ This package creates a Stripe GraphQL API.
13
+
14
+ ```graphql
15
+ query {
16
+ stripe {
17
+ customer(id: "cus_MVBRpmnBis9v5x") {
18
+ id
19
+ name
20
+ invoices {
21
+ data {
22
+ id
23
+ created
24
+ paid
25
+ hostedInvoiceUrl
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }
31
+ ```
17
32
 
18
- Connect data in your database with data from Stripe, via GraphQL.
33
+ You can also add the Stripe GraphQL API as a Hasura Remote Schema and connect data from your database and Stripe. This allows you to request data from your database and Stripe in a single GraphQL query:
19
34
 
20
35
  ```graphql
21
36
  query {
@@ -62,9 +77,11 @@ const server = createStripeGraphQLServer()
62
77
  export default server
63
78
  ```
64
79
 
80
+ > You can run the Stripe GraphQL API in any JS environment because it's built using [GraphQL Yoga](https://github.com/dotansimha/graphql-yoga).
81
+
65
82
  ### Test
66
83
 
67
- Test the Stripe GraphQL API in the rowser:
84
+ Test the Stripe GraphQL API in the browser:
68
85
 
69
86
  [http://localhost:1337/v1/functions/graphql/stripe](http://localhost:1337/v1/functions/graphql/stripe)
70
87
 
@@ -72,7 +89,19 @@ Test the Stripe GraphQL API in the rowser:
72
89
 
73
90
  Add the Stripe GraphQL API as a Remote Schema in Hasura.
74
91
 
75
- URL: `{{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe`
92
+ **URL**
93
+
94
+ ```
95
+ {{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe`
96
+ ```
97
+
98
+ **Headers**
99
+
100
+ ```
101
+ x-nhost-webhook-secret: NHOST_WEBHOOK_SECRET (from env var)
102
+ ```
103
+
104
+ ![Hasura Remote Schema](./assets//hasura-remote-schema.png)
76
105
 
77
106
  ## Permissions
78
107
 
@@ -150,6 +179,8 @@ Start the development server:
150
179
  pnpm dev
151
180
  ```
152
181
 
182
+ The GraphQL Server will reload every time the code changes.
183
+
153
184
  Open GraphiQL:
154
185
 
155
186
  [http://0.0.0.0:4000/graphql](http://0.0.0.0:4000/graphql)
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAGpD,QAAA,MAAM,yBAAyB,YAAa,iBAAiB;;;eA6C5D,CAAA;AAED,OAAO,EAAE,yBAAyB,EAAE,MAAM,EAAE,CAAA"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAGpD,QAAA,MAAM,yBAAyB,YAAa,iBAAiB;;;eA+C5D,CAAA;AAED,OAAO,EAAE,yBAAyB,EAAE,MAAM,EAAE,CAAA"}
package/dist/server.js CHANGED
@@ -8,6 +8,7 @@ const utils_1 = require("./utils");
8
8
  const createStripeGraphQLServer = (params) => {
9
9
  const cors = params === null || params === void 0 ? void 0 : params.cors;
10
10
  const isAllowed = params === null || params === void 0 ? void 0 : params.isAllowed;
11
+ const graphiql = params === null || params === void 0 ? void 0 : params.graphiql;
11
12
  const context = (context) => {
12
13
  const { request } = context;
13
14
  // user id
@@ -33,6 +34,7 @@ const createStripeGraphQLServer = (params) => {
33
34
  };
34
35
  return (0, node_1.createServer)({
35
36
  cors,
37
+ graphiql,
36
38
  context,
37
39
  schema: schema_1.schema
38
40
  });
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;AAAA,6CAAqE;AAErE,qCAAiC;AAmDG,uFAnD3B,eAAM,OAmD2B;AAjD1C,mCAAuC;AAEvC,MAAM,yBAAyB,GAAG,CAAC,MAA0B,EAAE,EAAE;IAC/D,MAAM,IAAI,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,CAAA;IACzB,MAAM,SAAS,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,SAAS,CAAA;IAEnC,MAAM,OAAO,GAAG,CAAC,OAA2B,EAAW,EAAE;QACvD,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAA;QAE3B,UAAU;QACV,MAAM,UAAU,GAAG,IAAA,qBAAa,EAAC,OAAO,CAAC,CAAA;QAEzC,wDAAwD;QACxD,MAAM,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;QAC1E,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAA;QAElD,sCAAsC;QACtC,MAAM,4BAA4B,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAA;QAClF,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAA;QAC3D,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;QAEjD,YAAY;QACZ,MAAM,OAAO,GACX,qBAAqB,KAAK,WAAW;YACrC,CAAC,IAAI,KAAK,OAAO,IAAI,4BAA4B,KAAK,kBAAkB,CAAC,CAAA;QAE3E,qEAAqE;QACrE,MAAM,iBAAiB,GACrB,SAAS;YACT,CAAC,CAAC,iBAA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AA;QAC3D,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;QAEjD,YAAY;QACZ,MAAM,OAAO,GACX,qBAAqB,KAAK,WAAW;YACrC,CAAC,IAAI,KAAK,OAAO,IAAI,4BAA4B,KAAK,kBAAkB,CAAC,CAAA;QAE3E,qEAAqE;QACrE,MAAM,iBAAiB,GACrB,SAAS;YACT,CAAC,CAAC,iBAAyB,EAAE,OAAgB,EAAE,EAAE;gBAC/C,OAAO,OAAO,CAAC,OAAO,CAAA;YACxB,CAAC,CAAC,CAAA;QAEJ,SAAS;QACT,uCACK,OAAO,KACV,SAAS,EAAE,iBAAiB,EAC5B,UAAU;YACV,OAAO,IACR;IACH,CAAC,CAAA;IAED,OAAO,IAAA,mBAAY,EAAC;QAClB,IAAI;QACJ,QAAQ;QACR,OAAO;QACP,MAAM,EAAN,eAAM;KACP,CAAC,CAAA;AACJ,CAAC,CAAA;AAEQ,8DAAyB"}
package/dist/types.d.ts CHANGED
@@ -9,6 +9,7 @@ export declare type Context = YogaInitialContext & StripeGraphQLContext;
9
9
  export declare type CreateServerProps = {
10
10
  cors?: CORSOptions;
11
11
  isAllowed?: (stripeCustomerId: string, context: Context) => boolean;
12
+ graphiql?: boolean;
12
13
  };
13
14
  export declare type StripePaymentMethod = Stripe.PaymentMethod & {
14
15
  customer: string | null;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAEhC,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAEzE,oBAAY,oBAAoB,GAAG;IACjC,SAAS,EAAE,CAAC,gBAAgB,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAA;IAClE,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,OAAO,EAAE,OAAO,CA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AA;AAED,oBAAY,OAAO,GAAG,kBAAkB,GAAG,oBAAoB,CAAA;AAE/D,oBAAY,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB,SAAS,CAAC,EAAE,CAAC,gBAAgB,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAA;IACnE,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAID,oBAAY,mBAAmB,GAAG,MAAM,CAAC,aAAa,GAAG;IACvD,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;CACxB,CAAA;AAED,oBAAY,kBAAkB,GAAG,MAAM,CAAC,YAAY,GAAG;IACrD,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B,CAAA;AAED,oBAAY,aAAa,GAAG,MAAM,CAAC,OAAO,GAAG;IAC3C,EAAE,EAAE,MAAM,CAAA;IACV,QAAQ,EAAE,MAAM,CAAA;IAChB,sBAAsB,EAAE,mBAAmB,GAAG,IAAI,CAAA;CACnD,CAAA;AAED,oBAAY,gBAAgB,GAAG;IAC7B,kBAAkB,EAAE,MAAM,CAAA;IAC1B,uBAAuB,EAAE,MAAM,CAAA;IAC/B,wBAAwB,EAAE,MAAM,EAAE,CAAA;CACnC,GAAG;IACF,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CACtB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nhost/stripe-graphql-js",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Stripe GraphQL API",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -53,5 +53,6 @@
53
53
  "lint:fix": "eslint . --ext .ts,.tsx --fix",
54
54
  "verify": "run-p prettier lint",
55
55
  "verify:fix": "run-p prettier:fix lint:fix"
56
- }
56
+ },
57
+ "readme": "<h1 align=\"center\">@nhost/stripe-graphql-js</h1>\n<h2 align=\"center\">Stripe GraphQL API</h2>\n\n<p align=\"center\">\n <img alt=\"npm\" src=\"https://img.shields.io/npm/v/@nhost/stripe-graphql\">\n <img alt=\"npm\" src=\"https://img.shields.io/npm/dm/@nhost/stripe-graphql\">\n <a href=\"LICENSE\">\n <img src=\"https://img.shields.io/badge/license-MIT-yellow.svg\" alt=\"license: MIT\" />\n </a>\n</p>\n\nThis package creates a Stripe GraphQL API.\n\n```graphql\nquery {\n stripe {\n customer(id: \"cus_MVBRpmnBis9v5x\") {\n id\n name\n invoices {\n data {\n id\n created\n paid\n hostedInvoiceUrl\n }\n }\n }\n }\n}\n```\n\nYou can also add the Stripe GraphQL API as a Hasura Remote Schema and connect data from your database and Stripe. This allows you to request data from your database and Stripe in a single GraphQL query:\n\n```graphql\nquery {\n users {\n # User in your database\n id\n displayName\n userData {\n stripeCustomerId # Customer's Stripe Customer Id\n stripeCustomer {\n # Data from Stripe\n id\n name\n paymentMethods {\n id\n card {\n brand\n last4\n }\n }\n }\n }\n }\n}\n```\n\n## Install\n\n```bash\nnpm install @nhost/stripe-graphql-js\n```\n\n## Quick Start\n\n### Serverless Function Setup\n\nCreate a new [Serverless Function](https://docs.nhost.io/platform/serverless-functions) `functions/graphql/stripe.ts`:\n\n```js\nimport { createStripeGraphQLServer } from '@nhost/stripe-graphql-js'\n\nconst server = createStripeGraphQLServer()\n\nexport default server\n```\n\n> You can run the Stripe GraphQL API in any JS environment because it's built using [GraphQL Yoga](https://github.com/dotansimha/graphql-yoga).\n\n### Test\n\nTest the Stripe GraphQL API in the browser:\n\n[http://localhost:1337/v1/functions/graphql/stripe](http://localhost:1337/v1/functions/graphql/stripe)\n\n### Remote Schema\n\nAdd the Stripe GraphQL API as a Remote Schema in Hasura.\n\n**URL**\n\n```\n{{NHOST_BACKEND_URL}}/v1/functions/graphql/stripe`\n```\n\n**Headers**\n\n```\nx-nhost-webhook-secret: NHOST_WEBHOOK_SECRET (from env var)\n```\n\n![Hasura Remote Schema](./assets//hasura-remote-schema.png)\n\n## Permissions\n\nHere's a minimal example without any custom permissions. Only requests using the `x-hasura-admin-secret` header will work:\n\n```js\nconst server = createStripeGraphQLServer()\n```\n\nFor more granular permissions, you can pass an `isAllowed` function to the `createStripeGraphQLServer`. The `isAllowed` function takes a `stripeCustomerId` and [`context`](#context) as parameters and runs every time the GraphQL server makes a request to Stripe to get or modify data for a specific Stripe customer.\n\nHere is an example of an `isAllowed` function:\n\n```js\n\nconst isAllowed = (stripeCustomerId: string, context: Context) => {\n const { isAdmin, userClaims } = context\n\n // allow requests if it has a valid `x-hasura-admin-secret`\n if (isAdmin) {\n return true\n }\n\n // get user id\n const userId = userClaims['x-hasura-user-id']\n\n // check if user is signed in\n if (!userId) {\n return false;\n }\n\n // get more user information from the database\n const { user } = await gqlSDK.getUser({\n id: userId,\n });\n\n if (!user) {\n return false;\n }\n\n // check if the user is part of a workspace with the `stripeCustomerId`\n return user.workspaceMembers\n .some((workspaceMember) => {\n return workspaceMember.workspace.stripeCustomerId === stripeCustomerId;\n });\n}\n\n```\n\n### Context\n\nThe `context` object contains:\n\n- `userClaims` - verified JWT claims from the user's access token.\n- `isAdmin` - `true` if the request was made using a valid `x-hasura-admin-secret` header.\n- `request` - [Fetch API Request object](https://developer.mozilla.org/en-US/docs/Web/API/Request) that represents the incoming HTTP request in platform-independent way. It can be useful for accessing headers to authenticate a user\n- `query` - the DocumentNode that was parsed from the GraphQL query string\n- `operationName` - the operation name selected from the incoming query\n- `variables` - the variables that were defined in the query\n- `extensions` - the extensions that were received from the client\n\nRead more about the [default context from GraphQL Yoga](https://www.the-guild.dev/graphql/yoga-server/docs/features/context#default-context).\n\n## Development\n\nInstall dependencies:\n\n```bash\npnpm install\n```\n\nStart the development server:\n\n```bash\npnpm dev\n```\n\nThe GraphQL Server will reload every time the code changes.\n\nOpen GraphiQL:\n\n[http://0.0.0.0:4000/graphql](http://0.0.0.0:4000/graphql)\n"
57
58
  }