【问题标题】:GraphQL add introspection from a variableGraphQL 从变量添加自省
【发布时间】:2019-07-10 06:41:51
【问题描述】:

我想将变量自省添加到我的应用程序中。

我有这样的自省:

{"data":{"__schema":{"queryType":{"name":"PageContentQuery"}....  

(我从休息请求中得到)

我希望这个变量成为架构提供者,可以吗?

完整的变量在这里:https://stackblitz.com/edit/ts-graphql-demo-ypgi18?file=index.tsx

const fetcher = (params: any) => {
  console.log(params);
  return graphql(schema, params.query, params.variables);
}

const defaultQuery = `{
 page{id,contents}
}`;

render(
  <GraphiQL fetcher={fetcher} schema={schema} defaultQuery={defaultQuery}/>,
  document.getElementById('root'),
);

谢谢

【问题讨论】:

  • 您的架构与您的自省查询不匹配。如果不是您要使用的架构,为什么还要传入该架构?

标签: graphql graphiql


【解决方案1】:

鉴于自省查询的结果,您可以使用buildClientSchema 构建架构:

import { buildClientSchema } from 'graphql'

const schema = buildClientSchema(introspectionResult)

然后您可以将该架构作为道具传递给 GraphiQL 组件:

<GraphiQL fetcher={fetcher} schema={schema} />

当然,这通常不是必需的——如果未传入架构,那么 GraphiQL 将使用您提供的 fetcher 为您进行自省查询。

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 2019-12-28
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    相关资源
    最近更新 更多