Graphql does not returns nested query result

Viewed 63

I am new to Graphql world. I am using AWS' DynamoDB, Lambda and serverless framework.

I have made simple book Graphql Schema. In book query, I have used author Type.

I have made resolvers, where graphql mutation or query, it will invoke each Lambda functions. For create a Book Item I have made nested mutation, where book can have multiple authors and I successfully create a Book Item. I want to make two diffrent queries. One is Book query where author has type and another is AllAuthors query where it will return all authors and their books. But I am having issue returning an author's type and queries. Both of the queries it returns null like this.

Books queries enter image description here

All Authors queries

enter image description here

This is my all Graphql schema types:

   enum Genre {
    adventure
    drama
    scifi
  }

  enum Authors {
    AUTHOR
  }
   
   
   // Query Type
   type Query {
    authors(author: Authors): [Author]
    books(book: String): [Book]
  }
   
   // Mutation type
   type Mutation {
    createBook(input: CreateBook!): Book
  }
 
 type Author {
    id: ID!
    authorName: String
    book: [Book]
  }

type Book {
    id: ID!
    name: String
    price: String
    publishingYear: String
    publisher: String
    author: [Author] // Author Type
    description: String
    page: Int
    genre: [Genre] // It's an ENUM 
  } 
  

 
 input CreateBook {
    name: String
    price: String
    publishingYear: String
    publisher: String
    author: [CreateAuthor]
    description: String
    page: Int
    genre: [Genre]
  }

  input CreateAuthor {
    authorName: String!
  }

This is how I create a Book

enter image description here

My DynamoDB image looks like this: enter image description here.

This is my Book queries Lambda function

import AWS from 'aws-sdk';
import { Book } from '../../generated/schema';

async function books(_: unknown, input: { book: string }): Promise<Book> {
  const dynamoDb = new AWS.DynamoDB.DocumentClient();

  const params = {
    TableName: process.env.ITEM_TABLE ? process.env.ITEM_TABLE : '',
    IndexName: 'GSI1',
    KeyConditionExpression: 'GSI1PK = :hkey',
    ExpressionAttributeValues: {
      ':hkey': `${input.book}`,
    },
  };

  const { Items } = await dynamoDb.query(params).promise();

  const Books = Items && Items.map((book: Book) => book); // where author returns null

  return Books;
}

export default books;

This is my authors queries

import AWS from 'aws-sdk';
import { Author, Authors } from '../../generated/schema';

async function authors(
  _: unknown,
  input: { author: Authors }
): Promise<Author> {
  const dynamoDb = new AWS.DynamoDB.DocumentClient();

  const params = {
    TableName: process.env.ITEM_TABLE ? process.env.ITEM_TABLE : '',
    KeyConditionExpression: 'PK = :hkey',
    ExpressionAttributeValues: {
      ':hkey': `${input.author}`,
    },
  };

  const { Items } = await dynamoDb.query(params).promise();

  const authors =
    Items &&
    Items.map((d) => {
      console.log({ d });

      d.id = d.PK;
      return d;
    });

  return authors;
}

export default authors;

This is my resolvers

import createBook from './mutations/createBook';
import authors from './queries/authors';
import books from './queries/books';

export default {
  Query: {
    books,
    authors,
  },
  Mutation: {
    createBook,
  },
};

0 Answers
Related