2

我是 react-native 和 appsync 的新手,graphql。当我尝试运行它时,我们正在尝试使用 react-native 实现一个 appsync 应用程序抛出一个错误说

14:16:38:必须包含查询定义。* null:null 在 getQueryDefinition - node_modules/apollo-cache-inmemory/lib/bundle.umd.js:649:64 在 diffQueryAgainstStore - node_modules/apollo-cache-inmemory/lib/bundle.umd.js:559:32 在 readQueryFromStore - node_modules/apollo-cache-inmemory/lib/bundle.umd.js:899:38 in read - node_modules/apollo-cache-inmemory/lib/bundle.umd.js:992:23 in readQuery * null:null in update - node_modules/apollo-client/bundle.umd.js:1609:0 in - node_modules/apollo-utilities/lib/bundle.umd.js:818:21 in tryFunctionOrLogError * null:null in - node_modules/apollo-cache-inmemory /lib/bundle.umd.js:961:22 in performTransaction - node_modules/apollo-client/bundle.umd.js:1473:0 in markMutationResult - node_modules/apollo-client/bundle.umd.js:797:

我试图找出阿波罗缓存中的错误,但我找不到。每当我按下发送按钮时都会得到这个。

import React, { Component } from 'react';
import { KeyboardAvoidingView, Text, Button, TextInput, StyleSheet, Alert } from 'react-native';

export default class ChatInput extends Component {

  constructor(props) {
      super(props);
      this.userid = props.userid;
      this.state = {
          text: ''
      }
  }

  handleSend = () => {
      if (this.state.text === '') {
          return false;
      }
      const chat = {
          userid: this.userid,
          text: this.state.text,
          createdAt: new Date().toISOString()
      }
      this.props.onSubmit(chat);
      this.setState({
         text: ''
      })
      this.textInput.clear();
  }

  render() {
    return (
    <KeyboardAvoidingView>
        <TextInput ref={input => { this.textInput = input }} placeholder="Message.." 
        onChangeText={(text) => this.setState({text})} onSubmitEditing={this.handleSend} 
        autoFocus={true} blurOnSubmit={false} returnKeyType="send"></TextInput>
        <Button title="Send" onPress={this.handleSend}></Button>
    </KeyboardAvoidingView>
    );
  }
}



 const ChatInputData = compose(
    graphql(PostMessage, {
        options: {
            fetchPolicy: 'no-cache'
        },props: (props) => ({
                onSubmit: chat => {
                    props.mutate({
                        variables: chat,
                        optimisticResponse: {
                            __typename: 'Mutation',
                            post: {
                                __typename: ChatHistory,
                                id: chat.createdAt,
                               userid: chat.userid,
                                text: chat.text,
                                createdAt: chat.createdAt
                            }
                        },
                        update: (proxy, {data: {post}}) => {
                            const data = proxy.readQuery({ query: PostMessage });
                            data.listPostMessages.items.push(Object.assign({}, post));
                            proxy.writeData({query: listPostMessages, data});
                        }
                    })
                }
            })
        })
    )(ChatInput)

请帮帮我!在此先感谢

4

4 回答 4

0

在这一行:

 const data = proxy.readQuery({ query: PostMessage });

我没有看到PostMessage您想在哪里定义查询的参考。

于 2018-06-05T09:40:47.487 回答
0

该错误意味着在导入PostMessage的 中,找不到查询。确保它看起来像这样:(而不是等效的速记)

query postMessage($id: ID!) {
    postMessage(id: $id) {
        id
        title
    }
}
于 2018-06-05T19:05:39.037 回答
0

我通过纠正我的突变解决了这个错误,即在我的情况下是 PostMesssage。这个错误

必须包含查询定义

主要发生在您用 graphql 编写的查询不正确时。开始了解如何传递变量以及如何使用它们以及如何定义它们。

于 2018-06-07T05:24:40.533 回答
0

当我添加新的解析器函数(在新文件中)但未能将这些解析器的引用包含到 graphql 的可执行模式中时,我也遇到了这个错误。

于 2019-09-13T16:55:41.603 回答