Giter VIP home page Giter VIP logo

prisma-serverless's Introduction

Prisma Serverless

Template for Prisma (Nexus) + Typescript + AWS Serverless Lambda = πŸŽ†

This boilerplate includes:

There's more to come πŸ‘€

Table of contents

Project Structure

.
β”œβ”€β”€ config/                     # Serverless env
β”œβ”€β”€ prisma/                     # Prisma related config & Datamodel
β”œβ”€β”€ src/                        # The magic happens here
β”‚   β”œβ”€β”€ generated/              # Generated prisma schema & client
β”‚   β”œβ”€β”€ permissions/            # Resolvers permissions
β”‚   └── resolvers/              # App resolvers
β”‚       β”œβ”€β”€ Mutation/           # Resolvers mutations
β”‚       └── Query/              # Resolvers queries
β”œβ”€β”€ typings/                    # Typescript generic typings
β”œβ”€β”€ .babelrc                    # babel config
β”œβ”€β”€ .editorconfig               # editor config
β”œβ”€β”€ .eslintrc.js                # eslint config
β”œβ”€β”€ .gitignore                  # git ignore list
β”œβ”€β”€ .nvmrc                      # nvm config
β”œβ”€β”€ .prettierrc                 # Prettier configuration
β”œβ”€β”€ docker-compose.yml          # Docker compose file (for local server)
β”œβ”€β”€ package.json                # build scripts and dependencies
β”œβ”€β”€ README.md                   # This file ;)
β”œβ”€β”€ serverless.yml              # Serverless configuration
β”œβ”€β”€ tsconfig.json               # Typescript configuration
└── yarn.lock                   # yarn lock file

Quickstart

Prerequisites

You'll need a prisma server setup to use this template. You can either create a local prisma server using Docker:

docker-compose up -d

Or deploy to a new prisma demo server:

yarn prisma deploy

Then, create an .env referencing the prisma endpoint:

PRISMA_ENDPOINT="http://localhost:4469/app/dev"
PRISMA_SECRET="PRISMA_SECRET"
APP_SECRET="MY_APP_SECRET"
# install dependencies
yarn install

# Run local lambda http environment (API Gateway simulator)
yarn dev

# Run tests
yarn run test

Deploying

  1. Deploy & Generate the prisma schema based on the datamodel to the prisma server.
# Deploy the schema to the prisma server & generate prisma client & schema
yarn prisma deploy

# Resolvers deployment

# Deploy (development)
yarn deploy:dev

# Deploy (production)
yarn deploy:prod

Help

prisma-serverless's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar gomah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

prisma-serverless's Issues

Generate type files on save

Hi,

First of all. This is an amazing repo that is super helpful to start a project with a similar stack.

It is more a question than a bug. I realized that the type files for both TS and GrapqhQL are (re)generated only when a new GraphQL query is executed.
If I look at the examples from nexus repo like this one, I noticed they are using ts-node-dev

Did you find any way to achieve the same behavior using the serverless-offline plugin?
It is quite a big drawback not to have the TS types available without having to run the server first.

Cheers

Q: How to do both Authentication & Authorization in Playground?

I have deployed the Prisma server locally in Docker with managementApiSecret and also defined it in prisma.yml. Now, there are two issues I'm facing.

  1. Prisma server link http://localhost:4466 from Docker is accessible without setting any header. How can I secure that?

  2. When Graphql Shield is disabled I can query documents on http://localhost:1337 by setting the Authorization header, obtained by using the command prisma token. The problem appears when I enable Graphql Shield. After this for any and all requests I'm getting:

"message": "Not Authorised!"

even for the resolvers that are marked as allow.

docker-compose.yml

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.34
    restart: always
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        managementApiSecret: PRISMA_SECRET
        port: 4466
        databases:
          default:
            connector: mongo
            database: tcf
            uri: 'mongodb://host.docker.internal/admin'

prisma.yml

# The endpoint of your Prisma API (deployed to a Prisma Sandbox).
endpoint: ${env:PRISMA_ENDPOINT}

# The file containing the definition of your data model.
datamodel:
  - datamodel/users.prisma
  - datamodel/school.prisma
  - datamodel/member.prisma
  - datamodel/group.prisma
  - datamodel/curriculum.prisma
  - datamodel/pillar.prisma
  - datamodel/question.prisma
  - datamodel/scheduler.prisma
  - datamodel/userCurriculum.prisma
  - datamodel/userPillar.prisma
  - datamodel/personalSurvey.prisma
  - datamodel/score.prisma
  - datamodel/survey.prisma
  - datamodel/surveyGroup.prisma
  - datamodel/wblibrary.prisma
  - datamodel/otp.prisma

# Specifies the type of the database
databaseType: document

# Generate
generate:
  - generator: typescript-client
    output: ../src/generated/prisma-client/
  - generator: graphql-schema
    output: ../src/generated/prisma.graphql

hooks:
  post-deploy:
    - prisma generate --endpoint
    - npx nexus-prisma-generate --client ./src/generated/prisma-client --output ./src/generated/nexus-prisma # Runs the codegen tool from nexus-prisma.

secret: PRISMA_SECRET

.env

PRISMA_ENDPOINT="http://localhost:4466/app/dev"
PRISMA_SECRET=PRISMA_SECRET
APP_SECRET=PRISMA_SECRET
PRISMA_MANAGEMENT_API_SECRET=PRISMA_SECRET

permissions/index.ts

export const permissions = shield({
  Query: {
    // Global
    '*': or(rules.isSchoolAdmin, rules.isSchoolTeacher, rules.isSuperAdmin),
    profile: rules.isUser,
  },
  Mutation: {
    '*': or(rules.isSchoolAdmin, rules.isSchoolTeacher, rules.isSuperAdmin),
    login: allow,
    signup: allow,
  },
}, { fallbackRule: deny });

Thanks for your help πŸ™

Not working after deployed

It is working well on local by running sls offline.
But after I deploy with sls deploy and open the lambda url, it shows Server cannot be reached error. (screenshot below)

image

I also checked cloudwatch logs but cannot get any idea from the log. Here is the error log on cloud watch.

[Error: ENOENT: no such file or directory, mkdir] errno: -2, code: 'ENOENT', syscall: 'mkdir'

Do I need to set any additional configuration?

Q: How to access the endpoint after deploying?

I have deployed Prisma Server on AWS Fargate and deployed the Client using the command:

yarn deploy:dev

Output

Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: nexus-prisma@^0.3.7, graphql@^14.5.4, nexus@^0.11.7, jsonwebtoken@^8.5.1, bcryptjs@^2.4.3, apollo-server-lambda@^2.9.2, graphql-middleware@^4.0.1, prisma-client-lib@^1.34.8
Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service graphql.zip file to S3 (29.13 MB)...
Serverless: Recoverable error occurred (write EPIPE), sleeping for 5 seconds. Try 1 of 4
✨  Done in 268.43s.

I can see the stack created in CloudFormation, but there is no external link or endpoint in output.

CleanShot 2019-09-05 at 15 56 30@2x

Thanks πŸ™

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.