第2章 Data Sync クライアントを使用した Data Sync サーバーのクエリー


このセクションでは、Voyager Client を使用して、Voyager サーバーアプリケーションと通信できるモバイルおよび Web アプリケーションを作成する方法について説明します。

Data Sync は、JavaScript アプリケーションを Data Sync も使用するサーバーと統合する JavaScript ライブラリーを提供します。クライアントライブラリーは、Apollo client に基づいています。

ライブラリーをモバイルプロジェクトに追加し、クライアントクラスを設定し、サーバーに接続して、それが機能することを確認します。

前提条件

  • Node.js と npm がインストールしました。
  • webpack getting started ガイドを使用して、ES6 をサポートする空の Web プロジェクトを作成しました。
  • サーバー入門ガイドを完了し、アプリケーションを実行しました。

手順

  1. アドレス帳サーバーを作成します。

    1. 次のコンテンツで index-2.js ファイルを作成します。

      const express = require('express')
      //Include our server libraries
      const { VoyagerServer, gql } = require('@aerogear/voyager-server')
      
      //Provide your graphql schema
      const typeDefs = gql`
      type Query {
        info: String!
        addressBook: [Person!]!
      }
      
      type Mutation {
        post(name: String!, address: String!): Person!
      }
      
      type Person {
        id: ID!
        address: String!
        name: String!
      }
      `
      
      let persons = [{
        id: 'person-0',
        name: 'Alice Roberts',
        address: '1 Red Square, Waterford'
      }]
      
      let idCount = persons.length
      const resolvers = {
        Query: {
          info: () => `This is a simple example`,
          addressBook: () => persons,
        },
        Mutation: {
      
          post: (parent, args) => {
             const person = {
              id: `person-${idCount++}`,
              address: args.address,
              name: args.name,
            }
            persons.push(person)
            return person
          }
        },
      }
      
      //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`)
      )
      Copy to Clipboard Toggle word wrap
    2. サーバーを実行します。

      $ node index-2.js
      
      🚀 Server ready at http://localhost:4000/graphql
      Copy to Clipboard Toggle word wrap
    3. http://localhost:4000/graphql を参照してプレイグラウンドを操作します。以下に例を示します。

      {
        addressBook {
          name
          address
      
        }
      }
      Copy to Clipboard Toggle word wrap
    4. 出力を確認します。上記の例では、出力は次のようになります。

      {
        "data": {
          "addressBook": [
            {
              "name": "Alice Roberts",
              "address": "1 Red Square, Waterford"
            }
          ]
        }
      }
      Copy to Clipboard Toggle word wrap
  2. 次のライブラリーを JavaScript クライアントに追加します。

    npm install @aerogear/voyager-client
    npm install graphql
    npm install graphql-tag
    Copy to Clipboard Toggle word wrap
    注記

    前提条件は、webpack getting started ガイドを使用して、ES6 をサポートする空の Web プロジェクトを作成していることです。

  3. index.js ファイルを作成して、手順 1 と同じクエリーを作成しますが、JavaScript を使用します。

    この例では、設定オブジェクトが作成され、httpUrl フィールドが Voyager サーバーアプリケーションの URL に設定されています。クライアントアプリケーションがサブスクリプションを使用する場合は、wsUrl フィールドも必要です。

    src/index.js

    // gql is a utility function that handles gql queries
    import gql from 'graphql-tag';
    
    import { OfflineClient } from '@aerogear/voyager-client';
    
    // connect to the local service.
    let config = {
      httpUrl: "http://localhost:4000/graphql",
      wsUrl: "ws://localhost:4000/graphql",
    }
    
    async function queryPeople() {
    
      // Actually create the client
      let offlineClient = new OfflineClient(config);
      let client = await offlineClient.init();
    
      // Execute the query
      client.query({
          fetchPolicy: 'network-only',
          query: gql`
          query addressBook{
            addressBook{
            name
            address
            }
          }
          `
        })
        //Print the response of the query
        .then( ({data}) => {
          console.log(data.addressBook)
        });
    }
    
    queryPeople();
    Copy to Clipboard Toggle word wrap

  4. クライアントアプリケーションをビルドして実行します。
  5. クライアントアプリケーションを参照し、コンソール出力を確認します。

    次のような配列を含める必要があります。

    address: "1 Red Square, Waterford"
    name: "Alice Roberts"
    __typename: "Person"
    Copy to Clipboard Toggle word wrap
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat
トップに戻る