0

I want to render on the page, simple data from manual API, but it seems that I cannot get access to the nested query.

This is what I have:

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

//access to queries and map function
const ChannelsList = ({ data: {loading, error, items }}) => {
  if (loading) {
    return <p>Loading ...</p>;
  }
  if (error) {
    return <p>{error.message}</p>;
  }

  return (
    <div className="channelsList">

      { items.map( itm => <div key={itm.id} className="channel">{itm.sku}</div> ) }
    </div>
  );
};

const channelsListQuery = gql`
  query ChannelsListQuery {
      allLinks {
        items {
          id
          sku
        }
      }
  }
`;

export default graphql(channelsListQuery)(ChannelsList);

Don't mind this "channel" names, this is from example that I used for my own api. The problem is, all the queries that contain "id, names, sku" etc is in "items" type and it looks like for me it cannot get access to it. The query in graphqli server is working perfectly. So, the question is- how to get access to nested queries, so that I can use map and render it on page?

Schema structure:

const typeDefs = `
type Link {
    items: [itemapi]      
    }

    type itemapi {
        id: ID!
        sku: String!
        name: String!
    }
    type Query {
    allLinks: Link!
  }
`;

@Solution by @OzySky

const ChannelsList = ({ data: {loading, error, allLinks }}) => {
  if (loading) {
    return <p>Loading ...</p>;
  }
  if (error) {
    return <p>{error.message}</p>;
  }

  return (
    <div className="channelsList">
      { allLinks.items.map( aLitm => <div key={aLitm.id} className="channel">{aLitm.sku}</div> ) }
    </div>
  );
}; 

2 Answers 2

1

In react-apollo, query data is passed through a prop with the name of the query. So in your case you would access items through the data.allLinks prop.

Sign up to request clarification or add additional context in comments.

6 Comments

You mean, instead of items i should use allLinks and that get me access to items?
This is really a comment, not an answer. With a bit more rep, you will be able to post comments.
From what I understood from you allLinks should be an array of type items, but that depends of course on the way you structured your schema
Then data.allLinks.items should be an array of type itemapi
@OzySky Edited first post, take a look, it still dont render anything.
|
0

If you have a highly nested return value from your query one way to access the nested object is to first turn it into an array with Object.values(). Then access the field the array items and their corresponding values with dot notation.

const myQuery = gql`
  query {
    users {
      id
      name
      pet {
        id
        name
      }
    }
  }
`;

// Inside Render function if using React ...
return users.map(({ id, name, pet }) => (
        <div key={id}>
          <ul>
            <li>
              {id} {name}
              {Object.values({ pet })[0].name}
            </li>
          </ul>
        </div>
      ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.