简体   繁体   English

useParams 用于阿波罗 GraphQL 查询

[英]useParams for apollo GraphQL query

I have an issue with my code with react and Apollo Client, i cant get the id in my URL (useParam) and put in my query for apollo i tried lot of possibilities but i cant find the right one.我的代码与 react 和 Apollo 客户端有问题,我无法在我的 URL(useParam)中获取 id 并输入我对 apollo 的查询,我尝试了很多可能性,但我找不到合适的。

If you have the right one it will be perfect;)如果你有合适的,那将是完美的;)

i think the react router part is ok but the problem is to pass the id in my query with apollo, i'am a new graphql user... cool but not easy to understand directkly the concept我认为反应路由器部分没问题,但问题是用阿波罗在我的查询中传递 id,我是一个新的 graphql 用户......很酷但不容易直接理解这个概念

const GET_MEETING_ACCESS = gql`
query {
    meeting (id:$id){
      protectedAccess
      
    }
  }
`;


export default function MeetingAccessPage() {
    let { id } = useParams();
    const { loading, error, data } = useQuery(GET_MEETING_ACCESS, {
        variables: { id:id },
          });

    if (loading) return 'Loading...';
    if (error) return `Error! ${error.message}`;
    return (
        <div className='flex flex-col items-center h-screen w-screen bg-gradient-to-r from-blue-600 to-purple-500 bg-gradient-to-r'>
            <div className='flex text-center h-auto p-6 flex-col h-1/3'>
                <img src={logo} alt="logo" className='flex object-fill' />
                <p className='text-white text-xl font-bold pt-6'>{data.Name}</p>
                

and here my router React这里是我的路由器 React

<ApolloProvider client={client}>
      <Router>
        <Switch>
          <Route path="/meeting/:id">
            <MeetingAccessPage />
          </Route>
        </Switch>
      </Router>
    </ApolloProvider>

You need to update your query to as below您需要将您的查询更新为如下

const GET_MEETING_ACCESS = gql`
  query Meeting($id: String){
    meeting (id:$id){
      protectedAccess
      
    }
  }
`;

Read more about how to use variables with GraphQl here在此处阅读有关如何将变量与 GraphQl一起使用的更多信息

You should declare the type of variable you use when you define the graphql query.您应该在定义 graphql 查询时声明您使用的变量类型。 like this...像这样...

const GET_MEETING_ACCESS = gql`    
    query my_meeting($id: String!) {
       meeting (id:$id){
         protectedAccess
       }
    }
`;

ask me if it is not clear to you.问我你是否不清楚。

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

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