In a vanilla ES6 project, using no framework:
// Configuration
import { Client } from 'absinthe-phoenix';
const client = new Client({
uri: "http://localhost:3010/graphql",
wsUri: "ws://localhost:3010/socket"
});
client.connect() // for websocket
.then(() => console.log('Connected.')
.catch(e => console.error(`Couldn't connect`, e));
// Example subscription
const subscription = `
subscription {
commentAdded(repoFullName: "apollographql/apollo-client") {
repoName
}
}
`;
client.subscribe({query: subscription}, response => {
// Executed every time this subscription is broadcast
console.log('Got subscription data', response);
})
.then(() => console.log("Subscribed."))
.catch(e => console.error(`Couldn't subscribe`, e))
Using Apollo Client:
// Configuration
import { createNetworkInterface } from 'absinthe-phoenix-apollo';
const client = new ApolloClient({
networkInterface: createNetworkInterface({
uri: 'http://localhost:3010/graphql',
wsUri: 'ws://localhost:3010/socket',
... otherConfigurationOptions
})
});
// Use client as normal
Using Relay (Modern):
// Configuration
import { createNetwork } from 'absinthe-phoenix-relay';
const network = createNetwork({
uri: 'http://localhost:3010/graphql',
wsUri: 'ws://localhost:3010/socket',
... otherConfigurationOptions
});
// Use network as normal (in your environment, etc)