Skip to content

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed May 6, 2024
1 parent 51888cc commit 1be259a
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions website/src/pages/docs/data/subscriptions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ Because subscriptions maintain a persistent connection, they can't use the defau
that Apollo Client uses for queries and mutations. Instead, Apollo Client subscriptions most
commonly communicate over WebSocket.

Install the community-maintained
[`graphql-ws`](https://github.com/enisdenjo/graphql-ws) library.
Install the community-maintained [`graphql-ws`](https://github.com/enisdenjo/graphql-ws) library.

```sh
npm install graphql-ws
Expand All @@ -90,24 +89,24 @@ npm install graphql-ws
Let's look at how to add support for this transport to Apollo Client.

```ts
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
import { createClient } from "graphql-ws";
import { createClient } from 'graphql-ws';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';

const ws = new GraphQLWsLink(
createClient({
url: "ws://localhost:3000/graphql"
})
url: 'ws://localhost:3000/graphql',
}),
);
```

```ts
import { type ApolloClientOptions, InMemoryCache, split } from "@apollo/client/core";
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
import { getMainDefinition } from "@apollo/client/utilities";
import { APOLLO_OPTIONS } from "apollo-angular";
import { HttpLink } from "apollo-angular/http";
import { Kind, OperationTypeNode } from "graphql";
import { createClient } from "graphql-ws";
import { APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLink } from 'apollo-angular/http';
import { Kind, OperationTypeNode } from 'graphql';
import { createClient } from 'graphql-ws';
import { InMemoryCache, split, type ApolloClientOptions } from '@apollo/client/core';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { getMainDefinition } from '@apollo/client/utilities';

@NgModule({
providers: [
Expand All @@ -122,23 +121,23 @@ import { createClient } from "graphql-ws";
// Create a WebSocket link:
const ws = new GraphQLWsLink(
createClient({
url: "ws://localhost:3000/graphql"
})
url: 'ws://localhost:3000/graphql',
}),
);

// Using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
// Split based on operation type
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === Kind.OPERATION_DEFINITION &&
definition.operation === OperationTypeNode.SUBSCRIPTION
);
},
ws,
http
const definition = getMainDefinition(query);
return (
definition.kind === Kind.OPERATION_DEFINITION &&
definition.operation === OperationTypeNode.SUBSCRIPTION
);
},
ws,
http,
);

return {
Expand Down

0 comments on commit 1be259a

Please sign in to comment.