Getting Started with Hello World Data Sync

In this example, you add the Data Sync Server library to your Express node.js project, create an index-1.js file, run the server, and query GraphQL.

  • Data Sync Server is a set of Node.js libraries that can be used to build a Data Sync server.

  • Data Sync Server is the starting point for developing a Data Sync application.

Prerequisites
  • You have Node.js and npm installed.

  • You have created a node.js project.

Procedure
  1. Add libraries to your Node.js application:

    $ npm install graphql (1)
    $ npm install express (2)
    $ npm install @aerogear/voyager-server (3)
    1 See https://graphql.org/
    2 See https://expressjs.com/
    3 The Data Sync Server library that enables data sync
  2. Create an index-1.js file with the following content:

    const express = require('express')
    //Include our server libraries
    const { VoyagerServer, gql } = require('@aerogear/voyager-server')
    
    //Provide your graphql schema
    const typeDefs = gql`
      type Query {
        hello: String
      }
    `
    
    //Create the resolvers for your schema
    const resolvers = {
      Query: {
        hello: (obj, args, context, info) => {
          return `Hello world`
        }
      }
    }
    
    //Initialize the library with your Graphql information
    const apolloServer = VoyagerServer({
      typeDefs,
      resolvers
    })
    
    //Connect the server to express
    const app = express()
    apolloServer.applyMiddleware({ app })
    
    app.listen(4000, () =>
      console.log(`🚀 Server ready at http://localhost:4000/graphql`)
    )
  3. Run the server:

    $ node index-1.js
    
    🚀 Server ready at http://localhost:4000/graphql
  4. Browse http://localhost:4000/graphql and interact with the playground. For example:

    {
      hello
    }
  5. Check the output. For the example above, the output should be:

    {
      "data": {
        "hello": "Hello world"
      }
    }

To get started with the Data Sync framework, see the sample application. In this app, you can explore a more complex schema.

Before proceeding, make sure you have an understanding of the following GraphQL concepts:

  • Schema design

  • Resolvers

  • Subscriptions