All Projects → realm → Realm Graphql

realm / Realm Graphql

Licence: apache-2.0
GraphQL client for Realm Object Server

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Realm Graphql

Realm Graphql Service
GraphQL service for Realm Object Server
Stars: ✭ 44 (-44.3%)
Mutual labels:  graphql, reactive, realm
A Pop
🎶 HD Music Streaming and Sharing Web App
Stars: ✭ 55 (-30.38%)
Mutual labels:  graphql, apollo-client
Apollo Client Devtools
Apollo Client browser developer tools.
Stars: ✭ 1,210 (+1431.65%)
Mutual labels:  graphql, apollo-client
Graphql Apollo Next.js
A server-rendering GraphQL client with Apollo Client and Next.js
Stars: ✭ 59 (-25.32%)
Mutual labels:  graphql, apollo-client
Githunt React
[DEPRECATED] 🔃 An example app frontend built with Apollo Client and React
Stars: ✭ 1,036 (+1211.39%)
Mutual labels:  graphql, apollo-client
Apollo Angular
A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
Stars: ✭ 1,058 (+1239.24%)
Mutual labels:  graphql, apollo-client
Starter
Opinionated SaaS quick-start with pre-built user account and organization system for full-stack application development in React, Node.js, GraphQL and PostgreSQL. Powered by PostGraphile, TypeScript, Apollo Client, Graphile Worker, Graphile Migrate, GraphQL Code Generator, Ant Design and Next.js
Stars: ✭ 1,082 (+1269.62%)
Mutual labels:  graphql, apollo-client
Persistgraphql Webpack Plugin
PersistGraphQL Webpack Plugin
Stars: ✭ 39 (-50.63%)
Mutual labels:  graphql, apollo-client
Github Graphql React
A Github client built with React / GraphQL 💻👩‍💻💽 👨‍💻
Stars: ✭ 60 (-24.05%)
Mutual labels:  graphql, apollo-client
Android Okgraphql
Reactive GraphQl client for Android
Stars: ✭ 64 (-18.99%)
Mutual labels:  graphql, reactive
Cynthesize Frontend
Frontend written in Angular 7 and deployed GraphQL for Cynthesize. Development build: https://cynthesize-develop.netlify.com
Stars: ✭ 65 (-17.72%)
Mutual labels:  graphql, apollo-client
Apollo Cache Invalidation
Experimental cache invalidation tools for Apollo.
Stars: ✭ 66 (-16.46%)
Mutual labels:  graphql, apollo-client
Fullstack Tutorial
🚀 The Apollo platform tutorial app
Stars: ✭ 1,007 (+1174.68%)
Mutual labels:  graphql, apollo-client
Apollo Invalidation Policies
An extension of the Apollo 3 cache with support for type-based invalidation policies.
Stars: ✭ 55 (-30.38%)
Mutual labels:  graphql, apollo-client
Rxrealm
RxSwift extension for RealmSwift's types
Stars: ✭ 1,007 (+1174.68%)
Mutual labels:  reactive, realm
Blaze Apollo
Blaze integration for the Apollo Client
Stars: ✭ 56 (-29.11%)
Mutual labels:  graphql, apollo-client
React Boilerplate
⚛ The stable base upon which we build our React projects at Mirego.
Stars: ✭ 39 (-50.63%)
Mutual labels:  graphql, apollo-client
Crypto Grommet
Crypto and equities app
Stars: ✭ 39 (-50.63%)
Mutual labels:  graphql, apollo-client
Guide To Graphql
A Frontend Developer's Guide to GraphQL (Fluent Conf 2018)
Stars: ✭ 59 (-25.32%)
Mutual labels:  graphql, apollo-client
Artemis Dev Tool
An Apollo GraphQL Query Schema Testing Tool
Stars: ✭ 66 (-16.46%)
Mutual labels:  graphql, apollo-client

Realm GraphQL Client

A set of helper methods and classes to make it easier to use the Apollo GraphQL client with the Realm Object Server.

Using the Client

The Realm GraphQL client provides a few helper and convenience API to make it easier to consume the Realm Object Server GraphQL API with the Apollo Client.

Prerequisites

Add the apollo-link, apollo-link-http, apollo-link-ws, and subscriptions-transport-ws packages to your project:

npm install graphql apollo-link apollo-link-http apollo-link-ws apollo-utilities subscriptions-transport-ws --save

Then, add the Realm GraphQL client package:

npm install realm-graphql-client --save

Getting Started

Authenticating the user

To start consuming the GraphQL API, you'll need to login a user:

import { Credentials, User } from 'realm-graphql-client';

const credentials = Credentials.usernamePassword('SOME-USERNAME', 'SOME-PASSWORD');
const user = await User.authenticate(credentials, 'http://my-ros-instance:9080');

