是否有方法将参数传递到GraphQL查询中,以指定应在其上运行的GraphQL类型?

是否有方法将参数传递到GraphQL查询中,以指定应在其上运行的GraphQL类型?,graphql,Graphql,我是GraphQL新手,希望能够在查询中使用变量作为GraphQL名称 我尝试使用标准的$语法,但没有成功 工作查询: query Tryptych($section: SectionsEnum = home) { enGB: entries(section: [$section], site: "enGB") { ... on Home { tryptych { ...tryptychFields } } } } 我希望能够做到

我是GraphQL新手,希望能够在查询中使用变量作为GraphQL名称

我尝试使用标准的$语法,但没有成功

工作查询:

query Tryptych($section: SectionsEnum = home) {
  enGB: entries(section: [$section], site: "enGB") {
    ... on Home {
      tryptych {
        ...tryptychFields
      }
    }
  }
}
我希望能够做到:

query Tryptych($section: SectionsEnum = home, $interface: SomeType = Home) {
  enGB: entries(section: [$section], site: "enGB") {
    ... on $interface {
      tryptych {
        ...tryptychFields
      }
    }
  }
}

参考片段:

fragment tryptychFields on TryptychTryptych {
  __typename
  theme
  tagline
  firstImageTitle
  firstImageContent
  firstImageAsset {
    url
  }
  firstImageLink
  secondImageTitle
  secondImageContent
  secondImageAsset {
    url
  }
  secondImageLink
  thirdImageTitle
  thirdImageContent
  thirdImageAsset {
    url
  }
  thirdImageLink
}
在我想要实现的代码片段中,我得到了错误消息:

Expected Name, found $

感谢您的帮助。

变量只能有一种类型,该类型必须是输入类型(即标量、枚举或输入对象类型),并且只能在需要输入类型(即字段或指令参数)的情况下使用。换句话说,您建议的语法不受支持

如果同一字段可能返回多个类型,则可以使用任意数量的片段按类型指定选择集。实际选择集将在运行时计算字段类型时确定。例如,如果
animal
字段返回
Cat
Dog
Bird
类型的并集:

query {
  animal {
    ... on Cat {
      meows
    }
    ... on Dog {
      barks
    }
    ... on Bird {
      chirps
    }
  }
}
您还可以使用
@skip
@include
指令来控制选择哪些字段:

query ($inAHouse: Boolean!, $withAMouse: Boolean!) {
  greenEggs @skip(if: $inAHouse)
  ham @include(if: $withAMouse)
}
您可以在一个文档中包含多个操作,然后在请求中指定
operationName
,告知服务器要运行的操作:

query OperationA {
  foo
}

query OperationB {
  bar
}

变量只能有一种类型,该类型必须是输入类型(即标量、枚举或输入对象类型),并且只能在预期输入类型(即字段或指令参数)的情况下使用。换句话说,您建议的语法不受支持

如果同一字段可能返回多个类型,则可以使用任意数量的片段按类型指定选择集。实际选择集将在运行时计算字段类型时确定。例如,如果
animal
字段返回
Cat
Dog
Bird
类型的并集:

query {
  animal {
    ... on Cat {
      meows
    }
    ... on Dog {
      barks
    }
    ... on Bird {
      chirps
    }
  }
}
您还可以使用
@skip
@include
指令来控制选择哪些字段:

query ($inAHouse: Boolean!, $withAMouse: Boolean!) {
  greenEggs @skip(if: $inAHouse)
  ham @include(if: $withAMouse)
}
您可以在一个文档中包含多个操作,然后在请求中指定
operationName
,告知服务器要运行的操作:

query OperationA {
  foo
}

query OperationB {
  bar
}