简体   繁体   中英

GraphQL: A schema must have a query operation defined

My IDE (Phpstorm with JS GraphQL) is giving me the title error for my schema.

I'm new to GraphQL, what should the query be set to if the actual query operation only has a mutation at the root level?

Below is an actual query taken out of a (Shopify) tutorial for their GraphQL API. I'm copying my local schema definition below which attempted to accommodate its shape.

As you can see, The query is entirely nested in a mutation so I don't know what a query definition at the root level should even have.

// graphql.ts

import "isomorphic-fetch";

const buildPricingPlanQuery = (redirectUrl: string) => `mutation {
  appSubscribeCreate(
    name : "Plan 1"
    returnUrl : "${redirectUrl}"
    test : true
    lineItems : [
      {
        plan : {
          appUsagePricingDetails : {
            cappedAmount : {
              amount : 10
              , currencyCode : USD
            }
            terms : "Up to 50 products"
          }
        }
      }
      {
        plan : {
          appRecurringPricingDetails : {
            price : {
              amount : 10
              , currencyCode : USD
            }
            terms : "some recurring terms"
          }
        }
      }
    ]
  )
  {
    userErrors {
      field
      message
    }
    confirmationUrl
    appSubscription {
      id
    }
  }
}`;


export const requestSubscriptionUrl = async (ctx: any, accessToken: string, shopDomain: string) =>  {
  const requestUrl = `https://${shopDomain}/admin/api/2019-10/graphql.json`;

  const response = await fetch(requestUrl, {
    method : 'post'
    , headers : {
      'content-type' : "application/json"
      , 'x-shopify-access-token' : accessToken
    },
    body : JSON.stringify({query: buildPricingPlanQuery(`https://${shopDomain}`)})
  });

  const responseBody = await response.json();
  const confirmationUrl = responseBody
    .data
    .appSubscriptionCreate
    .confirmationUrl;

  return confirmationUrl;
};
// pricingSchema.graphql

# ------------ Minor Types

enum CurrencyCode {
  USD
  EUR
  JPY
}

type cappedAmount {
  amount: Int
  currencyCode : CurrencyCode
}

type appUsagePricingDetails {
  cappedAmount: cappedAmount
}

input PlanInput {
  appUsagePricingDetails: cappedAmount
  terms: String
}

type userErrors {
  field: String
  message: String
}

type appSubscription {
  id: Int
}

# ------------ Major Type and Schema definition

type PricingPlan {
  appSubscribeCreate(
    name: String!
    returnUrl: String!
    test: Boolean
    lineItems: [PlanInput!]!
  ): String
  userErrors: userErrors
  confirmationUrl: String
  appSubscription: appSubscription
}

schema {
  mutation: PricingPlan
}

The error you see is referring to this stipulation of the GraphQL specification :

The query root operation type must be provided and must be an Object type.

There have been a couple proposals to remove this restriction, but as of the latest (June 2018) spec, a schema is considered invalid if there is no Query type. The spec also states that Object types (including Query) cannot be empty.

My advice: Just add a simple query type, such as

type Query {
    ping: String @deprecated(reason: "https://stackoverflow.com/questions/59868942/graphql-a-schema-must-have-a-query-operation-defined")
}

If the spec gets updated, you can remove it later :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM