Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

graphql custom query on upload relationship returns null instead of data

default discord avatar
megetron_last year
3

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
    paulpopuslast year

    Does adding

    depth: 0

    to your local API find() query fix it?



    That was an issue that I had

  • default discord avatar
    megetron_last year

    yes. it solved the issue 😶

  • default discord avatar
    paulpopuslast year

    I'll make sure to document this!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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