1
votes

I am trying to convert from an introspection query to a GraphQL schema using the npm GraphQL library.

It keeps stating:

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: { ... } 

Issue is I am getting it directly from GraphiQL Shopify and can't figure out how to validate my introspection return is correct.

Code:

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());

I thought the introspection result could be a string? Or is there not enough info to build a schema? Do I need to expand the number of fields? What's the bare minimum needed to exchange an introspection result into a schema?

1
@DanStarns It wasn't a careless edit...it had backticks already. - ugh StackExchange
I have tried it as a string. I thought backticks/template literals were a form of string. also toString() conversion and just standard ' to no luck - Kyle Calica-St
@DanStarns my code had backticks. - Kyle Calica-St

1 Answers

1
votes

You should use getIntrospectionQuery to get the complete introspection query needed. If the response you're working with is a string, it should then be parsed to an object before being passed to buildClientSchema -- the function accepts an object, not a string.

Here's an example directly querying a schema using the graphql function -- you'll need to modify it based on how you're actually executing the query.

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

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

Make sure that you are only passing in the data portion of the response. The object you pass in should look like this:

{
  __schema: {
    // ...more properties
  }
}