![](/img/trans.png)
[英]NestJS 5 GraphQL Error Query defined in resolvers, but not in schema
[英]GraphQL: A schema must have a query operation defined
我的 IDE(帶有 JS GraphQL 的 Phpstorm)給了我架構的標題錯誤。
我是 GraphQL 的新手,如果實際的查詢操作只在根級別有一個突變,那么應該將查詢設置為什么?
下面是從他們的 GraphQL API 的 (Shopify) 教程中提取的實際查詢。 我正在復制我的本地架構定義,試圖適應其形狀。
如您所見,查詢完全嵌套在一個突變中,所以我不知道根級別的查詢定義應該具有什么。
// 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
}
你看到的錯誤是指GraphQL 規范的這個規定:
必須提供查詢根操作類型,並且必須是 Object 類型。
有幾個提議可以取消此限制,但根據最新的(2018 年 6 月)規范,如果沒有 Query 類型,則架構被視為無效。 該規范還指出對象類型(包括查詢)不能為空。
我的建議:只需添加一個簡單的查詢類型,例如
type Query {
ping: String @deprecated(reason: "https://stackoverflow.com/questions/59868942/graphql-a-schema-must-have-a-query-operation-defined")
}
如果規范得到更新,您可以稍后將其刪除:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.