Apollo客户端订阅在操场上工作,但在Expo应用程序中不起作用



订阅在操场上有效,返回预期字段,但在阿波罗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>

该组件不稳定。log任何错误或数据!

真正感谢任何帮助:(

对于仍在为此问题而苦苦挣扎的任何人,使用查询组件和调用sisscribetomore似乎有效!我仍然不知道源问题是什么,目前,我只能使用查询组件订阅!有关订户的更多信息:https://www.apollographql.com/docs/react/advanced/subscriptions/#subscribetetomore

相关内容

最新更新