传递给解析器的不是有效的 GraphQL DocumentNode。您可能需要使用“graphql-tag”或其他方法将操作转换为文档

Bha*_*ala 7 reactjs aws-sdk graphql react-native aws-appsync

我是 react-native、graphql 和 Aws AppSync 的新手。我们尝试使用 aws appsync 构建一个react-native应用程序。当我运行react-native run-android时,它抛出一个错误说

传递给解析器的不是有效的 GraphQL DocumentNode。您可能需要使用“graphql-tag”或其他方法将操作转换为文档

请看一下

https://i.stack.imgur.com/hr5fA.jpg

用于获取参考的图像 url。

 import { graphql, compose } from 'react-apollo';
import gql from 'graphql-tag';

 const listMessages = graphql(gql`
    query listMessages {
      listMessages {
        id
        text
        createdAt
        sentBy {
          id
          name
        }
      }
    }`)
  const createMessage = graphql(gql`
  mutation createMessage($text: String!, $sentById: ID!) {
      createMessage(input : {
        id : $sentById
        text : $text
        createdAt : "1/06/2018"
        updateAt:"1/06/2018"
      }){
       sentBy {
         id
         name
      }
      }
     }`)


     export default compose(
        graphql(createMessage,{
          options: {
            fetchPolicy: 'cache-and-network'
          }
        }, {name : 'createMessageMutation'}),
        graphql(listMessages, {
          options : {
            fetchPolicy: 'cache-and-network'
          }
        }, {name: 'listMessagesQuery'})
      )(Chat)
Run Code Online (Sandbox Code Playgroud)

上面的代码就是graphql的基本实现。

我无法真正找出错误,请帮助我。谢谢!提前

Dan*_*den 1

您调用该函数两次 - 一次在andgraphql的定义中,然后在您的导出语句中再次调用。您需要更改定义用于查询的变量的方式:listMessagescreateMessage

const createMessage = gql`
  mutation createMessage($text: String!, $sentById: ID!) {
    createMessage(input : {
      id : $sentById
      text : $text
      createdAt : "1/06/2018"
      updateAt:"1/06/2018"
    }) {
      sentBy {
        id
        name
      }
    }
 }`
Run Code Online (Sandbox Code Playgroud)