graphql custom query on upload relationship returns null instead of data

default discord avatar
megetron
7 months ago
4

querying with graphql on a custom query doesn't query a relationship data.





//payload.config
...
        search: {
          type: payload.Query.fields.Products.type,
          args: {
            input: {
              type: new GraphQL.GraphQLInputObjectType
              (
                {
                name: 'SearchInput',
                fields: {
                  term: { type:  GraphQL.GraphQLString },
                }
              )
            }
          },
          resolve: search,
        }

//search file
export default async (parent, args, context) => {  
  let query:any = {
    collection: "products",
    where: {},
    ...sort
  }
  
  if(args.input.term)
    query.where.name = { contains: args.input.term }
  
  const products = await context.req.payload.find(query);
  for (let product of products.docs){
    console.log(product) // output below

  }
  return products;
}

//products output:

{
  id: '6458a18fceb82968a83902cd',
  mainImage: {
    id: '6458a18eceb82968a83902c9',
    ...
  }
}

//query the graphql
query getAllProducts($input: SearchInput! = { term: "SE" }) {
  search(input: $input) {
    docs {
      mainImage {
        id
      }
    }
  }
}
//results
{
  "data": {
    "search": {
      "docs": [
        {
          "mainImage": null // mainImage is null
        }
      ]
    }
  }
}

//model
    {
      name: 'mainImage',
      type: 'upload',
      relationTo: 'media-main',
    },
  • default discord avatar
    noheadphones
    7 months ago

    Does adding

    depth: 0

    to your local API find() query fix it?



    That was an issue that I had

  • default discord avatar
    megetron
    7 months ago

    yes. it solved the issue 😶

  • default discord avatar
    noheadphones
    7 months ago

    I'll make sure to document this!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.