Other credential providers are supported, such as JWT, Facebook, Google etc. They are all exposed as factories on the Credentials class.

After you have your user, you can create a helper config that will handle token refreshes and authentication:

import { GraphQLConfig } from 'realm-graphql-client';

const config = await GraphQLConfig.create( 
  user,
  '/~/test'
);

Note that each config is created per Realm path, so if you need to query multiple Realms, you'll need to obtain a config instance for each of them.

Setting up the Client

Once you have a config, you can use that to create an Apollo client instance and configure it. The config exposes 4 properties:

  • httpEndpoint: This is the endpoint you'll use to execute queries and mutations against. It can be used to configure Apollo's httpLink.
  • authLink: This is a link that provides an Authorization header for the user/path combination. It should be composed together with your httpLink.
  • webSocketEndpoint: This is the endpoint you'll use to execute subscriptions against. It can be used to configure Apollo's WebSocket Link.
  • connectionParams: This is a function that will provide an authorization object each time a websocket connection is established. You should pass that directly (without invoking it) to the WebSocketLink's constructor's options.

Let's look at a small example. First, let's configure the httpLink that we'll use for querying and mutating:

import { HttpLink } from 'apollo-link-http';
import { concat } from 'apollo-link';

const httpLink = concat(
    config.authLink,
    // Note: if using node.js, you'll need to provide fetch as well.
    new HttpLink({ uri: config.httpEndpoint })
  );

Then, let's configure the websocket link that we'll use for subscriptions:

import { WebSocketLink } from 'apollo-link-ws';

// Note: if using node.js, you'll need to provide webSocketImpl as well.
const webSocketLink = new WebSocketLink({
  uri: config.webSocketEndpoint,
  options: {
    connectionParams: config.connectionParams,
  }
});

Finally, we need to use split to direct subscriptions to the websocket link and queries and mutations to the http link:

import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';

const link = split(
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },
  webSocketLink,
  httpLink,
);

// Finally, create the client
client = new ApolloClient({
  link: link,
  cache: new InMemoryCache()
});

Using the client

Now that you have configured your client, you can use to access the ROS GraphQL API.

Queries

Querying data is as simple as invoking client.query():

const query = gql`
  query {
    companies {
      companyId
      name
      address
    }
  }
`;

const response = await client.query({
  query: query
});

const companies = response.data.companies;

For a complete list of supported query operations, refer to the GraphQL Server docs.

For a detailed documentation on the Apollo Client query capabilities, refer to the Apollo docs.

For a comprehensive documentation on the query language syntax, refer to the Realm JS Query Language docs.

Note: Server Realms (used by GraphQL) don't have named backlinks, even if the client Realms had them defined, so you'll need to use the fully qualified backlink syntax, e.g.:

{
  golfers(query: "@links.Club.golfers.id = 'some-club-id'") {
    firstName
    lastName
  }
}

Mutations

Mutating data happens when you invoke the client.mutate() method:

const mutation = gql`
  mutation {
    result: addCompany(input: {
      companyId: "some-unique-id"
      name: "My Amazing Company"
      address: "Mars"
    }) {
      companyId
      name
      address
    }
  }
`

const response = await client.mutate({
  mutation: mutation
});

const addedCompany = response.data.result;

For a complete list of supported mutation operations, refer to the GraphQL Server docs.

For a detailed documentation on the Apollo Client query capabilities, refer to the Apollo docs.

Subscriptions

Subscribing for changes happens when you invoke the client.subscribe() method. You get an Observable sequence you can then add an observer to:

const observable = await client.subscribe({
  query: gql`
    subscription {
      companies${additionalParameters || ''} {
        companyId
        name
        address
      }
    }
  `
});

observable.subscribe({
  next(data) {
    const companies = data.data.companies;
    // Update you UI
  },
  error(value) {
    // Notify the user of the failure
  }
});

For a complete list of supported mutation operations, refer to the GraphQL Server docs.

For a detailed documentation on the Apollo Client query capabilities, refer to the Apollo docs.

Developing the client

Commands for Building, Cleaning, Testing, Linting and Watching

After npm install

  1. To Build npm run build
  2. To Clean Artifacts npm run clean
  3. To Test npm run test
  4. To Lint npm run lint
  5. To Buld and Watch when you make changes npm run watch

Debugging with Visual Studio Code

  1. Set a breakpoint in your code ending in .ts or your test ending in .spec.ts
  2. Run Either src/index.ts or All Tests in the debug pane.

Some Advice

Never use arrow functions on the Mocha test handlers. Why? The callback has its own set of methods. The arrow function does not have an easy way to reference this

Say you want to increase the timeout of the function callback in your tests

it('should do something', () => {
    this.timeout(2000) // this is not the callback function!
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].