简体   繁体   English

无法使用Apollo过滤GraphQL查询并显示数据库数据对象

[英]Can't Filter GraphQL query with Apollo and display database data object

I am trying to use a form input in react to filter my mongodb database with graphql and apollo. 我试图在反应中使用表单输入来使用graphql和apollo过滤我的mongodb数据库。 I have the backend set up and working. 我已经设置好了后端并且正在工作。 I have the front end mostly working except I can't access my data in an apollo query. 我的前端大部分工作正常,除了无法访问阿波罗查询中的数据。 Not sure what I am missing so not sure the exact question to ask. 不知道我缺少什么,所以不确定要问的确切问题。 I am just doing a search or query not a mutation. 我只是在做搜索或查询而不是突变。 I have 3 components in my React app and I am using styled-components. 我的React应用程序中有3个组件,并且正在使用样式组件。 I just want to capture the form input data and store it in the apollo query for use. 我只想捕获表单输入数据并将其存储在阿波罗查询中以供使用。 I want to call <h1>{data.herb.name}</h1> and get the data object from my apollo server. 我想调用<h1>{data.herb.name}</h1>并从我的阿波罗服务器获取数据对象。

I know this is a full stack question so if you can't help maybe you can give me suggestions on how to better ask this in smaller chunks. 我知道这是一个完整的问题,因此,如果您不能帮忙,可以给我一些建议,以便更好地以较小的块数进行询问。 Thanks. 谢谢。

App.js App.js

import React, { Component } from 'react'
import ApolloClient from 'apollo-boost'
import { ApolloProvider } from 'react-apollo'
import { BrowserRouter as Router, Route, Switch } from 'react-route-dom'

import Home from './components/Home'
import Herb from './components/Herb'

const client = new ApolloClient({
 uri: 'http://localhost:4000/graphql',
})

export default class App extends Component {
 render() {
   return (
    <ApolloProvider client={client}>
     <Router>
      <Switch>
        <Route exact path="/" component={Home} />
        <Route path="/:herbId" component={Herb} />
      </Switch>
     </Router>
    </ApolloProvider>
  )
 }
}

Home.js Home.js

import React, { Component } from 'react'
import styled from 'styled-components'

import SearchInput from './Search'
import Herb from './Herb'
import bgImage from '../Elements/images/measure_colorized.jpg'

const HomeContainer = styled.div`
  width: 100vw;
  height: 100vh;
  background: url(${bgImage});
  background-size: cover;
  background-repeat: no-repeat;
`

const Title = styled.h1`
  font-size: 6rem;
  color: #fff;
`

export default class Home extends Component {
  state = { name: '' }

  handleInput = e => {
    const formData = {}
    formData[e.target.name] = e.target.value
    this.setState({ ...formData })
  }

  render() {
    const { name } = this.state
    return (
      <HomeContainer>
        <header>
          <Title>Measure App</Title>
        </header>
        <SearchInput name={name} onChangeValue={this.handleInput} />
        <Herb name={name} />
      </HomeContainer>
    )
  }
}

Search.js Search.js

import React, { Component, Fragment } from 'react'
import styled from 'styled-components'
import { Query } from 'react-apollo'
import gql from 'graphql-tag'

const SearchInput = styled.input`
  background: rgba(0, 0, 0, 0.6);
  width: 30vw;
  padding: 1rem;
  font-size: 1.6rem;
  color: white;
  border: none;
`

const SubmitBTN = styled.button`
  padding: 1rem;
  font-size: 1.6rem;
  border: none;
  background: #7ff2ca;
`

export default class Search extends Component {
  handleSubmit = e => {
    e.preventDefault()
  }

  render() {
    const { name, onChangeValue } = this.props
    return (
      <div>
        <Query query={GET_HERB_QUERY} variables={{ name }}>
          {(data, loading, error) => {
            if (loading) return 'Loading...'
            if (error) return `Error: ${error.message}`
            console.log(data)
            return (
              <Fragment>
                <SearchInput
                  name="name"
                  type="text"
                  placeholder="search"
                  value={name}
                  onChange={onChangeValue}
                />
                <SubmitBTN onClick={this.handleSubmit}>convert</SubmitBTN>
              </Fragment>
            )
          }}
        </Query>
      </div>
    )
  }
}

//writing query to fetch herb that matches search result
const GET_HERB_QUERY = gql`
  query searchHerbs($name: String) {
    herb(name: $name) {
      name
      description
      imageURL
    }
  }
`

As explained in the comment, when you wrap your component with a query, the query will be triggered when your component renders. 如注释中所述,当用查询包装组件时,将在组件呈现时触发查询。

You can manually fire the query and manage data as you wish. 您可以根据需要手动触发查询并管理数据。 And you can also use the refetch method using the wrapper to trigger the query again with different variables. 您还可以使用带包装的refetch方法,以不同的变量再次触发查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM