github.com/anycable/anycable-go@v1.5.1/docs/apollo.md (about)

     1  # Apollo GraphQL support
     2  
     3  <p class="pro-badge-header"></p>
     4  
     5  AnyCable can act as a _translator_ between Apollo GraphQL and Action Cable protocols (used by [GraphQL Ruby][graphql-ruby]).
     6  
     7  That allows us to use the variety of tools compatible with Apollo: client-side libraries, IDEs (such as Apollo Studio).
     8  
     9  > See also the [demo](https://github.com/anycable/anycable_rails_demo/pull/18) of using AnyCable with Apollo GraphQL React Native application.
    10  
    11  ## Usage
    12  
    13  Run `anycable-go` with `graphql_path` option specified as follows:
    14  
    15  ```sh
    16  $ anycable-go -graphql_path=/graphql
    17  
    18   ...
    19   INFO 2021-05-11T11:53:01.186Z context=main Handle GraphQL WebSocket connections at http://localhost:8080/graphql
    20   ...
    21  
    22  # or using env var
    23  $ ANYCABLE_GRAPHQL_PATH=/graphql anycable-go
    24  ```
    25  
    26  Now your Apollo-compatible\* GraphQL clients can connect to the `/graphql` endpoint to consume your GraphQL API.
    27  
    28  \* Currently, there are two protocol implementations supported by AnyCable: [graphql-ws][] and (legacy) [subscriptions-transport-ws][].
    29  
    30  GraphQL Ruby code stays unchanged (make sure you use [graphql-anycable][] plugin).
    31  
    32  Other configuration options:
    33  
    34  **--graphql_channel** (`ANYCABLE_GRAPHQL_CHANNEL`)
    35  
    36  GraphQL Ruby channel class name (default: `"GraphqlChannel"`).
    37  
    38  **--graphql_action** (`ANYCABLE_GRAPHQL_ACTION`)
    39  
    40  GraphQL Ruby channel action name (default: `"execute"`).
    41  
    42  ## Client configuration
    43  
    44  We test our implementation against the official Apollo WebSocket link configuration described here: [Get real-time updates from your GraphQL server][apollo-subscriptions].
    45  
    46  ## Authentication
    47  
    48  Apollo GraphQL supports passing additional connection params during the connection establishment. For example:
    49  
    50  ```js
    51  import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
    52  import { createClient } from 'graphql-ws';
    53  
    54  const wsLink = new GraphQLWsLink(createClient({
    55    url: 'ws://localhost:8080/graphql',
    56    connectionParams: {
    57      token: 'some-token',
    58    },
    59  }));
    60  ```
    61  
    62  AnyCable passes these params via the `x-apollo-connection` HTTP header, which you can access in your `ApplicationCable::Connection#connect` method:
    63  
    64  ```ruby
    65  module ApplicationCable
    66    class Connection < ActionCable::Connection::Base
    67      identified_by :user
    68  
    69      def connect
    70        user = find_user
    71        reject_unauthorized_connection unless user
    72  
    73        self.user = user
    74      end
    75  
    76      private
    77  
    78      def find_user
    79        header = request.headers["x-apollo-connection"]
    80        return unless header
    81  
    82        # Header contains JSON-encoded params
    83        payload = JSON.parse(header)
    84  
    85        User.find_by_token(payload["token"])
    86      end
    87    end
    88  end
    89  ```
    90  
    91  Note that the header contains JSON-encoded connection params object.
    92  
    93  ### Using with JWT identification
    94  
    95  You can use [JWT identification](./jwt_identification.md) along with Apollo integration by specifying the token either via query params (e.g., `ws://localhost:8080/graphql?jid=<token>`) or by passing it along with connection params like this:
    96  
    97  ```js
    98  const wsLink = new GraphQLWsLink(createClient({
    99    url: 'ws://localhost:8080/graphql',
   100    connectionParams: {
   101      jid: '<token>',
   102    },
   103  }));
   104  ```
   105  
   106  [subscriptions-transport-ws]: https://github.com/apollographql/subscriptions-transport-ws
   107  [apollo-subscriptions]: https://www.apollographql.com/docs/react/data/subscriptions/
   108  [graphql-ruby]: https://graphql-ruby.org
   109  [graphql-anycable]: https://github.com/anycable/graphql-anycable
   110  [graphql-ws]: https://github.com/enisdenjo/graphql-ws