我目前正在反应原生应用中使用react-apollo而我正面临一个问题 .

每当我尝试创建包含联合的查询时,它都会失败,从而引发以下网络错误:

网络错误:请求的值不是对象的键

我在React.js应用程序中使用完全相同的代码,它可以正常工作 . GraphQL endpoints 是我自己的服务器,使用Youshido软件包在PHP中开发 .

这是片段声明部分:

const httpLink = new HttpLink({uri: 'http://10.1.68.64:8000/graphql'});

const fragmentMatcher = new IntrospectionFragmentMatcher({
    introspectionQueryResultData: {
      __schema: {
        types: [
          {
            "kind": "UNION",
            "name": "QuestionUnion",
            "possibleTypes": [
              {
                "name": "QuestionUnionTypeA"
              },
              {
                "name": "QuestionUnionTypeB"
              },
              {
                "name": "QuestionUnionTypeC"
              }
            ]
          },
        ],
      },
    }
});

const cache = new InMemoryCache({ fragmentMatcher });

const client = new ApolloClient({
    link: httpLink,
    cache: cache,
});

Screen.js文件中的查询(在React.js和react-native中完全相同):

const FindForm = gql`query FindForm($id: ID!){
    findForm(id: $id){
        id
        title
        questions {
            id
            ... on QuestionUnionTypeA {
                parameterA
            } 
            ... on QuestionUnionTypeB {
                parameterB
            }
            ... on QuestionUnionTypeC {
                parameterC
            }
        }
    }
}`;

在同一Screen.js文件中使用查询的部分:

const resultDisplayer = ({ data }) => {
    if(data.error){
        return <Text> {JSON.stringify(data)} </Text>;
    }
    if(data.loading){
        return <Spinner/>;
    }
    else {
        return <Text> Ok </Text>;
    }
};

const query = graphql(FindForm, {
    options: {
        variables: {
            id: this.state.id,
        }
    }
})(resultDisplayer);

令人不安的是,在少数情况下不会抛出错误:

  • 当我远程调试应用程序时

  • 当查询不包含union部分时

  • 当union部分没有返回时(如果没有QuestionUnionTypeA,B和C的实例)

因此,我想我做错了什么,但我似乎无法找到我误解的地方 . 如果你们有任何想法,我愿意尝试任何出现的事情 .