内省查询对buildClientSchema

我正在尝试使用npm GraphQL库从自省查询转换为GraphQL模式。

它一直在说明:

devAssert.mjs:7 Uncaught Error: Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: { ... } 

问题是我直接从GraphiQL Shopify获得它,却无法弄清楚如何验证我的内省申报是否正确。

代码:

var introspection = `
{
  "data": {
    "__schema": {
      "types": [
        {
          "name": "Boolean"
        },{
          "name": "String"
        },{
          "name": "QueryRoot"
        },{
          "name": "Job"
        },{
          "name": "ID"
        },{
          "name": "Node"
        },{
          "name": "Order"
        }
      ]
    }
  },"extensions": {
    "cost": {
      "requestedQueryCost": 2,"actualQueryCost": 2,"throttleStatus": {
        "maximumAvailable": 1000,"currentlyAvailable": 980,"restoreRate": 50
      }
    }
  }
}`;

      let schema = buildClientSchema(introspection);
      //console.log(schema.printSchema());

我认为自省结果可能是字符串?还是没有足够的信息来构建模式?我是否需要扩展字段数?将自省结果转换为模式所需的最低限度是多少?

ildar_kh 回答:内省查询对buildClientSchema

您应该使用getIntrospectionQuery来获取所需的完整自省查询。如果您使用的响应是字符串,则应在将其传递给buildClientSchema之前将其解析为一个对象-该函数接受一个对象,而不是字符串。

下面是使用graphql函数直接查询模式的示例-您需要根据实际执行查询的方式对其进行修改。

const { getIntrospectionQuery,buildClientSchema,graphql } = require('graphql')

const schema = new GraphQLSchema(...)
const source = getIntrospectionQuery()
const { data } = await graphql({ source,schema })
const clientSchema = buildClientSchema(data)

确保只传递响应的data部分。您传入的对象应如下所示:

{
  __schema: {
    // ...more properties
  }
}
本文链接:https://www.f2er.com/3098649.html

大家都在问