简体   繁体   English

如何在graphql查询中使用参数?

[英]How to use parameters in a graphql query?

Hi im a bit new in react and graphql, everything is working nice but i dont get how i can pass parameters to a query 嗨,我在react和graphql中有点新了,一切都很好,但是我不知道如何将参数传递给查询

i need some help 我需要协助

 import React from 'react'; import { gql, graphql } from 'react-apollo'; const CourseForm = ({ data: {loading, error, getData }}) => { const testVariable = 2; console.log(getData); return ( <form > <div className="form-group"> <label className="label-control">test</label> <input type="text" className="form-control"/> </div> <button type="submit" className="btn btn-default"> Save </button> </form>); } const testQuery = gql` query getData($id: Int!){ getData(id: $id) { id, field { id } } } `; const test = graphql( testQuery,{ options: { variables: { id: 1 } }} )(CourseForm); export default test; 

for example how can i use testVariable value in my query? 例如,如何在查询中使用testVariable值? to fetch data with id 2 获取ID为2的数据

i have seen some snippets using ownProps but i dont understand it, should i use props? 我已经看到一些使用ownProps的片段,但我不明白,我应该使用道具吗? i feel a bit lost 我有点迷失

Thanks guys! 多谢你们!

You can use props like in this examples: 您可以在以下示例中使用道具:

 // The caller could do something like: <ProfileWithData avatarSize={300} /> // And our HOC could look like: const ProfileWithData = graphql(CurrentUserForLayout, { options: ({ avatarSize }) => ({ variables: { avatarSize } }), })(Profile); 

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

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