GraphQL Ruby Client for JavaScript

1.14.9 · active · verified Tue Apr 21

The `graphql-ruby-client` is a JavaScript client library specifically designed to integrate with `graphql-ruby` servers. It facilitates frontend interactions by generating JavaScript modules from `.graphql` query, mutation, and subscription files, which are then consumed by popular client-side GraphQL libraries such as Apollo Client, Relay, or urql. Currently at version 1.14.9, the package demonstrates an active, incremental release cadence focused on bug fixes and feature enhancements, with the latest update being a couple of months ago. Its key differentiator lies in its tight integration with `graphql-ruby`, offering specialized runtime utilities like fetchers for various subscription backends (e.g., Ably, Pusher) and tooling for persisted queries, which are optimized for the Ruby GraphQL ecosystem. This makes it a primary choice for JavaScript frontends interfacing with Ruby-based GraphQL APIs, particularly within Rails applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up Apollo Client with `graphql-ruby-client`'s `createFetcher` and execute a mock generated GraphQL query against a GraphQL Ruby server. It shows basic client instantiation and a query execution pattern.

import { ApolloClient, InMemoryCache, ApolloProvider, HttpLink } from '@apollo/client';
import { createFetcher } from 'graphql-ruby-client';

// Assuming 'QueryName' and 'QueryName_variables' are generated from your .graphql files
// In a real application, these would be imported from your generated code.
const GENERATED_QUERY = {
  id: 'some-unique-query-id',
  document: `query SomeQuery($id: ID!) {
    user(id: $id) {
      id
      name
      email
    }
  }`,
  ast: { /* full GraphQL AST would be here */ },
  variables: (id) => ({ id }),
  operationName: 'SomeQuery'
};

// Configure the HTTP endpoint for your GraphQL Ruby server
const httpLink = new HttpLink({ uri: 'http://localhost:3000/graphql' });

// Create a fetcher compatible with Apollo Link, using graphql-ruby-client's utility
// This will handle the specific serialization/deserialization for graphql-ruby
const clientFetcher = createFetcher({ 
  link: httpLink,
  // In a production setup, you'd likely pass a more robust link chain
});

const client = new ApolloClient({
  link: clientFetcher,
  cache: new InMemoryCache(),
});

// Example of executing a generated query
async function fetchUser(userId: string) {
  try {
    const { data } = await client.query({
      query: GENERATED_QUERY.document,
      variables: GENERATED_QUERY.variables(userId),
      operationName: GENERATED_QUERY.operationName,
    });
    console.log('Fetched User:', data.user);
    return data.user;
  } catch (error) {
    console.error('Error fetching user:', error);
    throw error;
  }
}

// Example usage:
fetchUser('123').then(user => {
  if (user) {
    console.log(`User name: ${user.name}`);
  }
});

view raw JSON →