typescript Apollo websocket TRM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript Apollo websocket TRM相关的知识,希望对你有一定的参考价值。

in TRM
websocket is not being used now. 
this is for backup codes
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { split } from 'apollo-link';
import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
import React from 'react';
import { ApolloProvider } from 'react-apollo';
import authService from './authService';

import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
import introspectionQueryResultData from './fragmentTypes.json';
import { env } from 'src/env.config';

const httpLink = createHttpLink({
  uri: env.ApolloServerURL
})

const authLink = setContext((_, { headers }) => {
  const token = authService.getUserData()
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : ''
    }
  }
})

/**
 * websocket is for subscription and the subscription functionality isn't currently in use
 */
const wsLink = new WebSocketLink({
  uri: `ws://localhost:4000`,
  options: {
    reconnect: true,
    connectionParams: {
      authToken: authService.getUserData(),
    }
  }
})

const link = split(
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query)
    return kind === 'OperationDefinition' && operation === 'subscription'
  },
  wsLink,
  authLink.concat(httpLink)
)

/**
 * to remove heuristic matching error
 */
const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData
});

const client = new ApolloClient({
  link,
  cache: new InMemoryCache({ fragmentMatcher }),
  connectToDevTools: true,
})

export default function ApolloWrapper(props) {
  return (
    <ApolloProvider client={client}>
      {props.children}
    </ApolloProvider>)
}
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { split } from 'apollo-link';
import { setContext } from 'apollo-link-context';
import { createHttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
import React from 'react';
import { ApolloProvider } from 'react-apollo';
import authService from './authService';

import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
import introspectionQueryResultData from './fragmentTypes.json';
import { env } from 'src/env.config';

const httpLink = createHttpLink({
  uri: env.ApolloServerURL
})

const authLink = setContext((_, { headers }) => {
  const token = authService.getUserData()
  console.log('token', token)
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : ''
    }
  }
})

/**
 * websocket is for subscription and the subscription functionality isn't currently in use
 */
// const wsLink = new WebSocketLink({
//   // uri: `ws://localhost:4000`,
//   options: {
//     reconnect: true,
//     connectionParams: {
//       authToken: authService.getUserData(),
//     }
//   }
// })

// const link = split(
//   ({ query }) => {
//     const { kind, operation } = getMainDefinition(query)
//     return kind === 'OperationDefinition' && operation === 'subscription'
//   },
//   wsLink,
//   authLink.concat(httpLink)
// )

/**
 * to remove heuristic matching error
 */
const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData
});

const client = new ApolloClient({
  // link,
  link : authLink.concat(httpLink),
  cache: new InMemoryCache({ fragmentMatcher }),
  connectToDevTools: true,
})

export default function ApolloWrapper(props) {
  return (
    <ApolloProvider client={client}>
      {props.children}
    </ApolloProvider>)
}

以上是关于typescript Apollo websocket TRM的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Apollo 工具为查询生成 Typescript 类型,包括 Apollo @client 和 @rest 指令?

NextJS/Typescript/Apollo 错误;类型上不存在属性

TypeScript,Apollo GraphQL 产生不可分配的节点获取错误

Apollo 的 TypeScript 类型使用Mutation 的 refetchQueries?

typescript Apollo websocket TRM

使用 TypeScript 反应 Apollo useQuery 钩子