Apollo客户端订阅可在游乐场中使用,但不能在Expo App中使用

时间:2019-07-09 00:06:13

标签: graphql expo subscription react-apollo apollo-client

订阅在操场上工作并返回期望的字段,但在apollo graphql客户端中不起作用,它不返回任何内容!这是在游乐场和客户端中都使用的查询:

export const PARTY_SUBSCRIPTION = gql`
  subscription onPartyUpdated($hostname: String!) {
    party(hostname: $hostname) {
      mutation
      node {
        id
        users {
          username
        }
        open
        hostname
      }
    }
  }
`;

这是我的apollo客户端配置文件:

const DEV_DB_ENDPOINT = "http://192.168.1.3:4000/";

const authLink = setContext(async (_, { headers }) => {
  const token = await AsyncStorage.getItem("userToken");
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : ""
    }
  };
});

const wsLink = new WebSocketLink({
  uri: "ws://localhost:5000/",
  options: {
    reconnect: true
  }
});
const httpLink = new HttpLink({
  uri: DEV_DB_ENDPOINT,
  credentials: "same-origin"
});

const link = split(
  // split based on operation type
  ({ query }) => {
    const definition = getMainDefinition(query);
    console.log(query);
    return (
      definition.kind === "OperationDefinition" &&
      definition.operation === "subscription"
    );
  },
  wsLink,
  httpLink
);

const client = new ApolloClient({
  link: ApolloLink.from([
    onError(({ graphQLErrors, networkError }) => {
      if (graphQLErrors)
        graphQLErrors.map(({ message, locations, path }) =>
          console.log(
            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
          )
        );

      if (networkError) console.log(`[Network error]: ${networkError}`);
    }),
    authLink,
    link
  ]),
  cache: new InMemoryCache()
});

export { client };

这是我的订阅组件

<Subscription
  subscription={PARTY_SUBSCRIPTION}
  variables={{
    hostname: "Amir004"
  }}
  onError={err => console.log(err)}
  onCompleted={data => console.log(data)}
>
  {() => {
    return <Text>Current list of friends in your party : </Text>;
  }}
</Subscription>

该组件没有console.log任何错误或数据!

我们非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

对于仍在努力解决此问题的任何人,使用Query组件并调用subscriptionToMore似乎可行!我仍然不知道问题出在哪里,目前,我只能使用查询组件进行订阅! 有关subscribeToMore的更多信息:https://www.apollographql.com/docs/react/advanced/subscriptions/#subscribetomore