How to use the apollo-client-preset.split function in apollo-client-preset

To help you get started, we’ve selected a few apollo-client-preset examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LFSCamargo / Chatify / packages / app / src / config / client.ts View on Github external
})

const httpLink = createHttpLink({
  uri: GRAPHQL_URI,
})

const authLink = setContext(async (_, { headers }) => {
  return {
    headers: {
      ...headers,
      Authorization: await AsyncStorage.getItem('token'),
    },
  }
})

const link = split(
  ({ query }) => {
    // @ts-ignore
    const { kind, operation } = getMainDefinition(query)
    return kind === 'OperationDefinition' && operation === 'subscription'
  },
  wsLink,
  httpLink,
)

const cache = new InMemoryCache()

const stateLink = withClientState({
  cache,
  resolvers: {
    Mutation: {
      // @ts-ignore
github Weakky / prisma-ecommerce / mobile / src / graphql / setupApollo.js View on Github external
cachedToken = token;

        // return the headers to the context so httpLink can read them
        resolve({
          headers: {
            ...headers,
            authorization: token ? `Bearer ${token}` : null,
          },
        });
      }),
  );

  const httpLinkWithAuth = authMiddleware.concat(httpLink);

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

  const client = new ApolloClient({
    link,
    cache: new InMemoryCache({ dataIdFromObject: o => o.id }),
    connectToDevTools: true,
  });

  return client;
}
github realm / my-first-realm-app / react-qbs / src / App / index.tsx View on Github external
// Create a configuration from the user
    const config = await GraphQLConfig.create(user, path, null, true);
    // Construct an HTTP link that knows how to authenticate against ROS
    const httpLink = concat(
      config.authLink,
      new HttpLink({ uri: config.httpEndpoint }),
    );
    // Construct a link based on WebSocket that can be used for real-time subscriptions
    const webSocketLink = new WebSocketLink({
      options: {
        connectionParams: config.connectionParams,
      },
      uri: config.webSocketEndpoint,
    });
    // Combine the links in a way that splits based on the operation
    const link = split(
      ({ query }) => {
        const definition = getMainDefinition(query);
        return (
          definition.kind === "OperationDefinition" &&
          definition.operation === "subscription"
        );
      },
      webSocketLink,
      httpLink,
    );
    // Create a client with the combined links
    return new ApolloClient({
      cache: new InMemoryCache(),
      link,
    });
  }
github catalinmiron / uzual-mobile / config / setup.js View on Github external
if (error.message === 'Network request failed') {
          // if (_operation.operationName === 'createPost') {
          return true;
          // }
        }
        return false;
      }
    }
  });

  const httpLinkWithAuth = authMiddleware.concat(httpLink);
  const wsLinkWithAuth = authMiddleware.concat(wsLink);

  const link = concat(
    retryLink,
    split(
      ({ query }) => {
        const { kind, operation } = getMainDefinition(query);
        return kind === 'OperationDefinition' && operation === 'subscription';
      },
      wsLinkWithAuth,
      httpLinkWithAuth
    )
  );

  const defaultOptions = {
    watchQuery: {
      fetchPolicy: 'cache-and-network',
      errorPolicy: 'all'
    },
    query: {
      fetchPolicy: 'cache-and-network',
github DCtheTall / node-video-chat / src / client / index.jsx View on Github external
});
const wsLink = new WebSocketLink({
  uri: process.env.GRAPHQL_WS_URI,
  options: {
    reconnect: true,
  },
});
const subscriptionMiddleware = {
  applyMiddleware(options, next) {
    const { token } = store.getState();
    options.connectionParams = { authToken: token };
    next();
  },
};
wsLink.subscriptionClient.use([subscriptionMiddleware]);
const link = split(
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },
  wsLink,
  httpLink,
);

const cache = new InMemoryCache().restore(window.__APOLLO_STATE__);
const client = new ApolloClient({ link, cache, connectToDevTools: process.env.NODE_ENV === 'development' });

delete window.__APOLLO_STATE__;
delete window.__JWT_TOKEN__;

/**
 * @class Router