必须包含查询定义

Must contain a query definition

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

14:16:38: Must contain a query definition. * null:null in getQueryDefinition - node_modules/apollo-cache-inmemory/lib/bundle.umd.js:649:64 in diffQueryAgainstStore - node_modules/apollo-cache-inmemory/lib/bundle.umd.js:559:32 in 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:20 in next - node_modules/zen-observable-ts/node_modules/zen-observable/lib/Observable.js:150:3 in notifySubscription - node_modules/zen-observable-ts/node_modules/zen-observable/lib/Observable.js:195:5 in onNotify * null:null in next - node_modules/zen-observable-ts/node_modules/zen-observable/lib/Observable.js:150:3 in notifySubscription * null:null in flushSubscription - node_modules/zen-observable-ts/node_modules/zen-observable/lib/Observable.js:190:26 in * null:null in - node_modules/promise/setimmediate/core.js:37:14 in tryCallOne - node_modules/promise/setimmediate/core.js:123:25 in - ... 10 more stack frames from framework internals

我试图找出 apollo-cache 中的错误,但我找不到 one.Am 当我按下发送按钮时得到这个。

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)

请帮帮我!提前致谢

在这一行:

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

我没有在您想定义查询的地方看到对 PostMessage 的引用。

错误的意思是在导入的PostMessage中找不到查询。确保它看起来像这样:(而不是 shorthand 等价物)

query postMessage($id: ID!) {
    postMessage(id: $id) {
        id
        title
    }
}

我通过更正我的突变解决了这个错误,即我的 case.This 错误中的 PostMessage

Must contain a query definition

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

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