GithubHelp home page GithubHelp logo

miguelramosfdz / parse-graphql-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bakery/parse-graphql-server

0.0 1.0 0.0 27 KB

GraphQL integration for Parse Server

License: MIT License

JavaScript 94.97% HTML 5.03%

parse-graphql-server's Introduction

GraphQL for Parse Server

Build Status Code Climate Test Coverage Dependency Status devDependency Status

When to use

  • you are using Parse SDK (web or mobile) coupled with Parse Server running on Express
  • you are using GraphQL
  • you want to control access to Parse models in your resolvers using Parse ACL system
  • you are using parse-graphql-client package

Quick start

npm install --save graphql parse-graphql-server parse
import express from 'express';
import Parse from 'parse/node';
import parseGraphQLHTTP from 'parse-graphql-server';
import {
  GraphQLObjectType,
  GraphQLSchema,
  GraphQLID,
  GraphQLString,
  GraphQLBoolean,
  GraphQLList,
  GraphQLNonNull,
} from 'graphql';

const Todo = Parse.Object.extend('Todo');

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  description: 'Item in todo list',
  fields: () => ({
    id: {
      type: GraphQLID,
    },
    text: {
      type: GraphQLString,
      resolve: todo => todo.get('text'),
    },
    isComplete: {
      type: GraphQLBoolean,
      resolve: todo => todo.get('isComplete'),
    },
  }),
});

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      todos: {
        type: new GraphQLList(TodoType),
        resolve: (_, args, { user, Query }) => {
          const query = new Query(Todo);
          return query.find();
        },
      },
    },
  }),
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      addTodo: {
        type: Todo.SchemaType,
        description: 'Create a new todo item',
        args: {
          text: { type: new GraphQLNonNull(GraphQLString) },
        },
        resolve: (_, { text }, { Query, user }) => {
          const newTodo = new Query(Todo).create({ text, isComplete: false });
          if (user) {
            newTodo.setACL(new Parse.ACL(user));
          }
          return newTodo.save().then(td => td);
        },
      },
      deleteTodo: {
        type: Todo.SchemaType,
        description: 'Delete a todo',
        args: {
          id: { type: new GraphQLNonNull(GraphQLID) },
        },
        resolve: (_, { id }, { Query }) =>
          new Query(Todo).get(id).then((todo) => {
            if (todo) {
              return todo.destroy();
            }
            return todo;
          }),
      },
    },
  }),
});

const app = express();
app.use('/graphql', parseGraphQLHTTP({ schema, graphiql: true, }));

app.listen(process.env.PORT, () => {
  console.log('server running');
});

Writing resolvers

When using Parse GraphQL server package, all your GraphQL resolvers include authentication information and updated version of Parse.Query that passes ACL info along to make sure your queries are authenticated

{
  resolve: (_, args, { Query, user }) => {
    // **Query** is a patched version of Parse.Query that
    // includes session token information for the currently authenticated 
    // user (if any)
    // It also extends basic Parse.Query to include a **create** method:
    // const newTodo = new Query(Todo).create({ text, isComplete: false });
    //
    // **user** is an instance of Parse.User set to the authenticated user (if any)
  },
}

Client side

Parse GraphQL server looks for Authorization header set to the session token of the current Parse User. Parse GraphQL client sets this up for you automatically.

See it in action

Credits

Parse GraphQL server relies heavily on Express GraphQL package.

parse-graphql-server's People

Contributors

callmephilip avatar

Watchers

 avatar

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.