GraphQl从变量中添加内省



我想从变量中添加内省。

我有这种内省:

{"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'),
);

谢谢

给定内置查询的结果,您可以使用buildClientSchema构建模式:

import { buildClientSchema } from 'graphql'
const schema = buildClientSchema(introspectionResult)

然后,您可以将该模式作为Prop传递给GraphIQL组件:

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

当然,这通常不是必需的 - 如果未传递该架构,则GraphIQL只会使用提供的fetcher为您提供内省查询。

最新更新