mapQueriesToProps and variables

Hi guys,

im working on a filter with meteor and apollo, right now i have some components and some queries one of those queries is this:

marcadores: {
    query: gql`
      query getPeople($name: String, $hobbies: [String]){
        people(name: $name, hobbies: $hobbies){
          id
          name
          hobbies
        }
      }
    `,
    variables: {
      name: ownProps.name,
      hobbies: ownProps.hobbies
    },
    forceFetch: false,
 },

now… that works great if i dont call the query with any variable. But as soon i set for example $hobbies with ["running"] it starts asking me to set $name … of course if i set also $name with "Rob Stark" it works…

EDIT: ~Any clue why is that happening ? i have to create conditionals for that querie ? ~

I know it is because the querie is waiting variables, but is there a way to avoid that ? like

variables: {
   name: ownProps.name.dontAddToTheQueryIfNull()
}

:smiley:

Thanks!

Can you explain what you mean by “without variables”? Do you mean you remove the variables key from above?

Hey @sashko thanks

When i call the refetch of that querie if i set people with no arguments it works like a charm, but if i set any of those variables i posted then it doesn’t perform the querie because is missing one argument.

Lets make an example:

{
  people(hobbies: ["Biking", "Running"]){
    name
  }
}

so that query would be like:

SELECT name FROM people AS people WHERE people.hobbies IN ('Biking', 'Running');

But… gpl is waiting for $name so it wont call that querie.

VM1901:1 Uncaught Error: The inline argument "name" is expected as a variable but was not provided.

I want all the people that have in $hobbies “biking” and “running” but it wont let me perform that querie if i dont have a $name. I know i can make a (ownProps.name) || "Some Random Name" on my variables set but i dont want one person only.

I have to create other Query for what i want ?

Thanks!

I would suggest passing null for the variable, then filtering our null arguments in your resolver.

It looks like GraphQL doesn’t support totally optional variables, but they can be nullable.

1 Like