0

I'm having trouble to make a query in React with Apollo. I know how to do it in the playground and basic queries in React but I can't figure out how to pass dynamically the values for an input inside the query. The query should be like this. This works on playground.

getCityByName(name: "London", country: "GB", config:{units:metric, lang: sp}) {
  id
  name
  weather {
    summary {
      description
    }
    temperature {
      actual
    }
  }
}

The units and lang are enum. In React I could only pass the name and country dinamically but not the config options, I only get errors and I've tried so many different syntax. The only way I can is hardcoding like this:

  const GET_DATA = gql`
    query getCity($name: String!, $country: String) {
      getCityByName(
        name: $name
        country: $country
        config: { units: metric, lang: sp }
      ) {
        id
        name
        country
        weather {
          summary {
            description
          }
          temperature {
            actual
          }
        }
      }
    }
  `;
  const { loading, data, error } = useQuery(GET_DATA, {
    variables: {
      name: 'London',
      country: 'GB',
    },
  });

How can I make units and lang dynamic?

4
  • define $config variable (read type from API docs), pass an object Commented Apr 10, 2021 at 21:16
  • Yes, I've tried but units and lang are type enum. units points to enum Unit { metric imperial kelvin }, and lang to enum Language. Commented Apr 10, 2021 at 22:18
  • stackoverflow.com/a/44414412/6124657 - enum value passed as string should be recognized as enum ... test in playground first Commented Apr 10, 2021 at 22:33
  • I could find the solution here finally stackoverflow.com/questions/66661988/… Commented Apr 10, 2021 at 23:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.