Graphql 中的动态函数名称
Dynamic function name in Graphql
我有以下查询
const COURSES = gql`
query(
$course: Course!
) {
**english**(course: $course) {
transfers(
options: {
desc: ["count_in", "count_out"]
}
) {
school {
address
symbol
tokenType
}
}
}
}
`
注意第 5 行以 'english' 开头,假设我有 1000 门不同的课程,例如 ['english'、'politics'、'arts'、....第 1000 门课程).我怎样才能使这个课程名称动态化。我没有开发后端,所以改变模式对我来说不是一个选择。如何通过使课程名称动态化来避免重复。
为了清楚起见,我想将变量名放在第 5 行的 'english' 的位置,以便该行的行为如下所示
english(course: $course) {
politics(course: $course) {
art(course: $course) {
anyCourseName(course: $course) {
更新:
我正在使用 useQuery 调用上面的查询并传递如下参数 $course
useQuery(
COURSES,
{
variables: {
course, //this serves as variable $course in above query
},
}
)
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')
?
你可以简单地这样做:
const course = "english";
const COURSES = gql`
query(
$course: Course!
) {
${course}(course: $course) {
transfers(
options: {
desc: ["count_in", "count_out"]
}
) {
school {
address
symbol
tokenType
}
}
}
}
`
或者如果您有多个查询:
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);
然后在你的 gql 中使用它:
const COURSES = gql`
query(
$course: Course!
)
{
${queries}
}
`;
我有以下查询
const COURSES = gql`
query(
$course: Course!
) {
**english**(course: $course) {
transfers(
options: {
desc: ["count_in", "count_out"]
}
) {
school {
address
symbol
tokenType
}
}
}
}
`
注意第 5 行以 'english' 开头,假设我有 1000 门不同的课程,例如 ['english'、'politics'、'arts'、....第 1000 门课程).我怎样才能使这个课程名称动态化。我没有开发后端,所以改变模式对我来说不是一个选择。如何通过使课程名称动态化来避免重复。
为了清楚起见,我想将变量名放在第 5 行的 'english' 的位置,以便该行的行为如下所示
english(course: $course) {
politics(course: $course) {
art(course: $course) {
anyCourseName(course: $course) {
更新: 我正在使用 useQuery 调用上面的查询并传递如下参数 $course
useQuery(
COURSES,
{
variables: {
course, //this serves as variable $course in above query
},
}
)
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')
?
你可以简单地这样做:
const course = "english";
const COURSES = gql`
query(
$course: Course!
) {
${course}(course: $course) {
transfers(
options: {
desc: ["count_in", "count_out"]
}
) {
school {
address
symbol
tokenType
}
}
}
}
`
或者如果您有多个查询:
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);
然后在你的 gql 中使用它:
const COURSES = gql`
query(
$course: Course!
)
{
${queries}
}
`;