Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
Dynamic function name in Graphql

I have the following query

const COURSES = gql`
  query(
    $course: Course!
  ) {
    **english**(course: $course) {
      transfers(
        options: {
          desc: ["count_in", "count_out"]
        }
      ) {
        school {
          address
          symbol
          tokenType
        }
      }
    }
  }
`

Note the 5th line starting with 'english' so let's say I have 1000 different courses like ['english', 'politics', 'arts', .... 1000th course). How can I make this course name dynamic. I didn't develop the backend so change in schema is not an option for me. how can I avoid duplication by making course name dynamic.

To be clear I want to place variable name at the place of 'english' at line 5 so that this line can behave like below

   english(course: $course) {
   politics(course: $course) {
   art(course: $course) {
   anyCourseName(course: $course) {

Update: I'm calling above query using useQuery and passing parameter $course like below

useQuery(
    COURSES,
    {
      variables: {
        course, //this serves as variable $course in above query
      },
    }
  )
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const COURSES = (c) => gql`
  query(
    $course: Course!
  ) {
    ${c}(course: $course) {
      transfers(
        options: {
          desc: ["count_in", "count_out"]
        }
      ) {
        school {
          address
          symbol
          tokenType
        }
      }
    }
  }
const query = COURSES('art')

?

about 4 years ago · Juan Pablo Isaza Report

0

You can simply do it like this:

const course = "english";

const COURSES = gql`
  query(
    $course: Course!
  ) {
    ${course}(course: $course) {
      transfers(
        options: {
          desc: ["count_in", "count_out"]
        }
      ) {
        school {
          address
          symbol
          tokenType
        }
      }
    }
  }
`

Or if you have multiple queries:

const courses = [
  "english",
  "politics",
  "art",
  "anyCourseName",
];

const queries = courses.reduce((acc, query) => (
  acc.concat(`${query}(course: $course) {
      transfers(
        options: {
          desc: ["count_in", "count_out"]
        }
      ) {
        school {
          address
          symbol
          tokenType
        }
      }
  }\n`)
), "");

console.log(queries);

then use it in your gql:

const COURSES = gql`
  query(
    $course: Course!
  )
  {
    ${queries}
  }
`;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!