Giter VIP home page Giter VIP logo

apollo2-subscriptions-how-to's Introduction

Apollo server 2.0

This project is a good starting point for all the people that want to have apollo-server 2.0 ready to use. It integrates:

  • queries
  • mutations
  • subscriptions (real-time data)
  • authentication and authorisation (using google sign-on as provider server)
  • schema stitching
  • mongo db
  • logger (it will create a file error.log and write on it in case of error - to change the level edit common/util/logger.ts lines 12, 13, 14 )

Installation:

Environment

In the root folder is available a file called .env. It contains all the configuration environment for:

  1. mongodb
  2. google-sign-in as identity server for authentication and authorisation. More info available at http://www.dzurico.com/apollo-server-2-0-auth-with-google-signin/

available commands

  • test environment: npm run dev (it will automatically rerun your code all the time you save and will check for tslint errors and formatting style errors.)
  • production environment: npm run prod

How to configure prettier on your editor:

Webstorm

alt text

VS Code

prettier-vscode can be installed using the extension sidebar. Search for Prettier - Code formatter. It can also be installed using ext install prettier-vscode in the command palette.

Other editors

https://prettier.io/docs/en/editors.html

Playground

  1. hello world
query {
  hello
}
  1. fetch all the posts:
query {
  posts {
    author
    comment
  }
}
  1. add a new post
mutation {
  addPost(author: "Daniele Zurico", comment: "www.dzurico.com") {
    author
    comment
  }
}
  1. realtime data (Subscription) for post
subscription {
  postAdded {
    author
    comment
  }
}

alt text

  1. fetch all the users (you need to be authenticated with google identity server): For more information how to setup: http://www.dzurico.com/apollo-server-2-0-auth-with-google-signin/
query{
  users {
    id
    name
    surname
  }
}

For a better explanation on how I setup this repo please refer to:

  1. http://www.dzurico.com/apolloserver-2-0-how-to-create-a-graphql-server/
  2. http://www.dzurico.com/apollo-server-2-0-auth-with-google-signin/
  3. http://www.dzurico.com/apollo-server-2-0-real-time-data-with-subscriptions/

apollo2-subscriptions-how-to's People

Contributors

trblackw 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  avatar

apollo2-subscriptions-how-to's Issues

Complex Type example

Thanks for your work on this. Can you give an example of how you would accomplish in your app structure?

gql`Post {
    author: Author
}`

Session in webSocket subscriptions

I'm using Apollo Server 2 and Express.js vanilla (with apollo-server-express).

Everything works good also with Subscriptions except the Express session mechanism.

The problem:

I'm using cookie-session (https://github.com/expressjs/cookie-session, but I think this is the same for express-session middleware) and when my browser start a new connection with my server the ApolloServer onConnect hook doesn't have the req attribute and neither req.session and so on...

What I can do is to parse the cookies from webSocket.upgradeReq.headers.cookie in onConnect lifecycle hook, but it seems to me very hacky.

The code:

const { ApolloServer } = require('apollo-server-express')

const typeDefs = require('../src/graphql/types')
const resolvers = require('../src/graphql/resolvers')
const models = require('../src/models')

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req, connection }) => {
    // connection exists only on webSocket connection
    if (connection) {
      return {
        currentUser: connection.context.currentUser // <-- I NEED THIS!
      }
    }
    // if not a (webSocket) connection it is a "default" HTTP call
    return {
      models,
      currentUser: { id: req.user.id }
    }
  },
  subscriptions: {
    onConnect: (connectionParams, webSocket) => {
      // "connectionParams" is from the client but I cannot use it because cookies are HTTP-Only
      // I can retrieve cookies from here: "webSocket.upgradeReq.headers.cookie" but then I need to parse them which seems a bit hacky to me
      // return { currentUser: req.user.id } // <-- I NEED THIS (req.user.id doesn't exists)!
    }
  }
})

module.exports = apolloServer

I can't find anything on Apollo Server Docs site (for other topics very well documented!).

Where am I doing wrong?

StackOverflow question: https://stackoverflow.com/questions/52280481/graphql-subscription-websocket-nodejs-express-session-with-apollo-server-2

ApolloServer 2 production question

Is this subscriptions setup subject to the production notice given here?

"This is not suitable for running in a serious production app, because there is no way to share subscriptions and publishes across many running servers."

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.