Code Monkey home page Code Monkey logo

graphql-platform's Introduction

GraphQL Platform is an OpenCRUD implementation

This @prismamedia/graphql-platform aims to provide a complete and flexible GraphQL implementation of the OpenCRUD specification. Given a set of nodes' definition, it will generate an executable GraphQL schema made easy to expose through an Apollo Server.

Build status

Requirements

  • NodeJS >=20.10
  • GraphQL >=16

Installation

with npm

npm install --save @prismamedia/graphql-platform @prismamedia/graphql-platform-connector-mariadb

or with yarn

yarn add @prismamedia/graphql-platform @prismamedia/graphql-platform-connector-mariadb

Usage

import {
  GraphQLPlatform,
  OnEdgeHeadDeletion,
} from '@prismamedia/graphql-platform';
import { MariaDBConnector } from '@prismamedia/graphql-platform-connector-mariadb';
import { randomUUID } from 'node:crypto';

export const ArticleStatusType = new GraphQLEnumType({
  name: 'ArticleStatus',
  values: {
    DRAFT: { value: 'draft' },
    PUBLISHED: { value: 'published' },
  },
});

export type MyUser = {
  id: string;
  name: string;
  role?: 'ADMIN' | 'JOURNALIST';
};

export type MyContext = {
  user?: MyUser;
};

const gp = new GraphQLPlatform<MyContext>({
  nodes: {
    Article: {
      authorization({ user }, mutationType) {
        if (user) {
          switch (user.role) {
            case 'ADMIN':
              // They can do as they please
              return true;

            case 'JOURNALIST':
              return mutationType
                ? mutationType === utils.MutationType.CREATION
                  ? // They can "create" new articles
                    true
                  : mutationType === utils.MutationType.UPDATE
                    ? // They can "update" the articles they have created
                      { createdBy: { id: user.id } }
                    : // They cannot "delete" articles
                      false
                : // They can "query" all the articles
                  true;

            default:
              // The authenticated users can "query" the published articles but cannot mutate anything
              return mutationType ? false : { status: 'published' };
          }
        }

        // Un-authenticated users cannot access articles at all
        return false;
      },

      components: {
        id: {
          type: 'UUIDv4',
          description: 'This UUID identifies an Article publicly',
          nullable: false,
          mutable: false,

          creation: { defaultValue: () => randomUUID() },
        },
        status: {
          type: ArticleStatusType,
          nullable: false,

          creation: { defaultValue: ArticleStatus.DRAFT },
        },
        title: {
          type: 'NonEmptyTrimmedString',
          nullable: false,
        },
        category: {
          kind: 'Edge',
          head: 'Category',
          onHeadDeletion: OnEdgeHeadDeletion.SET_NULL,
        },
        createdBy: {
          kind: 'Edge',
          head: 'Author',
          nullable: false,
          mutable: false,

          creation: {
            public: false,
          },
        },
      },
      uniques: [['id']],
    },
    Category: {
      components: {
        id: {
          type: 'UUIDv4',
          description: 'This UUID identifies a Category publicly',
          nullable: false,
          mutable: false,

          creation: { defaultValue: () => randomUUID() },
        },
        title: {
          kind: 'Leaf',
          type: 'NonEmptyTrimmedString',
          nullable: false,
        },
      },
      uniques: [['id']],
    },
    Author: {
      components: {
        id: {
          type: 'UUIDv4',
          description: 'This UUID identifies an Author publicly',
          nullable: false,
          mutable: false,

          creation: { defaultValue: () => randomUUID() },
        },
        name: {
          kind: 'Leaf',
          type: 'NonEmptyTrimmedString',
          nullable: false,
        },
      },
      uniques: [['id']],
    },
  },

  connector: (gp) => new MariaDBConnector(gp),
});

Expose it through Apollo Server

Given the gp instance above:

import { ApolloServerIntegration } from '@prismamedia/graphql-platform-integration-apollo-server';

// `server` is an Apollo Server instance
const server = new ApolloServerIntegration(gp);

// you can now expose it as you wish, AWS Lambda, ExpressJS, ...

graphql-platform's People

Contributors

yvann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

graphql-platform's Issues

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.