#graphql custom query on upload relationship returns null instead of data

1 messages · Page 1 of 1 (latest)

plain flower
#

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',
    },

brazen spruce
#

Does adding depth: 0 to your local API find() query fix it?

#

That was an issue that I had

plain flower
#

yes. it solved the issue 😶

brazen spruce
#

I'll make sure to document this!